5

I am trying to change the opacity of an image using GD, Almost all the solutions that I found are similar to the code below, where you create a white transparent background and merge it with your image.

But this doesn't make the image transparent, the image just gets brighter, you can't actually look through it.

So my question is, how to change the opacity of an image so that you can look through it? Or am i doing something wrong with this code?

//Create an image with a white transparent background color
$newImage = ImageCreateTruecolor(300, 300);
$bg        = ImageColorAllocateAlpha($newImage, 255, 255, 255, 127);
ImageFill($newImage, 0, 0, $bg);

//Get the image
$source = imagecreatefrompng('my_image.png');

$opacity = 50;    

//Merge the image with the background
ImageCopyMerge($newImage,
               $source,
               0, 0, 0, 0,
               300,
               300,
               $opacity);

header('Content-Type: image/png');
imagepng($newImage);
imagedestroy($newImage);

Thank you!

4

3 回答 3

15

你只需要imagefilter使用IMG_FILTER_COLORIZE

$image = imagecreatefrompng('my_image.png');
$opacity = 0.5;
imagealphablending($image, false); // imagesavealpha can only be used by doing this for some reason
imagesavealpha($image, true); // this one helps you keep the alpha. 
$transparency = 1 - $opacity;
imagefilter($image, IMG_FILTER_COLORIZE, 0,0,0,127*$transparency); // the fourth parameter is alpha
header('Content-type: image/png');
imagepng($image);

imagealphablending,我认为,是出于绘图目的,所以你不想使用它。我可能错了。我们都应该查一下:)

如果你想使用百分比,你可以$opacity相应地计算。

于 2016-12-31T06:55:28.563 回答
6

没有直接的方法可以使用 DG 函数更改不透明度(实际上)。但可以使用逐像素操作来完成:

/**
 * @param resource $imageSrc Image resource. Not being modified.
 * @param float $opacity Opacity to set from 0 (fully transparent) to 1 (no change)
 * @return resource Transparent image resource
 */
function imagesetopacity( $imageSrc, $opacity )
{
    $width  = imagesx( $imageSrc );
    $height = imagesy( $imageSrc );

    // Duplicate image and convert to TrueColor
    $imageDst = imagecreatetruecolor( $width, $height );
    imagealphablending( $imageDst, false );
    imagefill( $imageDst, 0, 0, imagecolortransparent( $imageDst ));
    imagecopy( $imageDst, $imageSrc, 0, 0, 0, 0, $width, $height );

    // Set new opacity to each pixel
    for ( $x = 0; $x < $width; ++$x )
        for ( $y = 0; $y < $height; ++$y ) {
            $pixelColor = imagecolorat( $imageDst, $x, $y );
            $pixelOpacity = 127 - (( $pixelColor >> 24 ) & 0xFF );
            if ( $pixelOpacity > 0 ) {
                $pixelOpacity = $pixelOpacity * $opacity;
                $pixelColor = ( $pixelColor & 0xFFFFFF ) | ( (int)round( 127 - $pixelOpacity ) << 24 );
                imagesetpixel( $imageDst, $x, $y, $pixelColor );
            }
        }

    return $imageDst;
}

$source = imagecreatefrompng( 'my_image.png' );
$newImage = imagesetopacity( $source, 0.5 );
imagedestroy( $source );

header( 'Content-Type: image/png' );
imagepng( $newImage );
imagedestroy( $newImage );
于 2015-11-20T03:47:12.210 回答
0

您是否尝试将 alpha blending 设置为 true?

imagealphablending($newImage,true);

于 2016-12-06T20:45:17.730 回答