【问题标题】:GIF transparent background turned to blackGIF透明背景变成黑色
【发布时间】:2013-11-25 11:19:50
【问题描述】:

我有一个调整上传图片大小的脚本。它适用于 PNG 和 JPG,但对于 GIF,它会将调整大小的 GIF 的透明度呈现为黑色。

    $src = imagecreatefromgif($file);
    $dst = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    imagegif($dst, $file);

【问题讨论】:

  • 只要搜索"php imagecopyresampled transparent gif"。很多结果,有些甚至在手册页上

标签: php gd gif


【解决方案1】:

我假设这是一个网页,如果是这样,你可以输出具有正确属性的图像标签吗?

echo "<img src='$file' width='$newWidth' height='$newHeight' />";

【讨论】:

    【解决方案2】:

    您仍然需要在新图像中设置透明度。以下摘自php docs

    <?php
    // Create a 55x30 image
    $im = imagecreatetruecolor(55, 30);
    $red = imagecolorallocate($im, 255, 0, 0);
    $black = imagecolorallocate($im, 0, 0, 0);
    
    // Make the background transparent
    imagecolortransparent($im, $black);
    // ^^ This is the command you are missing.
    
    // Draw a red rectangle
    imagefilledrectangle($im, 4, 4, 50, 25, $red);
    
    // Save the image
    imagepng($im, './imagecolortransparent.png');
    imagedestroy($im);
    ?>
    

    在您的情况下,您希望使透明度与原始透明度具有相同的颜色 - 我总是把它做成丑陋的紫色或其他东西,a)在我的图像处理软件中像拇指酸痛一样突出,其次,有一个几乎不可能错误地包含在图像中的 RGB 键。

    【讨论】:

      【解决方案3】:

      来自手册页http://us3.php.net/manual/en/function.imagecopyresampled.php#104028

      imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
      imagealphablending($new, false);
      imagesavealpha($new, true);
      

      被一张海报用来保持透明度。

      这里有一个提示...请务必查看 php.net 上的用户 cmets,因为它们通常非常有助于理解函数的细微差别并提供处理常见任务的提示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-26
        • 1970-01-01
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 2016-02-22
        相关资源
        最近更新 更多