note 104238 deleted from function.imagettftext by danbrown
| From: | danbrown@php.net | Date: | Thu, 02 Jun 2011 21:48:13 +0000 |
| Subject: | note 104238 deleted from function.imagettftext by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180210@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
If you would like to create an image that perfectly wraps a true type string in width and height
then use this code below. I have tested this on a few fonts and it seems to work!
This will also take into consideration the baseline of the text for rendering of seperate strings
with different characters.
<?php
//vars
$font = 'font.ttf';
$text = 'hello world!';
$size = 40;//pt
$color = '#ff0000';
//over compensate dimensions!
$cropPadding = 6;
$fontRange = 'xgypqXi';
$bounds = imagettfbbox($size,0,$font,$fontRange);
$height = abs($bounds[1]-$bounds[5])+$cropPadding;
$bounds = imagettfbbox($size,0,$font,$text);
$width = abs($bounds[0]-$bounds[2])+$cropPadding+$cropPadding;
$x = ($bounds[0] * -1)+$cropPadding;
$y = abs($bounds[7])-1;
//create transparent image
$image = imagecreatetruecolor($width,$height);
imagesavealpha($image, true);
imagealphablending($image, false);
$background = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefilledrectangle($image, 0, 0, $width, $height, $background);
imagealphablending($image, true);
//make color
$rgb = str_split(ltrim($color,'#'),2);
$color = imagecolorallocatealpha($image,hexdec($rgb[0]),hexdec($rgb[1]),hexdec($rgb[2]),0);
//render text to image
imagettftext($image,$size,0,$x,$y,$color,$font,$text.' '.$fontRange);
//calculate crop
$trim_bottom = 0;
$trim_left = 0;
$trim_right = 0;
//bottom
for($trim_y = $height-1;$trim_y >= 0; $trim_y--) {
for($trim_x = 0; $trim_x < $width; $trim_x++) {
$alpha = (imagecolorat($image, $trim_x, $trim_y) >> 24) & 0xFF;
if($alpha != 127) { break 2; }
}
$trim_bottom++;
}
//left
for($trim_x = 0;$trim_x < $width; $trim_x++) {
for($trim_y = 0; $trim_y < $height; $trim_y++) {
$alpha = (imagecolorat($image, $trim_x, $trim_y) >> 24) & 0xFF;
if($alpha != 127) { break 2; }
}
$trim_left++;
}
//right
for($trim_x = $width-1;$trim_x >= 0; $trim_x--) {
for($trim_y = 0; $trim_y < $height; $trim_y++) {
$alpha = (imagecolorat($image, $trim_x, $trim_y) >> 24) & 0xFF;
if($alpha != 127) { break 2; }
}
$trim_right++;
}
//do crop
if ($trim_left || $trim_right || $trim_bottom) {
//get crop dimensions
$x = $trim_left;
$y = 0;
$newWidth = $width - $trim_left - $trim_right;
$newHeight = $height - $trim_bottom;
//create new image
$newImage = imagecreatetruecolor($newWidth,$newHeight);
imagesavealpha($newImage,true);
imagealphablending($newImage, false);
$background = imagecolorallocatealpha($newImage,255,255,255,127);
imagefilledrectangle($newImage,0,0,$newWidth,$newHeight,$background);
imagealphablending($newImage,true);
//crop old image
imagecopyresampled($newImage,$image,0,0,$x,$y,$width,$height,$width,$height);
//swap the new image
imagedestroy($image);
$image = $newImage;
}
//do something with image
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
header( "Cache-Control: no-store, no-cache, must-revalidate" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header( "Pragma: no-cache" );
header( "Content-Type: image/png" );
imagepng($image);
//free image
imagedestroy($image);
?>