.8); $fontSize = 20; $maxWidth = 120; $maxHeight = 35; if($book) $book = $book . '!'; //wrapper for imageftbbox that only returns the width function textWidth($text) { global $fontSize,$extraInfo; $box = imageftbbox($fontSize,0,'/Humor-Sans.ttf',$text,$extraInfo); return $box[4]; } //wrapper for imageftbbox that only returns the height function textHeight($text) { global $fontSize,$extraInfo; $box = imageftbbox($fontSize,0,'/Humor-Sans.ttf',$text,$extraInfo); return $box[1]; } //insert linebreaks or shrink text to fit in the horizontal space while(textWidth($book)>$maxWidth) { $lastSpace = strrpos($book,' '); if ($lastSpace === false) { //no spaces left $fontSize--; } else {//replace the last space with a linebreak $book[$lastSpace] = "\n"; } } //shrink text to fit in the vertical space //reduces line spacing if possible, then collapses lines, then decreases font while(textHeight($book)>$maxHeight) { $spacing = $extraInfo["linespacing"]; if ($spacing > .6) {//too much overlap after this. Values <1 are only okay because this is an all-caps font. $extraInfo["linespacing"] = $spacing - .1; } else { $foundUnneededNewline = false; $lines = explode("\n",$book); for($i=0;($i+1) .8); $fontSize = 32; $maxWidth = 55; //insert linebreaks or shrink text to fit in the horizontal space while(textWidth($comment)>$maxWidth) { $lastSpace = strrpos($comment,' '); if ($lastSpace === false) { //no spaces left $fontSize--; } else {//replace the last space with a linebreak $comment[$lastSpace] = "\n"; } } $maxHeight = min(100,(substr_count($comment,"\n")+1)*32); //100 pixels or 32 per line $width = textWidth($comment)+2; $height = textHeight($comment) + (1.5*$fontSize); //write to a dummy image $shortImage = imagecreate($width,$height); imagecolorallocate($shortImage,0xB2,0xB2,0xB2); $textColor = imagecolorallocate($shortImage, 0x00, 0x00, 0x00); imagefttext($shortImage,$fontSize,0,2,$fontSize,$textColor,'/Humor-Sans.ttf',$comment,$extraInfo); //stretch the dummy image, mostly vertically, then apply filters $stretchedImage = imagecreate($maxWidth,$maxHeight); imagecopyresized($stretchedImage,$shortImage,0,0,0,0,$maxWidth,$maxHeight,$width,$height); imagefilter($stretchedImage,IMG_FILTER_GAUSSIAN_BLUR); //paste it onto the comic imagecopy($img,$stretchedImage,436,56,0,0,$maxWidth,$maxHeight); // Output image to the browser header('Content-Type: image/png'); imagepng($img); imagedestroy($img); ?>