2

I have a simple service that generates a PNG image of any text, using the parameters passed via a URL. One of the parameters is the text itself, the rest are things like 'font', 'color', 'font weight' etc.

An example of such a URL is: http://picselbocs.com/projects/cakemyface/text.php?params=Verdana%7C18%7Cbold%7Cnormal%7Ccenter%7C%23cc0000%7Cunderline&text=Hello%20world!

which generates the following PNG:

original

In another script, I'm using cURL to retrieve such a resource generated by this service, which I then convert to an image using imagecreatefromstring(), because I need to operate on it - things like rotating and scaling -, then to merge it with some other images. For this I use the following code:

function getImage($url){
            $ch = curl_init ($url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
            $resource = curl_exec($ch);
            curl_close ($ch);

            return $resource;
    }


    $url = "http://picselbocs.com/projects/cakemyface/text.php?params=Verdana%7C18%7Cbold%7Cnormal%7Ccenter%7C%23cc0000%7Cunderline&text=Hello%20world!";

    $string = getImage($url);
    $image = imagecreatefromstring($string);

    // Send the image to the client
    header("Content-type: image/png");
    header("Content-disposition: inline; filename=mytext.png");
    imagepng($image);
    imagedestroy($image);

This same code is available here, where you can see the output. The problem is that the code above outputs some strange PNG, where all the letters are filled rectangles, like in the example bellow:

sample result

Why does this happen and how can I solve it?

One other curious thing is that if I replace a URL to a text-image with, for instance, a link to a QR code generated with the Google Charts tool (e.g. QR code), the result is what it's supposed to be...

4
  • 1
    When I follow the example url (the first one) it's just a 10x10px white image? Commented Nov 24, 2012 at 9:52
  • I corrected the link, please try again. Commented Nov 24, 2012 at 9:57
  • 2
    Might it be a transparency issue? Try playing around with imagecolortransparent in text.php Commented Nov 24, 2012 at 10:08
  • @gogowitsch My thoughts exactly Commented Nov 24, 2012 at 10:10

1 Answer 1

3

After a little playing about I discovered the issue and the solution!

$image = imagecreatefromstring(file_get_contents('http://picselbocs.com/projects/cakemyface/text.php?params=Verdana|18|bold|normal|center|%23cc0000|underline&text=Hello%20world!'));
imagesavealpha($image, TRUE); // this is the fix
header("Content-Type: image/png");
imagepng($image);

The way I create my image is just a quick way without using curl, it seems the image needs it's alpha channels saving.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.