php - 在 PHP 中通过 JPG 透明 PNG

标签 php image png transparency php-gd

看似简单,其实不然:(

我正在尝试在图像 (jpg) 上添加水印(透明 png)之类的东西。 这是我正在使用的代码:

$width = 800; 
$height = 600; 
$bottom_image = imagecreatefromjpeg("portrait1.jpg"); 
$top_image = imagecreatefrompng("man2.png"); 
imagesavealpha($top_image, true); 
imagealphablending($top_image, true); 
imagecopy($bottom_image, $top_image, 200, 200, 0, 0, $width, $height); 
header('Content-type: image/png');
imagepng($bottom_image);

当我合并图像时,png 位于正确的位置,它上方和左侧的所有内容都很好(复制了 jpg),但其他所有内容都是黑色的。

我试过将 imagesavealpha 和 imagealphablending 设置为 false,没有任何区别。

您可以在 http://ekstrakt.selfip.com/photobomb/image.php 查看生成的图像

我在网上搜索过,找不到解决方案。

感谢任何帮助。

最佳答案

您的 $width$height 应该是水印的尺寸,而不是照片的尺寸。您告诉它要做的是复制比实际尺寸大得多的水印。当它读取不存在的图像部分(坐标越界)时,结果是不透明的黑色,给出您看到的结果。

关于php - 在 PHP 中通过 JPG 透明 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7985583/

相关文章:

hibernate - 如何使用 Struts 2 + Hibernate 3 向浏览器提供图像?

c++ - 为什么我的 libharu pdf 与 .png 图像过大?

html - Logo 和标题容器未正确调整大小

php - Codeigniter - 以管理员身份编辑用户

php - 如何使用 MySQLi 准备好的语句创建新的 MySQL 数据库用户?

php - Mysql 选择代码不起作用

c# - 从 url 加载和显示图像

linux - 网页截图 khtml2png 替代品

ios - PNG文件重命名后不显示在IOS设备上

php - Cucumber、webrat Mechanize 在 MAMP 上测试 php/drupal 应用程序、身份验证问题