I'm not sure if this is a PHP issue or an GD issue, but after upgrading to PHP 5.3.2, text written at an angle has become top-justified (so "N" and "n" have the same top, but the bottom of the "N" is lower than the bottom of the "n". I've written a kludgy work-around, which writes the text to a non-rotated temporary image, then copies the temporary image, rotated onto the main image. The kludginess is to get around the fact that I can't seem to extract the font info, particularly the distance between the baseline and the very bottom (I've hard-coded it as 30% of the font size)
I hope the bug can be fixed (if it is indeed a bug) or that others can improve this code:
<?php
function imageTextRotated($image, $size, $angle, $x, $y, $inColor, $fontfile, $text, $info=array()) {
$bbox = imageftbbox($size, 0, $fontfile, $text, $info);
$dropdown = $size*0.3;
$xsize = abs($bbox[2] - $bbox[0]);
$ysize = abs($bbox[5] - $bbox[3]);
$tmpImage = imagecreatetruecolor($xsize*1.25, $ysize*1.25); $transparent = imagecolorallocate($tmpImage, 255, 255, 154);
if (!$transparent) {
error_log("Color allocate failed");
}
imagecolortransparent($tmpImage, $transparent);
if (!imagefill($tmpImage, 0, $ysize, $transparent)) {
error_log("Fill failed");
}
$rgb = imagecolorsforindex($image, $inColor);
$color = imagecolorexact($tmpImage, $rgb['red'], $rgb['green'], $rgb['blue']);
if ($color == -1) {
$color = imagecolorallocate($tmpImage, $rgb['red'], $rgb['green'], $rgb['blue']);
if (!$color) {
error_log("Color allocate 2 failed");
}
}
$newbbox = imagefttext($tmpImage, $size, 0, 0, $ysize*1.0, $color, $fontfile, $text, $info);
$tmpImage = imagerotate($tmpImage, $angle, $transparent);
$newWidth = imagesx($tmpImage);
$newHt = imagesy($tmpImage);
imagecopymerge($image, $tmpImage, $x-$newWidth+$dropdown, $y-$newHt, 0, 0, $newWidth, $newHt, 100);
imagedestroy($tmpImage);
?>
-Dave