在 PHP 中合并透明图像


Merge transparent images in PHP

我正在尝试将部分透明的PNG分层到另一个不透明的PNG之上。

在这个网站和网络上有很多关于如何做到这一点的例子,但是对于我尝试的每个版本,我似乎都无法保持前景图像的透明度。

目前代码如下所示:

 $image = imagecreatefrompng($_GET['fg']);
 $frame = imagecreatefrompng($_GET['bg']);
 imagealphablending($frame,true);
 imagecopymerge($image, $frame, 0, 0, 0, 0, 0, 100, 100);
 # Save the image to a file
 $output_file = 'preview-' . time() . '.png';
 imagepng( $image, $_SERVER['DOCUMENT_ROOT'] . '/share/' . $output_file );

这将生成由前景图像组成的图像,其中透明部分为白色(或黑色)。

我也尝试过这个,如图像缩略图生成器 TimThumb 所示,它产生相同的输出:

$canvas= imagecreatefrompng($_GET['bg']);
$overlay_gd_image = imagecreatefrompng( $_GET['fg'] );
$overlay_width = imagesx( $overlay_gd_image );
$overlay_height = imagesy( $overlay_gd_image );
imagealphablending($canvas, true );
imagecopy( $canvas, $overlay_gd_image, 0, 0, 0, 0, $overlay_width, $overlay_height);
imagealphablending($canvas, false );
imagesavealpha($canvas , true); 
imagepng($canvas, 'new.png');

我已经没有事情可以尝试了,如果有人能阐明这个问题,我将不胜感激。

当涉及到透明图像时,本机图像复制合并并不能很好地工作。如果你想将两个PNG合并在一起,而不是让alpha通道搞砸,在手册页上找到的这个功能对我来说就像一个魅力,实际上保持了透明度。我一直在不停地将 PNG 与此功能合并,并且没有遇到问题,所以我认为它可以解决您的问题。(发布者 "rodrigo dot polo at gmail dot com",所以所有功劳都归于他/她。

<?php 
/** 
 * PNG ALPHA CHANNEL SUPPORT for imagecopymerge(); 
 * This is a function like imagecopymerge but it handle alpha channel well!!! 
 **/ 
// A fix to get a function like imagecopymerge WITH ALPHA SUPPORT 
// Main script by aiden dot mail at freemail dot hu 
// Transformed to imagecopymerge_alpha() by rodrigo dot polo at gmail dot com 
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    if(!isset($pct)){ 
        return false; 
    } 
    $pct /= 100; 
    // Get image width and height 
    $w = imagesx( $src_im ); 
    $h = imagesy( $src_im ); 
    // Turn alpha blending off 
    imagealphablending( $src_im, false ); 
    // Find the most opaque pixel in the image (the one with the smallest alpha value) 
    $minalpha = 127; 
    for( $x = 0; $x < $w; $x++ ) 
    for( $y = 0; $y < $h; $y++ ){ 
        $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF; 
        if( $alpha < $minalpha ){ 
            $minalpha = $alpha; 
        } 
    } 
    //loop through image pixels and modify alpha for each 
    for( $x = 0; $x < $w; $x++ ){ 
        for( $y = 0; $y < $h; $y++ ){ 
            //get current alpha value (represents the TANSPARENCY!) 
            $colorxy = imagecolorat( $src_im, $x, $y ); 
            $alpha = ( $colorxy >> 24 ) & 0xFF; 
            //calculate new alpha 
            if( $minalpha !== 127 ){ 
                $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha ); 
            } else { 
                $alpha += 127 * $pct; 
            } 
            //get the color index with new alpha 
            $alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha ); 
            //set pixel with the new color + opacity 
            if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){ 
                return false; 
            } 
        } 
    } 
    // The image copy 
    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); 
} 
// USAGE EXAMPLE: 
$img_a = imagecreatefrompng('image1.png'); 
$img_b = imagecreatefrompng('wm2.png'); 
// SAME COMMANDS: 
imagecopymerge_alpha($img_a, $img_b, 10, 10, 0, 0, imagesx($img_b), imagesy($img_b),50); 
// OUTPUT IMAGE: 
header("Content-Type: image/png"); 
imagesavealpha($img_a, true); 
imagepng($img_a, NULL); 
?>

如果你使用这个例子,你会看到$img_a将低于$img_b,而不是一个奇怪的黑框,$img_b的透明度将被保留。

我遇到了完全相同的问题。对我来说,解决方案是使用imagecopyresampled而不是imagecopymerge。这将保持两个图像的透明度。

请注意要传递的参数略有不同