【问题标题】:gif image is not created by imagecreatefromstring() and imagegif()gif 图像不是由 imagecreatefromstring() 和 imagegif() 创建的
【发布时间】:2013-10-10 09:13:55
【问题描述】:

我正在开发一个 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 文件的情况下工作得很好。

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

【问题讨论】:

  • 你想给出哪些效果?在哪里?
  • “问题是效果没有出现”是什么意思?
  • 我的意思是,在gif文件中,当我运行这段代码时,总是有一些动画,它创建了相同类型的图像文件,但是在原始图像文件中的动画效果,在新的不可用创建的文件..为了更清楚你打开这两个文件,说原始文件和新创建的文件..你会得到两者之间的区别。
  • 请检查两个文件的代码。我的意思是比较新旧文件的代码...

标签: php image file file-io


【解决方案1】:

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

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

这可能有不同的原因:

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

【讨论】:

  • 在php手册(in2.php.net/function.imagecreatefromstring)中指定imagecreatefromstring()函数用于jpg、jpeg、gif、png等文件
  • 我知道,但扩展名为 .gif 的文件也可能包含不同的数据。人们经常重命名 yust 文件而没有真正转换它们,因此可能是 .bmp 文件被重命名为 .gif。在这种情况下 imagecreatefromstring 将不支持这一点。而且我现在不知道 imagecreatefromstring 中的多帧 gif 文件会发生什么。我应该知道 'imagegif()' 只支持单帧 gif 文件。
  • 嗨,Radon,我从互联网上下载了那个文件,然后测试了这个 scrupt。但感谢您分享此信息。
猜你喜欢
  • 2011-10-21
  • 2015-11-21
  • 2015-02-06
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 2013-06-05
  • 2014-06-18
  • 2010-10-24
相关资源
最近更新 更多