note 27850 modified in function.imagettfbbox by didou

From: Date: Fri, 15 Aug 2003 22:13:46 +0000
Subject: note 27850 modified in function.imagettfbbox by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54272@lists.php.net to get a copy of this message
It took me a while to figure out what was going on with the bounding box. The hardest part is basically getting the width and height for your entire string rendered in a given font: $bbox = Imagettfbbox($fontsize,0,$font,$string) ; $width = ( ($bbox[0] > 0 && $bbox[2] > 0) || ($bbox[0] < 0 && $bbox[2] < 0) ) ? abs($bbox[2] - $bbox[0]) : ( abs($bbox[2]) + abs($bbox[0]) + 1 ); $height = ( ($bbox[1] > 0 && $bbox[5] > 0) || ($bbox[1] < 0 && $bbox[5] < 0) ) ? abs($bbox[1] - $bbox[5]) : ( abs($bbox[1]) + abs($bbox[5]) + 1 ); The trick is that some text will render totally above the horizontal axis, some will render totally bellow it and some will render on both sides of it (same with the vertical axis). The two functions listed above will give you the width and height in every possibly case (given that your angle is set to 0). After that, finding the lower left corner to draw your text to an image using the ImageTTFText() is as simple as: $X = (imagesx($image)-$width)/2; $Y = ((imagesy($image)-$height)/2)+$height; --was-- It took me a while to figure out what was going on with the bounding box. The hardest part is basically getting the width and height for your entire string rendered in a given font: $bbox = Imagettfbbox($fontsize,0,$font,$string) ; $width = ( ($bbox[0] > 0 && $bbox[2] > 0) || ($bbox[0] < 0 && $bbox[2] < 0) ) ? abs($bbox[2] - $bbox[0]) : ( abs($bbox[2]) + abs($bbox[0]) ); $height = ( ($bbox[1] > 0 && $bbox[5] > 0) || ($bbox[1] < 0 && $bbox[5] < 0) ) ? abs($bbox[1] - $bbox[5]) : ( abs($bbox[1]) + abs($bbox[5]) ); The trick is that some text will render totally above the horizontal axis, some will render totally bellow it and some will render on both sides of it (same with the vertical axis). The two functions listed above will give you the width and height in every possibly case (given that your angle is set to 0). After that, finding the lower left corner to draw your text to an image using the ImageTTFText() is as simple as: $X = (imagesx($image)-$width)/2; $Y = ((imagesy($image)-$height)/2)+$height; http://www.php.net/manual/en/function.imagettfbbox.php

« previous php.notes (#54272) next »