如何将2个图像与PHP(GD-GD2库)混合,如multiply,colorburn,colordodge等

时间:2013-03-16 15:12:56

标签: php image gd blend multiplying

我想在PHP和GD库中混合两个图像 - 如乘法,叠加和其他混合模式。

我的图片=İmage1.jpg - imageblend.jpg或image3.png

请帮我写一些例子或一步一步教我?我找到了一些例子,但它们不起作用。

1 个答案:

答案 0 :(得分:8)

GD内置的混合模式并不多,所以你必须自己实现它们。不幸的是,它会比本机C实现慢得多,但它是可能的。只需加载两个图像并迭代整个基本图像,应用混合模式将每个单独的像素从顶部图像复制到基本图像上。添加/减去/乘法非常简单,而正常的alpha混合非常简单(尽管只要正确使用imagealphablending就可以内置alpha混合)。我不知道颜色灼伤或颜色躲闪的算法,但我确信快速搜索会发现如何做到这一点。至于叠加,如果你在复制顶部图像之前使用imagelayereffect,它实际上已内置到GD中。

我从permadi.com提取此代码以获得叠加效果。我还为多重效果添加了一些备用代码,向您展示如何将PHP用于混合模式算法。此代码假定顶部图像在任一维度上都不大于基本图像。

<?php
// GD images can be true-color or indexed color. This makes a difference for getting
// and setting pixels so we'll take note of each image.
// Load the base image. For overlays, this should be the template ("overlay.png" in source)
$baseImage = imagecreatefrompng("base.png");
$baseIsTrueColor = imageistruecolor($baseImage);
// Load the image to blend on top.
$topImage = imagecreatefrompng("top.png");
$topIsTrueColor = imageistruecolor($topImage);

// Get image dimensions
$baseWidth  = imagesx($baseImage);
$baseHeight = imagesy($baseImage);
$topWidth   = imagesx($topImage);
$topHeight  = imagesy($topImage);

// Destination X and Y - these values will center $topImage within $baseImage:
$destX = ($baseWidth - $topWidth) / 2;
$destY = ($baseHeight - $topHeight) / 2;

// OVERLAY MODE {
  // This line causes all GD operations to use the overlay algorithm
  // when blending pixels together.
  imagelayereffect($baseImage, IMG_EFFECT_OVERLAY);
  // Blend the top image onto the base image.
  imagecopy(
    $baseImage, // destination
    $topImage, // source
    // destination x and y
    $destX, $destY,
    // x, y, width, and height of the area of the source to copy
    0, 0, $topWidth, $topHeight);
// } OVERLAY

// MULTIPLY MODE {
  // Because we can't just use imagecopy, we have to iterate over all the pixels in
  // the entire image in order to apply the multiply algorithm to each individual pixel.
  // There is probably an easier way to handle true-color vs. indexed color in this
  // section but without testing the code this was the most likely to work. Depending on
  // how `imagecolorsforindex` and `imagecolorclosestalpha` work this section might work
  // a lot simpler.
  for ($x = 0; $x < $topWidth; ++$x) {
    for ($y = 0; $y < $topHeight; ++$y) {
      // First get the colors for the base and top pixels.
      $color = imagecolorat($baseImage, $x + $destX, $y + $destY);
      // If the image is true-color, we simply use bitwise operations to separate out
      // red, green, blue, and alpha from the result of imagecolorat above.
      if ($baseIsTrueColor) {
        $baseColor = array(
          'red'   => ($color >> 16) & 0xFF,
          'green' => ($color >> 8) & 0xFF,
          'blue'  => $color & 0xFF,
          'alpha' => ($color & 0x7F000000) >> 24,
        );
      }
      // If the image uses indexed color, we can get the color components by looking up
      // the color index in the image's color table.
      else {
        $baseColor = imagecolorsforindex($baseImage, $color);
      }

      $color = imagecolorat($topImage, $x, $y);
      // If the image is true-color, we simply use bitwise operations to separate out
      // red, green, blue, and alpha from the result of imagecolorat above.
      if ($topIsTrueColor) {
        $topColor = array(
          'red'   => ($color >> 16) & 0xFF,
          'green' => ($color >> 8) & 0xFF,
          'blue'  => $color & 0xFF,
          'alpha' => ($color & 0x7F000000) >> 24,
        );
      }
      // If the image uses indexed color, we can get the color components by looking up
      // the color index in the image's color table.
      else {
        $topColor = imagecolorsforindex($topImage, $color);
      }

      // Now perform the multiply algorithm.
      $destColor = array(
        'red'   => intval($baseColor['red']   * ($topColor['red']   / 255.0)),
        'green' => intval($baseColor['green'] * ($topColor['green'] / 255.0)),
        'blue'  => intval($baseColor['blue']  * ($topColor['blue']  / 255.0)),
        'alpha' => intval($baseColor['alpha'] * ($topColor['alpha'] / 127.0)),
      );

      // Now set the destination pixel.
      $colorIndex = imagecolorallocatealpha($baseImage, $destColor['red'], $destColor['green'], $destColor['blue'], $destColor['alpha']);
      // If we failed to allocate the color, try to find the already allocated color
      // that is closest to what we want.
      if ($colorIndex === FALSE) {
        $colorIndex = imagecolorclosestalpha($baseImage, $destColor['red'], $destColor['green'], $destColor['blue'], $destColor['alpha']);
      }
      // Now that we have a valid color index, set the pixel to that color.
      imagesetpixel($baseImage, $x + $destX, $y + $destY, $colorIndex);
    }
  }
// } MULTIPLY

// Set type of image and send the output
header("Content-type: image/png");
imagepng($baseImage);

// Release memory
imagedestroy($baseImage);
imagedestroy($topImage);     
?>

我没有对代码进行测试,因此它可能无法正常运行,而且几乎肯定会比它需要的更复杂,但它应该是自己做这些东西的良好起点。