note 39720 deleted from function.imagettfbbox by cmb

From: Date: Sun, 07 Jul 2019 08:09:07 +0000
Subject: note 39720 deleted from function.imagettfbbox by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211511@lists.php.net to get a copy of this message
Note Submitter: jtopland at hive dot no ---- Finally managed to make a fixed version of imagettfbbox(). All angles returns correct values. Except that imagettftext() returns different trackings (space between each character) when rotating. <?php // Set some test variables $font = "d://www//tahoma.ttf"; $text = "Finally, I can center rotated text!"; $size = 20; $angle = 20; // Create an image and fill the background with lightgray $image = imagecreatetruecolor(500, 400); imagefill($image, 0, 0, hexdec("dddddd")); // Make a cross to make it easier to analyze imageline($image, 0, 0, imagesx($image), imagesy($image), hexdec("000000")); imageline($image, imagesx($image), 0, 0, imagesy($image), hexdec("000000")); // Run a fixed version of imagettfbbox() $bbox = imagettfbbox_fixed($size, $angle, $font, $text); // Make some text and center the text on the image. // imagettftext() pivot is on lower left imagettftext($image, $size, $angle, imagesx($image) / 2 - $bbox['width'] / 2, imagesy($image) / 2 + $bbox['height'] / 2, hexdec("0000ff"), $font, $text); // Show the image imagepng($image); function imagettfbbox_fixed($size, $angle, $font, $text) { // Get the boundingbox from imagettfbbox(), which is correct when angle is 0 $bbox = imagettfbbox($size, 0, $font, $text); // Rotate the boundingbox $angle = pi() * 2 - $angle * pi() * 2 / 360; for ($i=0; $i<4; $i++) { $x = $bbox[$i * 2]; $y = $bbox[$i * 2 + 1]; $bbox[$i * 2] = cos($angle) * $x - sin($angle) * $y; $bbox[$i * 2 + 1] = sin($angle) * $x + cos($angle) * $y; } // Variables which tells the correct width and height $bbox['width'] = $bbox[0] + $bbox[4]; $bbox['height'] = $bbox[1] - $bbox[5]; return $bbox; } ?>

« previous php.notes (#211511) next »