使用 PHP 合并图像时颜色错误

发布于 2024-08-27 12:43:48 字数 1208 浏览 0 评论 0原文

我想获取图像 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();

图像完全按照我想要的方式显示,但颜色错误。我最近添加了 imagecreatetruecolorimagecolortransparent 部分,但仍然得到错误的结果。

我还将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2024-09-03 12:43:48

经过长时间的尝试...一如既往,解决方案非常简单:

只需将背景图像也设为 24 位即可。
因此,如果有人正在寻找一种制作分层透明图像的方法,那么这是完整的代码:

<?php  

if (isset($_REQUEST['items'])){
    $req_items = $_REQUEST['items'];
} else {
    $req_items = 'a';
}

$items = explode(',',$req_items);

$bg_img = imagecreatefrompng('bg.png');
$bg_tc = imagecreatetruecolor(300, 200);
imagecopy($bg_tc,$bg_img,0,0,0,0,300,200);


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_tc, $image_tc, $dest_x, $dest_y, 0, 0, $bg_width, $bg_height, $opacity);

}
$file = $_SERVER['REQUEST_TIME'].'.jpg';
imagejpeg($bg_tc, $file, 100);
echo $file;
imagedestroy($image);
imagedestroy($bg_img);
imagedestroy($bg_tc);
imagedestroy($image_tc);
die();
?>

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:

<?php  

if (isset($_REQUEST['items'])){
    $req_items = $_REQUEST['items'];
} else {
    $req_items = 'a';
}

$items = explode(',',$req_items);

$bg_img = imagecreatefrompng('bg.png');
$bg_tc = imagecreatetruecolor(300, 200);
imagecopy($bg_tc,$bg_img,0,0,0,0,300,200);


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_tc, $image_tc, $dest_x, $dest_y, 0, 0, $bg_width, $bg_height, $opacity);

}
$file = $_SERVER['REQUEST_TIME'].'.jpg';
imagejpeg($bg_tc, $file, 100);
echo $file;
imagedestroy($image);
imagedestroy($bg_img);
imagedestroy($bg_tc);
imagedestroy($image_tc);
die();
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文