【问题标题】:Resized GIF images change to black调整大小的 GIF 图像变为黑色
【发布时间】:2013-12-24 13:01:59
【问题描述】:

我有一个调整上传图片大小的脚本。我添加了一些说明以将 GIF 中的任何透明度转换为白色。它通常可以正常工作,但在某些情况下,调整大小的 GIF 完全是黑色的。 (请注意,我将这些图像用于 PDF,所以我不能使用 CSS 来处理这个......)。无论如何,我是 PHP 图像处理的新手,所以我不知道如何解决这个问题。

这里有两个示例 GIF。第一个工作正常,而第二个更改为黑色。

这是相关的sn-p:

elseif ($fileType == 'gif') {
    $src = imagecreatefromgif($file);
    $dst = imagecreatetruecolor($newWidth, $newHeight);
    imagecolortransparent($dst, imagecolorallocatealpha($dst, 0, 0, 0, 127));
    imagealphablending($dst, false);
    imagesavealpha($dst, true);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    imagegif($dst, $file);
} 

【问题讨论】:

  • 这些 gif 都没有透明度。
  • 第二张图片不是gif。

标签: php gd gif


【解决方案1】:

首先将您的第二张图片转换为 gif 格式。为此,请使用任何图像处理工具(gimp/photoshop)或在代码 sn-p 下方添加前缀:

// Load the JPEG
$jpeg = imagecreatefromjpeg($file);
// Save the image as a GIF
imagegif($jpeg, $file);

在给定的代码 sn-p 中,您正在使用 imagecolorallocatealpha($dst, 0, 0, 0, 127) 函数,该函数将在您的图像中添加完全透明度。而不是这个,尝试使用 imagecolorallocate($dst, 0, 0, 0) 函数或不要在 imagecolortransparent() 函数中使用颜色参数。

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 2014-06-03
    • 2012-07-14
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多