开发者

Why doesn't this image work?

I wrote:

$im = imagecreatefromstring($im_string);        
return imagegif($im,'a.gif');

The content of variable $im_string I开发者_如何学Go get from a ZIP file. It should work, but it returns '1'.

Why is the reason?


The imagegif function returns a boolean, so a return value of 1 would indicate that it was successful.

If you need to return the actual image data, you need pass the second parameter as well and read the data from the file, or buffer the output of the function.

<?php
ob_start();
$im = imagecreatefromstring($im_string);        
imagegif($im);
$img_data = ob_get_clean();

return $img_data;
?>

Or:

<?php
$im = imagecreatefromstring($im_string);        
imagegif($im, 'a.gif');

$img_data = file_get_contents('a.gif');

return $img_data;
?>


According to the php manual imagegif returns as follows:

Return Values

Returns TRUE on success or FALSE on failure.

What did you expect it to return? The image data?

Edit: To answer the question in the comment. To output the actual data you should either not have the second argument (which sends it to the file a.gif) so that it sends the data straight to the output or you pick up that a.gif and sends the data in there. In both cases you need to remember to send a correct content-type header back to the client as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜