Search code examples
phpdrawingimagettftext

imagettftext giving weird results


Trying to draw some text on an already existing image in php, but get weird results.

I have this image enter image description here

and I'm trying to draw a number on it with white text, but I get this result enter image description here

Here is the code:

<?php


    $font = "files/fonts/open_sans/OpenSans-Regular-webfont.ttf";
    $image = imagecreatefrompng('images/icons/marker_icon.png'); 
    $white = ImageColorAllocate($image, 255,255,255);
    imagettftext($image, 1, 1, 1, 1, $white, $font, $_GET['count']);
    header("content-type: image/png");
    imagepng($image);
    imagedestroy($image);


?>

First time drawing on an image, so I have no idea what I'm doing wrong.


Solution

  • Figured it out. Since there is a lot of transparency in my image, I had to set imageAlphaBlending to true:

        <?php
                $font = "files/fonts/open_sans/OpenSans-Regular-webfont.ttf";
                $image = imagecreatefrompng('images/icons/marker_icon.png'); 
                $white = ImageColorAllocate($image, 255,255,255);
    
    
                imageAlphaBlending($image, true);
                imageSaveAlpha($image, true);
    
    
                imagettftext($image, 15, 0, 10, 35, $white, $font, $_GET['count']);
                header("content-type: image/png");
                imagepng($image);
                imagedestroy($image);
            ?>