imagecreatefrompng() 制作黑色背景而不是透明?

Posted

技术标签:

【中文标题】imagecreatefrompng() 制作黑色背景而不是透明?【英文标题】:imagecreatefrompng() Makes a black background instead of transparent? 【发布时间】:2011-02-06 09:34:13 【问题描述】:

我使用 php 和 GD 库制作缩略图,但我的代码将 png 透明度变成了纯黑色,有没有改进我的代码的解决方案?

这是我的 php 缩略图制作代码:

function cropImage($nw, $nh, $source, $stype, $dest) 
     $size = getimagesize($source);
     $w = $size[0];
      $h = $size[1];

      switch($stype) 
          case 'gif':
          $simg = imagecreatefromgif($source);
          break;
          case 'jpg':
          $simg = imagecreatefromjpeg($source);
          break;
          case 'png':
          $simg = imagecreatefrompng($source);
          break;
      

      $dimg = imagecreatetruecolor($nw, $nh);
      $wm = $w/$nw;
      $hm = $h/$nh;
      $h_height = $nh/2;
      $w_height = $nw/2;

      if($w> $h) 
          $adjusted_width = $w / $hm;
          $half_width = $adjusted_width / 2;
          $int_width = $half_width - $w_height;
          imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
       elseif(($w <$h) || ($w == $h)) 
          $adjusted_height = $h / $wm;
          $half_height = $adjusted_height / 2;
          $int_height = $half_height - $h_height;

          imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
       else 
          imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
      

      imagejpeg($dimg,$dest,100);

谢谢

【问题讨论】:

【参考方案1】:

imagecreatetruecolor() 之后:

<?php

// ... Before imagecreatetruecolor()

$dimg = imagecreatetruecolor($width_new, $height_new); // png ?: gif

// start changes
switch ($stype) 

    case 'gif':
    case 'png':
        // integer representation of the color black (rgb: 0,0,0)
        $background = imagecolorallocate($dimg , 0, 0, 0);
        // removing the black from the placeholder
        imagecolortransparent($dimg, $background);

        // turning off alpha blending (to ensure alpha channel information
        // is preserved, rather than removed (blending with the rest of the
        // image in the form of black))
        imagealphablending($dimg, false);

        // turning on alpha channel information saving (to ensure the full range
        // of transparency is preserved)
        imagesavealpha($dimg, true);
        break;

    default:
        break;

// end changes

$wm = $w/$nw;
$hm = $h/$nh;

// ...

【讨论】:

澄清一下,在上面的代码中,$simageimagecreatetruecolor 的输出 为我工作了 png,但 gif 仍然有黑色背景;也不明白第二种情况'png'语句的价值。那有必要吗?我尝试了有无,没有发现任何区别。 将近 9 年后,同样的问题仍然存在......上面的解决方案没有帮助......还有其他建议吗?【参考方案2】:

如果正确答案的代码不起作用,试试这个:

    //After imagecreatetruecolor():
$white = imagecolorallocate($dimg, 255, 255, 255); 
imagefill($dimg,0,0,$white); 

【讨论】:

对于某些图像,您需要先用透明填充背景,奇怪,但有效【参考方案3】:

操作顺序很重要。对于 .gif 图像,我发现我需要先复制调整大小的图像,然后将黑色背景指定为透明。对于 PNG,我发现下面的代码调整了图像大小并保留了透明背景。

另外,这段代码对我有用...

$resized_image = imagecreatetruecolor($target_width, $target_height);
switch ( $asset->a_mime_type )

    case 'image/jpeg':
        imagecopyresampled($resized_image, $source, 0, 0, 0, 0, $target_width, $target_height, $asset->a_image_width, $asset->a_image_height);
        $r = imagejpeg($resized_image,$file_name);
        break;
    case 'image/png':
        imagealphablending($resized_image, FALSE);
        imagesavealpha($resized_image, TRUE);
        imagecopyresampled($resized_image, $source, 0, 0, 0, 0, $target_width, $target_height, $asset->a_image_width, $asset->a_image_height);
        $r = @imagepng($resized_image,$file_name);
        break;
    case 'image/gif':
        imagecopyresampled($resized_image, $source, 0, 0, 0, 0, $target_width, $target_height, $asset->a_image_width, $asset->a_image_height);
        $background = imagecolorallocate($resized_image, 0, 0, 0); 
        imagecolortransparent($resized_image, $background);
        $r = @imagegif($resized_image,$file_name);
        break;

【讨论】:

【参考方案4】:

有时如果.jpg图像内部有小错误(你看不到),所有透明像素都会变成黑色。尝试使用:

ini_set('gd.jpeg_ignore_warning', 1);

【讨论】:

【参考方案5】:

只需添加这一行:

imagesavealpha($image, true);

之前

imagepng($image)

【讨论】:

【参考方案6】:

这是我的总测试代码。它对我有用

$imageFileType = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);
$filename = 'test.' . $imageFileType;
move_uploaded_file($_FILES["image"]["tmp_name"], $filename);

$source_image = imagecreatefromjpeg($filename);

$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);

$dest_imagex = 400;
$dest_imagey = 600;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);

imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);

imagesavealpha($dest_image, true);
$trans_colour = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);
imagefill($dest_image, 0, 0, $trans_colour);

imagepng($dest_image,"test1.png",1);

【讨论】:

【参考方案7】:

将 rgb 更改为 255,它将为您提供透明图像,而不是黑色。

<?php
switch ($stype)

    case "png":
        // integer representation of the color black (rgb: 0,0,0)
        $background = imagecolorallocate($simage, 255, 255, 255);
        // removing the black from the placeholder
        imagecolortransparent($simage, $background);

        // turning off alpha blending (to ensure alpha channel information is preserved, rather than removed (blending with the rest of the image in the form of black))
        imagealphablending($simage, false);

        // turning on alpha channel information saving (to ensure the full range of transparency is preserved)
        imagesavealpha($simage, true);

        break;
    case "gif":
        // integer representation of the color black (rgb: 0,0,0)
        $background = imagecolorallocate($simage,  255, 255, 255);
        // removing the black from the placeholder
        imagecolortransparent($simage, $background);

        break;

?>

【讨论】:

【参考方案8】:

以上一些使图像的黑色部分变成白色,而有些则根本不起作用。然而,这对我有用https://github.com/claviska/SimpleImage/issues/28

【讨论】:

以上是关于imagecreatefrompng() 制作黑色背景而不是透明?的主要内容,如果未能解决你的问题,请参考以下文章

PHP 保存 imagecreatefrompng

PHP 致命错误:调用未定义的函数 imagecreatefrompng()

html制作一个黑色箭头-02

html制作一个黑色箭头-02

html制作一个黑色箭头-02

如何使用 OpenGL 制作渐变到黑色的效果?