Image transparency and alpha when merging images with PHP

限于喜欢 提交于 2019-11-30 22:33:47

Please note that your frame has white edges and if you sill want the windows to be wite you need to crop it and remove the imagecolortransparent added below if not you can use this

$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";

$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
imagecolortransparent($src, imagecolorat($src, 0, 0));

$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);

// Output and free from memory
header('Content-Type: image/png');
imagegif($dest);

imagedestroy($dest);
imagedestroy($src);

Output


You can also have

$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";

$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);

$src_x = imagesx($src);
$src_y = imagesy($src);

$srcNew = imagecreatetruecolor($src_x, $src_y);
ImageColorTransparent($srcNew, imageColorAllocate($srcNew, 0, 0, 0));
imagecopy($srcNew, $src, 70, 50, 78, 60, 473, 293);
imagecopymerge($dest, $srcNew, 0, 0, 0, 0, $src_x, $src_y, 100);

header('Content-Type: image/png');
imagegif($dest);

imagedestroy($dest);
imagedestroy($src);

Output

Your image is not transparent as you described, try using this instead if I understood what you described correctly.

also you should find a program which does not transform transparency to white when saving (or check for options regarding this) if you really made those transparent in the first place.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!