Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

PHP合并透明图背景为黑色时候的处理

 Share


HACK1949

Recommended Posts

情况这样,左边的图片是使用php磨圆后的图片,然后与白色背景的图片合并之后,透明部分奇怪的变成了黑色。

?i=20210722103017647.png?,type_ZmFuZ3poZ

最初的代码,简单的合并

  1. //背景图片
  2. $bgPath= './resource/bg.png';
  3. $bgImage= imagecreatefrompng($bgPath);
  4. //透明图片
  5. $desPath = './resource/avator.png';
  6. $desImage = imagecreatefrompng($desPath);
  7. //创建图像
  8. $imagebox = imagecreatetruecolor(imagesx($bgImage),imagesy($bgImage));
  9. imagecopyresampled($imagebox, $bgImage, 0, 0, 0, 0, imagesx($bgImage), imagesy($bgImage), imagesx($bgImage), imagesy($bgImage));
  10. //----合并透明图片----
  11. imagecopymerge($imagebox, $desImage, 20, 0, 0, 0, imagesx($desImage), imagesy($desImage), 100);
  12. $filename = './images/'.uniqid().'.jpg';
  13. imagejpeg($imagebox,$filename);
  14. imagedestroy($imagebox);

第一种方式

使用 imagecopy 合并

这里既是直接把 imagecopymerge($imagebox, $desImage, 20, 0, 0, 0, imagesx($desImage), imagesy($desImage), 100);   这段代码替换成 imagecopy($imagebox, $desImage, 20, 0, 0, 0, imagesx($desImage), imagesy($desImage));  

第二种方式

如果需要使用 imagecopymerge 合并 ,还是把那段代码替换成如下代码,测试能够正常工作

  1. $x = imagesx($desImage);
  2. $y = imagesy($desImage);
  3. $newDes = imagecreatetruecolor($x, $y);
  4. $color = imagecolorallocate($newDes,255,255,255);
  5. imagecolortransparent($newDes,$color);
  6. imagefill($newDes,0,0,$color);
  7. imagecopy($newDes, $desImage, 0, 0, 0, 0,$x, $y);
  8. imagecopymerge($imagebox, $newDes, 20, 0, 0, 0, $x, $y, 100);

 一顿操作之后

?i=20210722113139670.png

Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...