php - 在 PHP 中合并两张图片并将其另存为一张(一张透明背景)

我有以下场景。有两个大小完全相同的 .png 图像。一张是背景图片,另一张是叠加图片。

叠加层由透明背景和白色字母组成。

我正在尝试将这两张图片合二为一,以便最终在背景图片上显示白色文本。

我用这段代码试过了:

$image_1 = imagecreatefrompng('newimages/'.$overla);

$imgFinal = imagecreatefrompng('imagebackrgounds/background.png');

imagealphablending($imgFinal, true);
imagesavealpha($imgFinal, false);

imagecopy($imgFinal, $image_1, 0, 0, 0, 0, 600, 579);

imagepng($imgFinal, 'new/new.png');

上面的代码只保存了一张白色的图片。我在这里做错了什么?有人有工作代码吗?

这是背景图片:

这是透明背景的图像(它应该在背景之上)

最佳答案

解决方法:

imagecolortransparent($image_1,imagecolorat($image_1,0,0));

完整代码:

$image_1 = imagecreatefrompng('newimages/'.$overla);
imagecolortransparent($image_1,imagecolorat($image_1,0,0));

$imgFinal = imagecreatefrompng('imagebackrgounds/background.png');

imagealphablending($imgFinal, true);
imagesavealpha($imgFinal, false);

imagecopy($imgFinal, $image_1, 0, 0, 0, 0, 600, 579);

imagepng($imgFinal, 'new/new.png');

https://stackoverflow.com/questions/43190481/

相关文章:

java - 我收到错误 : Invocation of init method failed; n

wordpress - WP_Query - 按文件名订购附件?

apache-spark - 检查spark sql中的日期有效性

python - 表示 SQLAlchemy 中偏移日期的混合属性

php - 单元测试oAuth2授权码

typescript - vscode 激活扩展失败 : Cannot find module wi

caching - 通过自定义响应 header 绕过 Nginx 缓存

javascript - react js 多图像上传与预览

microservices - 基于微服务的架构中的最终一致性暂时限制了功能

angularjs - 人行横道的替代方案