简体   繁体   English

使用 PHP GD `imagecreatefromstring` 函数创建图像的问题

[英]Issue with creating an image with PHP GD `imagecreatefromstring` function

I have a simple service that generates a PNG image of any text, using the parameters passed via a URL.我有一个简单的服务,它使用通过 URL 传递的参数生成任何文本的 PNG 图像。 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!此类 URL 的示例是: http : //picselbocs.com/projects/cakemyface/text.php?params=Verdana%7C18%7Cbold%7Cnormal%7Ccenter%7C%23cc0000%7Cunderline&text=Hello%20world!

which generates the following PNG:生成以下PNG:

原来的

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.在另一个脚本中,我使用 cURL 来检索此服务生成的此类资源,然后我使用imagecreatefromstring()将其转换为图像,因为我需要对其进行操作 - 诸如旋转和缩放之类的 - 然后合并它与其他一些图像。 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:问题是上面的代码输出了一些奇怪的 PNG,其中所有字母都是填充的矩形,如下例所示:

样本结果

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 (eg QR code ), the result is what it's supposed to be...另一件奇怪的事情是,如果我将文本图像的 URL 替换为,例如,使用 Google Charts 工具生成的 QR 码的链接(例如QR 码),结果就是它应该是......

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.我创建图像的方式只是一种不使用 curl 的快速方法,看来图像需要保存它的 alpha 通道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM