Mostafa Elkady
Mostafa Elkady

Reputation: 5791

Why doesn't this image work?

I wrote:

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

The content of variable $im_string I get from a ZIP file. It should work, but it returns '1'.

Why is the reason?

Upvotes: 0

Views: 120

Answers (2)

Atli
Atli

Reputation: 7930

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;
?>

Upvotes: 2

Fredrik
Fredrik

Reputation: 5839

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.

Upvotes: 2

Related Questions