php 将透明 png 图像合并为 jpeg 以防止出现白色背景
php merge transparent png image to jpeg preventing the white background
我已经使用 php GD 库成功地将 png 图像合并到 jpeg 背景,但是我希望 png 图像具有透明背景,但白色背景不断显示,如下所示:
我看了一些帖子来解决这个问题并尝试了不同的实现方法都是徒劳的,关于我应该添加或做什么有什么建议吗?
这是我的代码:
<?php
$background = imagecreatefromjpeg('img3.jpg');
$bird = imagecreatefrompng('img4.png');
$bird_x = imagesx($bird);
$bird_y = imagesy($bird);
imagesavealpha($bird, true);
$color = imagecolorallocatealpha($bird, 0, 0, 0, 127);
imagefill($bird, 0, 0, $color);
if (imagecopymerge($background, $bird, 0, 0, 0, 0, $bird_x, $bird_y, 100))
{
header('Content-Type: image/jpeg');
imagejpeg($background);
imagedestroy($bird);
}
else
{
header('Content-Type: text/html');
echo "Failed to Merge images!";
}
?>
我想指定的行为是设计使然。
imagecopymerge
将源图像的 N% 像素与目标图像的 (100-N)% 像素合并。根据这个定义,可以合理地假设您在复制鸟的区域(即整个矩形)中看不到任何背景图像像素。因此白色像素而不是 alpha。
尝试 imagecopy
或 imagecopyresampled
() 而不调整大小。
使用图像复制
imagecopy($dest_image, $src, ($offset + 250), $offset, 0, 0, imagesx($src),imagesy($src));
我已经使用 php GD 库成功地将 png 图像合并到 jpeg 背景,但是我希望 png 图像具有透明背景,但白色背景不断显示,如下所示:
我看了一些帖子来解决这个问题并尝试了不同的实现方法都是徒劳的,关于我应该添加或做什么有什么建议吗?
这是我的代码:
<?php
$background = imagecreatefromjpeg('img3.jpg');
$bird = imagecreatefrompng('img4.png');
$bird_x = imagesx($bird);
$bird_y = imagesy($bird);
imagesavealpha($bird, true);
$color = imagecolorallocatealpha($bird, 0, 0, 0, 127);
imagefill($bird, 0, 0, $color);
if (imagecopymerge($background, $bird, 0, 0, 0, 0, $bird_x, $bird_y, 100))
{
header('Content-Type: image/jpeg');
imagejpeg($background);
imagedestroy($bird);
}
else
{
header('Content-Type: text/html');
echo "Failed to Merge images!";
}
?>
我想指定的行为是设计使然。
imagecopymerge
将源图像的 N% 像素与目标图像的 (100-N)% 像素合并。根据这个定义,可以合理地假设您在复制鸟的区域(即整个矩形)中看不到任何背景图像像素。因此白色像素而不是 alpha。
尝试 imagecopy
或 imagecopyresampled
(
使用图像复制
imagecopy($dest_image, $src, ($offset + 250), $offset, 0, 0, imagesx($src),imagesy($src));