note 112467 deleted from function.imagettftext by cmb
| From: | cmb@php.net | Date: | Sun, 28 Jul 2019 08:13:05 +0000 |
| Subject: | note 112467 deleted from function.imagettftext by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211728@lists.php.net to get a copy of this message | ||
Note Submitter: lilfo
----
If you need to rotate text around center you can use this function instead of imagettftext_cr() by
pillepop2003 that doesn't mantain the same center of the original image (shifting a bit):
function imagettftext_center_rotate(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)
{
// retrieve boundingbox of the non rotated text
$bbox = imagettfbbox($size, 0, $fontfile, $text);
//calculate the parameters of the line from lowerleft to upperright point
$m1 = ($bbox[5]-$bbox[1])/($bbox[4]-$bbox[0]);
$q1 = $bbox[1]-($bbox[0]*$m1);
//calculate the parameters of the line from upperleft to lowerright point
$m2 = ($bbox[3]-$bbox[7])/($bbox[2]-$bbox[6]);
$q2 = $bbox[7]-($bbox[6]*$m2);
//calculate the rectangle center (intersection of the two lines)
$vx = ($q1-$q2)/($m2-$m1);
$vy = ($m1*($q1-$q2)/($m2-$m1))+$q1;
// retrieve boundingbox of the rotated text
$bbox = imagettfbbox($size, $angle, $fontfile, $text);
//calculate the parameters of the line from lowerleft to upperright point
$m1 = ($bbox[5]-$bbox[1])/($bbox[4]-$bbox[0]);
$q1 = $bbox[1]-($bbox[0]*$m1);
//calculate the parameters of the line from upperleft to lowerright point
$m2 = ($bbox[3]-$bbox[7])/($bbox[2]-$bbox[6]);
$q2 = $bbox[7]-($bbox[6]*$m2);
//calculate the rectangle center (intersection of the two lines)
$nx = ($q1-$q2)/($m2-$m1);
$ny = ($m1*($q1-$q2)/($m2-$m1))+$q1;
//calculate the x and y shifting needed
$dx = $nx - $vx;
$dy = $ny - $vy;
// new pivotpoint
$px = $x-$dx;
$py = $y-$dy;
//output
return imagettftext($im, $size, $angle, $px, $py, $color, $fontfile, $text);
}