在PHP中合并两个透明图像时的异常结果 [英] Unusual result when merging two transparent images in php
问题描述
我想知道我做错什么了吗,或者是否能像我得到的结果一样好.左侧的两个PNG均为95x111.机器人的图像周围有5像素左右的透明像素填充,但是合并时似乎会引起问题?
Im wondering if I'm doing something wrong, or if this is as good a result as im going to get. Both PNGs on the left are 95x111. The image of the robot has a 5px or so padding of transparent pixels around it, but it seems to be causing problems when they merge?
$avatar = imagecreatefrompng("../guy.png");
$borderImg = imagecreatefrompng("../frame.png");
imagealphablending( $borderImg, false );
imagesavealpha( $borderImg, true );
imagecopyresampled($avatar,$borderImg, 0, 0, 0, 0, 95, 111,95, 111);
imagepng($avatar, $newfilenameBig);
我尝试了所有我能想到的imagealphablending和imagesavealpha组合.当我将$ avatar设置为imagesavealpa = true时,它甚至不显示所有图像,仅显示框架.看起来不奇怪吗? 就我将要使用PHP GD而言,这是吗?
Ive tried every combo of imagealphablending and imagesavealpha I can think of. When I set $avatar to imagesavealpa= true, then it doesnt even show the image as all, just the frame. Doesn't that seem strange? Is this as far as i'm gonna get using PHP GD?
更新:当使用24位模式在PS中手动创建两个图像时,可以实现所需的结果.有没有办法使用imagecopy或类似方法做到这一点?
UPDATE: The desired result can be achieved when both images are created manually in PS using 24 bit mode. Is there a way to do this using imagecopy or similar?
推荐答案
尝试以下代码可以正常工作.
Try the following code its working fine.
$width = 95;
$height = 111;
$base_image = imagecreatefrompng("../guy.png");
$top_image = imagecreatefrompng("../frame.png");
imagesavealpha($top_image, false);
imagealphablending($top_image, false);
imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height);
imagepng($base_image, "merged.png");
这篇关于在PHP中合并两个透明图像时的异常结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!