tags:

views:

76

answers:

2

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?

+2  A: 

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.

Fredrik
lol,yes i expected that, how i can make it return The image data
moustafa
and in a sense, returning 1 (if that is what it does) is to return true.
Fredrik
+2  A: 

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;
?>
Atli
this mean i must make an other url to but the gif header
moustafa
I've added a couple of examples. Maybe that helps? I might be misunderstanding the reason you are doing this tho. I'm assuming you are doing something like converting JPEG and PNG images to GIF?
Atli
+1 for the op_get_clean(), I didn't think of that.
Fredrik