note 27676 deleted from function.imagettftext by vincent

From: Date: Thu, 11 Sep 2003 20:28:46 +0000
Subject: note 27676 deleted from function.imagettftext by vincent
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-56235@lists.php.net to get a copy of this message
Note Submitter: andrew dot chu at ualberta dot net ---- Here's some (not so pretty) code to do TTF text wrapping and alignment (sorry about the long lines...): function ImageTextLength(&$bbox) { $deltax = $bbox[2] - $bbox[0]; $deltay = $bbox[3] - $bbox[1]; return sqrt(pow($deltax,2) + pow($deltay,2)); } function ImageTextHeight(&$bbox) { $deltax = $bbox[6] - $bbox[0]; $deltay = $bbox[7] - $bbox[1]; return sqrt(pow($deltax,2) + pow($deltay,2)); } function ImageTtfTextAlign(&$image, $font_size, $angle_degrees, $x, $y, $colour, $font, $text, $length, $align) { $align = strtoupper($align); if (strcmp($align, "LEFT")) { $angle_radians = $angle_degrees * pi() / 180; $bbox = imagettfbbox($font_size, $angle_degrees, $font, $text); $text_length = ImageTextLength($bbox); if (!strcmp($align, "CENTRE")) $adjustment = ($text_length - $length) / 2; elseif (!strcmp($align, "RIGHT")) $adjustment = $text_length - $length; $x += $adjustment * cos($angle_radians); $y += $adjustment * sin($angle_radians); } imagettftext($image, $font_size, $angle_degrees, $x, $y, $colour, $font, $text); } function ImageAddBbox($bbox1, $bbox2, $angle_degrees) { $angle_radians = $angle_degrees * pi() / 180; $new_length = max(ImageTextLength($bbox1), ImageTextLength($bbox2)); $new_height = ImageTextHeight($bbox1) + ImageTextHeight($bbox2); $bbox[0] = 0; $bbox[1] = 0; $bbox[2] = (int)round($new_length * cos($angle_radians)); // add an extra 10 pixels for spacing between lines $bbox[3] = (int)round($new_length * sin($angle_radians)) + 10; $bbox[6] = (int)round(0 - $new_height * sin($angle_radians)); // add an extra 10 pixels for spacing between lines $bbox[7] = (int)round(0 - $new_height * cos($angle_radians)) + 10; $bbox[4] = $bbox[2] + $bbox[6]; $bbox[5] = $bbox[3] + $bbox[7]; return $bbox; } function ImageTtfBboxWrap($font_size, $angle, $font, $text, $max_length) { if (strlen($text) == 0) { for ($i = 0; $i < 8; $i++) $bbox[$i] = 0; return $bbox; } $subtext = $text; do { $bbox = imagettfbbox($font_size, $angle, $font, $subtext); $text_length = ImageTextLength($bbox); if ($text_length > $max_length) { $text_cutoff = (int)(($max_length / $text_length) * strlen($subtext)); $subtext = substr($subtext, 0, strrpos(substr($subtext, 0, $text_cutoff), " ")); } } while ($text_length > $max_length); $bbox_rest = ImageTtfBboxWrap($font_size, $angle, $font, substr($text, strlen($subtext) + 1), $max_length); return ImageAddBbox($bbox, $bbox_rest, $angle); } function ImageTtfTextWrap(&$image, $font_size, $angle_degrees, $x, $y, $colour, $font, $text, $max_length) { ImageTtfTextWrapAndAlign($image, $font_size, $angle_degrees, $x, $y, $colour, $font, $text, $max_length, "LEFT"); } function ImageTtfTextWrapAndAlign(&$image, $font_size, $angle_degrees, $x, $y, $colour, $font, $text, $max_length, $align) { if (strlen($text) == 0) { for ($i = 0; $i < 8; $i++) $bbox[$i] = 0; return $bbox; } $subtext = $text; do { $bbox = imagettfbbox($font_size, $angle_degrees, $font, $subtext); $text_length = ImageTextLength($bbox); if ($text_length > $max_length) { // The following assumes a fixed width font $text_cutoff = (int)(($max_length / $text_length) * strlen($subtext)); $subtext = substr($subtext, 0, strrpos(substr($subtext, 0, $text_cutoff), " ")); } } while ($text_length > $max_length); $text_height = ImageTextHeight($bbox); $angle_radians = $angle_degrees * pi() / 180; $bbox_rest = ImageTtfTextWrapAndAlign($image, $font_size, $angle_degrees, $x, $y, $colour, $font, substr($text, strlen($subtext) + 1), $max_length, $align); $text_height = ImageTextHeight($bbox_rest); imagettftextalign($image, $font_size, $angle_degrees, $x - $text_height * sin($angle_radians), $y - $text_height * cos($angle_radians), $colour, $font, $subtext, $max_length, $align); return ImageAddBbox($bbox, $bbox_rest, $angle_degrees); }

« previous php.notes (#56235) next »