gif图像不是由imagecreatefromstring()和imagegif()创建的

时间:2013-10-10 09:13:56

标签: php image file file-io

我正在开发一个api,它通过移动应用程序以base64编码形式加入图像文件。

现在apis任务是创建该图像文件并存储在服务器中。

下面是代码。

<?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..";
}
?>

图像在所需位置正确存储,问题是效果未到来。

imagejpeg(),imagepng()这样的函数在jpg,joeg,png文件的情况下工作得非常好。

不知道哪个地方出了问题?

1 个答案:

答案 0 :(得分:0)

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

如果您的imagecreatefromstring仅针对您的gif文件失败,则可能意味着此文件的内容是php支持的图像格式。

这可能有不同的原因:

  1. 您的gif文件内容已损坏或无法正确读取
  2. 你的文件不是真正的gif图像(也许是其他图像格式,例如bmp,它被重命名为.gif)
  3. 这是一个有效的gif文件,但包含多个图像框架(例如动画gif文件)