【问题标题】:Is it possible to create an PNG with full alpha channel by using: imagecolortransparent()是否可以使用以下方法创建具有完整 Alpha 通道的 PNG:imagecolortransparent()
【发布时间】:2012-06-10 08:31:52
【问题描述】:

我正在寻找一种通过使用其 aplha 通道在图像中创建透明区域的方法。

我就是这样找到的:

定义一种颜色并将其设置为透明。

imagealphablending($img, false);   // has to be false for imagecolortransparent
imagesavealpha($img, false);       // false = single color transparency

$mask_color = imagecolorallocatealpha($img, 0, 255, 0, 127);
imagecolortransparent( $img, $mask_color );

但结果是单色透明。如果我尝试将其与具有 alpha 通道的图像合并,则蒙版颜色会再次可见。

这发生在我再次启用 imagesavealpha() 时,这是合并过程所需的。

顺便说一句:我通过将源图像与蒙版图像合并来创建 $img

$new_img = imagecreatetruecolor( 150, 100 );
$new_white = imagecolorallocatealpha( $new_img, 255, 255, 255, 0 );
imagefill( $new_img, 0, 0, $new_white );

imagealphablending($new_img, false);   // has to be false for full alpha
imagesavealpha($new_img, true);       // true = full alpha channel

imagecopyresampled( $new_img, $img       ,0, 0, 0, 0, 150, 100, 150, 100);
imagecopyresampled( $new_img, $alpha_img ,0, 0, 0, 0, 150, 100, 150, 100);

// save preview
imagepng( $new_img, $image_path . $preview_dir_name . $new_file_name );

$img 中的透明区域现在再次可见。

那么,有没有办法通过使用完整的 Alpha 通道使特定的颜色区域透明?

【问题讨论】:

    标签: php png transparency alpha gdlib


    【解决方案1】:

    我找到了办法:

    // get source image
    $img = imagecreatefromjpeg( $image_path . $image_file );
    
    // save transparency in alpha channel
    imagealphablending($img, false);
    imagesavealpha($img, true);
    
    // define index for transparent color
    $transparent = imagecolorallocatealpha( $preview_temp, 255, 255, 255, 127 );
    
    // replace pixel information
    imagesetpixel( $img, $x, $y, $transparent );
    

    使用imagesetpixel(),我可以替换一个像素并通过使用它的alpha通道在给定图片中获得透明区域。

    它对我有用,也许我稍后会发布完整的代码。

    但是您知道其他解决方案吗?

    【讨论】:

      猜你喜欢
      • 2011-02-08
      • 2011-06-13
      • 2013-10-17
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 2012-01-31
      • 2017-04-18
      • 1970-01-01
      相关资源
      最近更新 更多