services-posters/0xdbPoster.php
2007-08-04 09:29:43 +00:00

128 lines
3.9 KiB
PHP

<?php
$id = $_GET['id'];
$data = json_decode(file_get_contents("http://0xdb.org/$id/posterData.json"), true);
$director = utf8_decode($data['director']);
$title = utf8_decode($data['title']);
$url = "http://0xdb.org/$id/posterStill.png";
$md5 = md5("$director/$title");
$rPoster = round(hexdec(substr($md5, 0, 2)) / 2);
$gPoster = round(hexdec(substr($md5, 2, 2)) / 2);
$bPoster = round(hexdec(substr($md5, 4, 2)) / 2);
$imagePoster = imagecreatetruecolor(80, 128);
$color = imagecolorallocate($imagePoster, $rPoster, $gPoster, $bPoster);
imagefilledrectangle($imagePoster, 0, 0, 79, 127, $color);
$color = imagecolorallocate($imagePoster, 0, 0, 0);
imagefilledrectangle($imagePoster, 0, 45, 79, 63, $color);
$imageStill = imagecreatefrompng($url);
$imagesize = getimagesize($url);
$wStill = $imagesize[0];
$hStill = $imagesize[1];
if ($wStill / $hStill >= 8 / 5) {
$xStill = round($wStill / 2 - $hStill * 4 / 5);
$yStill = 0;
$wStill = round($hStill * 8 / 5);
}
else {
$xStill = 0;
$yStill = round($hStill / 2 - $wStill * 5 / 16);
$hStill = round($wStill * 5 / 8);
}
imagecopyresampled($imagePoster, $imageStill, 0, 0, $xStill, $yStill, 80, 50, $wStill, $hStill);
/*
$color = imagecolorallocate($imagePoster, $rPoster + 128, $gPoster + 128, $bPoster + 128);
imagettftext($imagePoster, 10, 0, 4, 60, $color, 'ttf/DINMit', 'Bla bla bla');
*/
$imageForeground = imagecreatetruecolor(80, 128);
$colorBlack = imageColorAllocate($imageForeground, 0, 0, 0);
imagefilledrectangle($imageForeground, 0, 0, 79, 127, $colorBlack);
$imageFont = imagecreatefrompng('png/font.png');
$imageForeground = writeString($imageForeground, $imageFont, $director, 'director');
$imageForeground = writeString($imageForeground, $imageFont, $title, 'title');
$imageForeground = writeString($imageForeground, $imageFont, substr($id, 0, 2) . strtoupper(substr($id, 2, 6)), 'id');
for ($y = 0; $y < 128; $y++) {
for ($x = 0; $x < 80; $x++) {
$rgb = imagecolorat($imagePoster, $x, $y);
$rB = ($rgb >> 16) & 255;
$gB = ($rgb >> 8) & 255;
$bB = $rgb & 255;
$rgb = imagecolorat($imageForeground, $x, $y);
$rF = ($rgb >> 16) & 255;
$gF = ($rgb >> 8) & 255;
$bF = $rgb & 255;
if ($rF + $gF + $bF) {
$r = $rB + $rF / 2;
$g = $gB + $gF / 2;
$b = $bB + $bF / 2;
$color = imagecolorallocate($imagePoster, $r, $g, $b);
imagesetpixel($imagePoster, $x, $y, $color);
}
}
}
header('Content-Type: image/png');
imagepng($imagePoster);
function writeString($imageForeground, $imageFont, $text, $type) {
$wPoster = 80;
$hPoster = 128;
$xMargin = 4;
$wFont = 64;
$hFont = 128;
if ($type == 'director')
$width = 36;
else if ($type == 'title')
$width = 24;
else
$width = 12;
$line = formatString($text, $width);
if ($type == 'director') {
while (count($line) > 3) {
$string = explode(', ', $text);
array_pop($string);
$text = implode(', ', $string);
$line = formatString($text, $width);
}
}
$wLine = $wPoster - 2 * $xMargin;
$hLine = round($wLine / $width * 2);
foreach ($line as $key => $value) {
$len = strlen($value);
// die(($maxlen * $wFont) . '!');
$imageLine = imagecreatetruecolor($width * $wFont, $hFont);
for ($i = 0; $i < $len; $i++) {
$ord = ord(substr($value, $i, 1));
$x = ($ord % 16) * $wFont;
$y = floor($ord / 16) * $hFont;
imagecopy($imageLine, $imageFont, $i * $wFont, 0, $x, $y, $wFont, $hFont);
}
if ($type == 'director')
$y = 63 - (count($line) - $key) * $hLine;
else if ($type == 'title')
$y = 66 + $key * $hLine;
else
$y = 113;
imagecopyresampled($imageForeground, $imageLine, $xMargin, $y, 0, 0, $wLine, $hLine, $width * $wFont, $hFont);
}
return $imageForeground;
}
function formatString($string, $width) {
$lines = count(explode("\n", wordwrap($string, $width)));
while ($width > 0) {
$width--;
if (count(explode("\n", wordwrap($string, $width))) > $lines) {
$width++;
break;
}
}
return explode("\n", wordwrap($string, $width));
}
?>