在php中合并两个图像(不同大小)

在php中合并两个图像(不同大小),php,image,image-processing,resize,Php,Image,Image Processing,Resize,我发现这个脚本可以在php中合并两个图像(保持透明度): 当我这样做的时候,透明度就不会发生,有人知道我哪里做错了吗 编辑: 在上面的代码中,它拍摄以下图像: 这将产生: 但理想的效果是: 谢谢当你花了很多时间写一个问题然后找出答案的时候,你不讨厌吗 添加imagealphabling($thumb,false)在调用imagecopyresized之前 function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $sr

我发现这个脚本可以在php中合并两个图像(保持透明度):

当我这样做的时候,透明度就不会发生,有人知道我哪里做错了吗

编辑:

在上面的代码中,它拍摄以下图像:

这将产生:

但理想的效果是:


谢谢

当你花了很多时间写一个问题然后找出答案的时候,你不讨厌吗

添加
imagealphabling($thumb,false)在调用
imagecopyresized
之前

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct, $trans = NULL)
{
  $dst_w = imagesx($dst_im);
  $dst_h = imagesy($dst_im);

  // bounds checking
  $src_x = max($src_x, 0);
  $src_y = max($src_y, 0);
  $dst_x = max($dst_x, 0);
  $dst_y = max($dst_y, 0);
  if ($dst_x + $src_w > $dst_w)
    $src_w = $dst_w - $dst_x;
  if ($dst_y + $src_h > $dst_h)
    $src_h = $dst_h - $dst_y;

  for($x_offset = 0; $x_offset < $src_w; $x_offset++)
    for($y_offset = 0; $y_offset < $src_h; $y_offset++)
    {
      // get source & dest color
      $srccolor = imagecolorsforindex($src_im, imagecolorat($src_im, $src_x + $x_offset, $src_y + $y_offset));
      $dstcolor = imagecolorsforindex($dst_im, imagecolorat($dst_im, $dst_x + $x_offset, $dst_y + $y_offset));

      // apply transparency
      if (is_null($trans) || ($srccolor !== $trans))
      {
        $src_a = $srccolor['alpha'] * $pct / 100;
        // blend
        $src_a = 127 - $src_a;
        $dst_a = 127 - $dstcolor['alpha'];
        $dst_r = ($srccolor['red'] * $src_a + $dstcolor['red'] * $dst_a * (127 - $src_a) / 127) / 127;
        $dst_g = ($srccolor['green'] * $src_a + $dstcolor['green'] * $dst_a * (127 - $src_a) / 127) / 127;
        $dst_b = ($srccolor['blue'] * $src_a + $dstcolor['blue'] * $dst_a * (127 - $src_a) / 127) / 127;
        $dst_a = 127 - ($src_a + $dst_a * (127 - $src_a) / 127);
        $color = imagecolorallocatealpha($dst_im, $dst_r, $dst_g, $dst_b, $dst_a);
        // paint
        if (!imagesetpixel($dst_im, $dst_x + $x_offset, $dst_y + $y_offset, $color))
          return false;
        imagecolordeallocate($dst_im, $color);
      }
    }
  return true;
}
header('Content-Type: image/png');

// Get image for the watermark
$smallImageFilename = 'http://ing0.net/watermark.png'; 
list($oldwidthSmall, $oldheightSmall) = getimagesize($smallImageFilename);
$newwidthSmall = $newheightSmall = 50;
$thumb = imagecreatetruecolor($newwidthSmall, $newheightSmall);
$source = imagecreatefrompng($smallImageFilename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidthSmall, $newheightSmall, $oldwidthSmall, $oldheightSmall);

// Create watermark img
$watermark = imagecreatetruecolor(50, 50);
$outImage = imagecreatefrompng('http://ing0.net/image.png');     
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($watermark);
$sy = imagesy($watermark);

imagecopymerge_alpha($outImage, $thumb, imagesx($outImage) - $sx - $marge_right, imagesy($outImage) - $sy - $marge_bottom, 0, 0, imagesx($thumb), imagesy($thumb), 100);

imagepng($outImage);
imagedestroy($outImage);