在PHP中合并2图像时删除png透明

时间:2014-12-29 11:40:34

标签: php image

我有下面的代码,我想合并两个png文件,它们都有透明部分,

文件1:enter image description here

和文件2: enter image description here

,输出png文件为

enter image description here

我也试图取消注释

    //imagealphablending($src2, false);
    //imagesavealpha($src2, true);
    //imagecolortransparent($src2, 2130706432);

但不行,问题出在哪里?

      $background2=imagecreatefrompng('avatar_blank_big.png'); 
      $dest2 = $background2;

      $toyitems2 = array(
        'cloth-1763-1249' =>  'cloth-55e.png',
      );
      foreach ($toyitems2 as $i => $item) {
        $src2 = imagecreatefrompng($item);
        if (!$src2) continue;
        //imagealphablending($src2, false);
        //imagesavealpha($src2, true);
        //imagecolortransparent($src2, 2130706432);

        $src_x = imagesx($src2);
        $src_y = imagesy($src2);
        $list = explode('-', $i);
        //var_dump($list);
        imagecopymerge_alpha($dest2, $src2, intval($list[1]), intval($list[2]), 0, 0, $src_x, $src_y, 100);
        imagealphablending($dest2, true);
        imagesavealpha($dest2, true);
      }
      header('Content-Type: image/png');
      imagepng($dest2);

      function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    // creating a cut resource 
    $cut = imagecreatetruecolor($src_w, $src_h); 

    // copying relevant section from background to the cut resource 
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 

    // copying relevant section from watermark to the cut resource 
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 

    // insert cut resource to destination image 
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
  }

0 个答案:

没有答案