ConFoo Montreal 2026: Call for Papers

Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

pillepop2003 at nospam dot yahoo dot de
20 years ago
Hey guys,

check this function if you want to rotate the text around its center and not its "lower left" pivot-point:

<?php
// Put center-rotated ttf-text into image
// Same signature as imagettftext();
function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)
{
// retrieve boundingbox
$bbox = imagettfbbox($size, $angle, $fontfile, $text);

// calculate deviation
$dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0; // deviation left-right
$dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0; // deviation top-bottom

// new pivotpoint
$px = $x-$dx;
$py = $y-$dy;

return
imagettftext($im, $size, $angle, $px, $py, $color, $fontfile, $text);
}

?>

Big up
Phil

<< Back to user notes page

To Top