update page now
Longhorn PHP 2026 - Call For Papers

Voting

: max(eight, eight)?
(Example: nine)

The Note You're Voting On

php@davehirschD0TK0MM
15 years ago
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 that draws rotated text by creating a temporary image and rotating it, since rotated text appears to be busted
    function imageTextRotated($image, $size, $angle, $x, $y, $inColor, $fontfile, $text, $info=array()) {
//        Force some demo text that contains risers and descenders:
//        $text = "Nlfbacejygq!";

        $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);        // need the extra space to accommodate risers and descenders
        $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);
        
//        Highlight the desired starting point (baseline) with a green dot:
//        $green = imagecolorallocate($image, 0, 251, 0);
//        imagefilledellipse($image, $x, $y, 10, 10, $green);
        imagedestroy($tmpImage);
?>

-Dave

<< Back to user notes page

To Top