PHP中使用带透明背景的imagecopy函数

8

我使用这段代码从另一个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);

有没有简单的方法来做这件事?谢谢。
6个回答

13

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

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

这里是链接


2
是的!这个有效:$black = imagecolorallocate($output, 0, 0, 0); imagecolortransparent($output, $black); 在imagecopy之前。 - 2by
2
如果我想要透明色作为透明颜色怎么办? - Curtis
1
所有被复制的内容在目标图像上都会以黑色绘制其透明颜色。我不想失去源图像中已经是黑色的像素。 - Curtis

12
自从PHP函数imagecopymerge无法处理Alpha通道以来,您需要使用此页面上第一个评论中的函数imagecopymerge_alphahttp://php.net/manual/en/function.imagecopymerge.php 只需将透明图像作为基础,并将其与您需要的图像合并即可。
我尝试过它,对于我的一个项目来说效果很好。

我也试过了,可以用。 - Tolgay Toklar
当我复制带有透明度的源图像时,这对我没有起作用,源图像中的所有透明颜色都会被绘制为黑色到目标图像上。 - Curtis

5

对我来说,这些解决方案都没有用,因为它们总是将源图像上的透明像素转换为目标图像上的黑色。有效的方法是将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");

这对我也起作用,可以剪切包含一些花哨透明度的gif图像的部分(不知道为什么只有少数图像会出现问题)。 谢谢! - DevilaN

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

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

6
在运行 imagesagealpha 前,应将弯曲设置为 false ,否则会出现错误。 - Martin

1
全额信用归于: http://consistentcoder.com/combine-a-transparent-png-image-on-top-of-another-image-with-php 以下代码将在保留叠加层透明度的同时,将前景图像覆盖在背景图像上:
//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);

0

imagesavealpha 只是设置在写出图像数据(到文件或缓冲区)时是否保存 alpha 通道信息。 - griffin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接