【问题标题】:PHP 5.5 imagecropauto() lost transparencyPHP 5.5 imagecropauto() 失去了透明度
【发布时间】:2018-09-23 12:13:27
【问题描述】:

我需要帮助。 我尝试使用 imagecropauto(),但 PNG 仍然是黑色背景。代码如下:

$im = imagecreatefrompng($imgPath);

imagealphablending($im, false);
imagesavealpha($im, true);

// if imagepng($im...) called here, original PNG is saved with transparency

// if I use IMG_CROP_TRANSPARENT - crop doesn't works
// IMG_CROP_SIDES working how I expect
$cropped = imagecropauto($im, IMG_CROP_SIDES);

if ($cropped !== false) {
    // destroy old image
    imagedestroy($im);

    imagealphablending($cropped, false);
    imagesavealpha($cropped, true);

    // save cropped image with black background
    imagepng($cropped, $imgPath);
    imagedestroy($cropped);
}

有什么建议吗?

编辑: PNG 图像通过点上的 alpha 通道是透明的,因此某些点的不透明度低于其他点。如果我使用 imagealphablending - true(默认),我只能将一种颜色设置为透明,结果在图片周围有黑线。

默认值:

使用图像混合 - 真实和黑色是透明的:

【问题讨论】:

    标签: php png transparency


    【解决方案1】:

    我在 Ubuntu 上也有这个问题,在 windows 上它工作得很好。 我的代码和你的完全一样。 The issue 已确认,尚未修复。

    Here is a link related to this issue

    就我而言,我可以使用白色背景,方法如下:

      $width = imagesx($im);
      $height = imagesy($im);
      $new = imagecreatetruecolor($width, $height);
      $white = imagecolorallocate($new, 255, 255, 255);
      //now the image is purely white
      imagefill($new, 0, 0, $white);
      //place the transparent image onto this white background image
      imagecopyresampled($new, $im, 0, 0, 0, 0, $width, $height, $width, $height);
    
      //crop the new image
      $white = imagecolorat($new, 1, $height - 1);
      $cropped = imagecropauto($new, IMG_CROP_THRESHOLD, 0.5, $white);
    
      if ($cropped) {
        imagepng($cropped, $output_file);
        imagedestroy($cropped);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      相关资源
      最近更新 更多