1

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

4

1 回答 1

0

您的问题不是很清楚,我想您想知道为什么您的脚本适用于 jpg 和 png 文件,但echo "some thing went wrong.."适用于 gif 文件。

如果您imagecreatefromstring的 gif 文件唯一失败,这可能意味着该文件的内容是 php 支持的图像格式。

这可能有不同的原因:

  1. 您的 gif 文件内容已损坏或未正确读取
  2. 您的文件不是真正的 gif 图像(可能是其他图像格式,例如重命名为 .gif 的 bmp)
  3. 它是一个有效的 gif 文件,但包含超过 1 个图像帧(例如动画 gif 文件)
于 2013-10-10T10:37:11.670 回答