使用 PHP 合并图像时颜色错误
我想获取图像 ID 并根据给定 ID 从文件创建合并图像。 这段代码由ajax调用并返回图像文件名(这是服务器时间,以防止浏览器缓存)。 代码:
if (isset($_REQUEST['items'])){
$req_items = $_REQUEST['items'];
} else {
$req_items = 'a';
}
$items = explode(',',$req_items);
$bg_img = imagecreatefrompng('bg.png');
for ($i=0; $i<count($items); $i++){
$main_img = $items[$i].'-large.png';
$image = imagecreatefrompng($main_img);
$image_tc = imagecreatetruecolor(300, 200);
imagecopy($image_tc,$image,0,0,0,0,300,200);
$black = imagecolorallocate($image_tc, 0, 0, 0);
imagecolortransparent($image_tc, $black);
$opacity = 100;
$bg_width = 300;
$bg_height = 200;
$dest_x = 0;//$image_size[0] - $bg_width - $padding;
$dest_y = 0;//$image_size[1] - $bg_height - $padding;
imagecopymerge($bg_img, $image_tc, $dest_x, $dest_y, 0, 0, $bg_width, $bg_height, $opacity)
;
}
$file = $_SERVER['REQUEST_TIME'].'.jpg';
imagejpeg($bg_img, $file, 100);
echo $file;
imagedestroy($bg_img);
imagedestroy($image);
die();
图像完全按照我想要的方式显示,但颜色错误。我最近添加了 imagecreatetruecolor 和 imagecolortransparent 部分,但仍然得到错误的结果。
我还将 PNG 本身保存为 24 位格式,后来又保存为 8 位格式 - 没有帮助。 每个想法都非常受欢迎! 谢谢
I want to get images ID's and creat from files a merged image according to the given ID's.
This code is called by ajax and return the image file name (which is the server time to prevent browser caching).
code:
if (isset($_REQUEST['items'])){
$req_items = $_REQUEST['items'];
} else {
$req_items = 'a';
}
$items = explode(',',$req_items);
$bg_img = imagecreatefrompng('bg.png');
for ($i=0; $i<count($items); $i++){
$main_img = $items[$i].'-large.png';
$image = imagecreatefrompng($main_img);
$image_tc = imagecreatetruecolor(300, 200);
imagecopy($image_tc,$image,0,0,0,0,300,200);
$black = imagecolorallocate($image_tc, 0, 0, 0);
imagecolortransparent($image_tc, $black);
$opacity = 100;
$bg_width = 300;
$bg_height = 200;
$dest_x = 0;//$image_size[0] - $bg_width - $padding;
$dest_y = 0;//$image_size[1] - $bg_height - $padding;
imagecopymerge($bg_img, $image_tc, $dest_x, $dest_y, 0, 0, $bg_width, $bg_height, $opacity)
;
}
$file = $_SERVER['REQUEST_TIME'].'.jpg';
imagejpeg($bg_img, $file, 100);
echo $file;
imagedestroy($bg_img);
imagedestroy($image);
die();
The images are shown exactly as I want but with wrong colors. I lately added the part with imagecreatetruecolor and imagecolortransparent, and still got wrong results.
I also saved the PNG itself on a 24 bit format and also later as 8 bit - not helping.
every ideas is very welcomed !
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过长时间的尝试...一如既往,解决方案非常简单:
只需将背景图像也设为 24 位即可。
因此,如果有人正在寻找一种制作分层透明图像的方法,那么这是完整的代码:
After long time of trying... as always the solution was very simple:
Just make the background image a 24 bit as well.
So if someone is looking for a way to make layered transparent images this is the complete code: