简体   繁体   中英

gif image is not created by imagecreatefromstring() and imagegif()

I am developing an api that accepets image file in base64 encoded form from the mobile app.

Now the apis task to create that image file and store in server.

below is the code.

<?php 

$file="mygif_image.gif";
$handle = fopen($file, "r");
if(!filesize($file)){
    echo "corrupted file.."; die;
}

$image_contents = fread($handle, filesize($file));
fclose($handle);

$encoded_data = base64_encode($image_contents);
/* assume that this $encoded_data I am getting in api as request */

/* this will be the server side code, where encoded data is accepted and 
   I required to store image in server*/

$image_binary_content = base64_decode($encoded_data);

$im = imagecreatefromstring($image_binary_content);

if ($im !== false) {
header('Content-Type: image/gif');
imagegif($im,"mygif_image2.gif");
imagedestroy($im);
}else{
    echo "some thing went wrong..";
}
?>

The image is getting stored properly at required place, issue is effects are not comming.

imagejpeg(), imagepng() such functions work very nice in case of jpg,joeg,png files.

Don't know which is the place, where something is going wrong ??

Your question is not realy clear, I think you like to know why you script is working for jpg and png files, but dos an echo "some thing went wrong.." for gif files.

If your imagecreatefromstring only failes for your gif files, this could mean that the content of this files is a image format that php supports.

this can have different reasons:

  1. Your gif File content is corrupted or was not read correctly
  2. Your file is no real gif image (Maybe its an other imageformat eg bmp that was renamed to .gif)
  3. It is a valid gif file, but contains more than 1 image frame (eg animated gif files)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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