【问题标题】:imagecreatefromgif() returning is not a valid GIF file inimagecreatefromgif() 返回的不是有效的 GIF 文件
【发布时间】:2013-12-31 00:11:09
【问题描述】:

我正在尝试从外部站点上的 .php 文件中提取图像,该文件是 gif(一帧)。我想最后将这些图像放入 gif 中,但目前这不是问题。每次我尝试将 gif 保存为 .pngs 时,它都会返回错误“不是有效的 GIF 文件”

include('GIFEncoder.class.php');
$user = $_GET['user'];
$url = 'http://link.com/image.php?username=' . $user . '';
$username = explode('=', $url);
$username = $username[1];
$img = 'image_cache/' . $username .'.gif';
file_put_contents($img, file_get_contents($url));
$i = imagecreatefromgif('image_cache/' . $username .  '.gif');
imagepng($i, "image_cache/". $username .".png");

我得到的确切错误

警告:imagecreatefromgif() [function.imagecreatefromgif]: 'image_cache/Lexo.gif' 不是第 9 行 z/projects/gif/index.php 中的有效 GIF 文件

【问题讨论】:

  • 错误不是告诉你问题是什么吗?
  • 外部gif的url是什么?
  • 为什么你认为它会在你的脚本中返回一个 GIF?
  • gif 在那里,我可以在 FTP 上看到它,它可以工作。外部 php im 抓取 gif 链接是 game.habboon.com/habbo-imaging/… - gif 在那里,但它说它不是“有效的”。

标签: php image file-get-contents


【解决方案1】:

图片实际上是一个png。在文件上运行exif_imagetype,你会看到类型是png。 gif 后缀是错误的。要执行您想要执行的操作,只需将其命名为 .png 后缀即可。

【讨论】:

    【解决方案2】:

    您的代码应该看起来更像:

    if(isset($_GET['user'])){
      header('Content-type:image/png');
      $user = $_GET['user']; $img = imagecreatefrompng("image_cache/$user.png");
      imagepng($img); imagedestroy($img);
    }
    

    【讨论】:

    • 看起来OP的意图是保存图像,而不是输出给客户端。
    【解决方案3】:

    尝试使用此代码解决 somefilename is not a valid GIF file in..imagecreatefromgif() 的问题

    $createFrom = imagecreatefromstring(file_get_contents($filepath));
    imagecopyresampled();
    imagegif();
    

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多