具有透明背景的 PHP imagecopy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7728508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:15:09  来源:igfitidea点击:

PHP imagecopy with transparent background

phppngimagecreatefrompng

提问by 2by

I use this code to create an image from another png image, the background is black by default. My question is how to set a transparent background?

我使用此代码从另一个 png 图像创建图像,默认情况下背景为黑色。我的问题是如何设置透明背景?

$input = imagecreatefrompng('image.png');
$output = imagecreatetruecolor(50, 50);

imagecopy($output, $input, 4,0, 8,8, 8,8);
imagecopy... etc.

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

Is there a easy way of doing this? Thanks

有没有简单的方法来做到这一点?谢谢

回答by Wazy

Sets the transparent color in the given image.

设置给定图像中的透明颜色。

int imagecolortransparent ( resource $image [, int $color ] )

Here's the link

这是链接

回答by leo.vingi

Since the PHP function imagecopymergedoesn't work with the Alpha channel, you'll want to use the function from the first comment on this page imagecopymerge_alpha: http://php.net/manual/en/function.imagecopymerge.php

由于 PHP 函数imagecopymerge不适用于 Alpha 通道,因此您需要使用本页第一条评论中的函数imagecopymerge_alphahttp: //php.net/manual/en/function.imagecopymerge.php

Just have the transparent image as the base and merge it together with the image you need.

只需将透明图像作为基础并将其与您需要的图像合并在一起。

I've tried it out and it works fine for a project of mine.

我已经试过了,它适用于我的一个项目。

回答by Mohammad Anini

imagealphablending($input, true);
imagesavealpha($input, true);

imagealphablending($output, true);
imagesavealpha($output, true);

回答by Curtis

None of the solutions worked for me, it would always convert transparent pixels on the source image to black on the destination image. What worked was changing imagecopy/imagecopymerge/imagecopymerge_alpha to imagecopyresampled and just passing the same width and height twice.

没有一个解决方案对我有用,它总是将源图像上的透明像素转换为目标图像上的黑色。有效的是将 imagecopy/imagecopymerge/imagecopymerge_alpha 更改为 imagecopyresampled 并仅传递相同的宽度和高度两次。

    //Create destination image.
    $png = imagecreatetruecolor(1024, 1024);
    imagealphablending($png, false);
    imagesavealpha($png, true);
    //Make destination image be all transparent.
    $color = imagecolorallocatealpha($png, 0, 0, 0, 127); //127 means completely transparent.
    imagefill($png, 0, 0, $color);

    //Load source image.
    $png2 = imagecreatefrompng($sourceurl);
    imagealphablending($png2, false);
    imagesavealpha($png2, true);
    $sizex = imagesx($png2);
    $sizey = imagesy($png2);

    //Copy to destination and save to file.
    imagecopyresampled( $png, $png2, 
    0, 0,
    0, 0, 
    $sizex, $sizey, 
    $sizex, $sizey);
    imagepng($png, "result.png");

回答by Vince Pike

Full credit goes to: http://consistentcoder.com/combine-a-transparent-png-image-on-top-of-another-image-with-php

全部归功于:http: //consistentcoder.com/combine-a-transparent-png-image-on-top-of-another-image-with-php

The following code will overlay the foreground image onto the background image while preserving the transparency of the overlay:

以下代码将前景图像叠加到背景图像上,同时保留叠加层的透明度

//set the source image (foreground)
$sourceImage = 'table.png';

//set the destination image (background)
$destImage = 'create-a-surreal-head-of-tree-photo-manipulation.jpg';

//get the size of the source image, needed for imagecopy()
list($srcWidth, $srcHeight) = getimagesize($sourceImage);

//create a new image from the source image
$src = imagecreatefrompng($sourceImage);

//create a new image from the destination image
$dest = imagecreatefromjpeg($destImage);

//set the x and y positions of the source image on top of the destination image
$src_xPosition = 75; //75 pixels from the left
$src_yPosition = 50; //50 pixels from the top

//set the x and y positions of the source image to be copied to the destination image
$src_cropXposition = 0; //do not crop at the side
$src_cropYposition = 0; //do not crop on the top

//merge the source and destination images
imagecopy($dest,$src,$src_xPosition,$src_yPosition,
          $src_cropXposition,$src_cropYposition,
          $srcWidth,$srcHeight);

//output the merged images to a file
/*
 * '100' is an optional parameter,
 * it represents the quality of the image to be created,
 * if not set, the default is about '75'
 */
imagejpeg($dest,
    'combine-a-transparent-png-image-on-top-of-another-image-with-php-01.jpg',
    100);

//destroy the source image
imagedestroy($src);

//destroy the destination image
imagedestroy($dest);

回答by Jan Wiemers