Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将两个图像与PHP(GD-GD2库)混合,如乘法、colorburn、colordodge等_Php_Image_Gd_Blend_Multiplication - Fatal编程技术网

如何将两个图像与PHP(GD-GD2库)混合,如乘法、colorburn、colordodge等

如何将两个图像与PHP(GD-GD2库)混合,如乘法、colorburn、colordodge等,php,image,gd,blend,multiplication,Php,Image,Gd,Blend,Multiplication,我想在PHP和GD库中混合两个图像,比如乘法、叠加和其他混合模式 My images=İmage1.jpg——imageblend.jpg或image3.png 请帮我写例子或者一步一步教我?我已经找到了一些示例,但它们不起作用。GD中没有很多这样的混合模式,因此您必须自己实现它们。不幸的是,它将比本机C实现慢得多,但这是可能的。只需加载两个图像并在整个基础图像上迭代,应用混合模式将每个像素从顶部图像复制到基础图像上。加法/减法/乘法非常简单,普通的alpha混合也相当简单(尽管alpha混合是

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

My images=İmage1.jpg——imageblend.jpg或image3.png


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

GD中没有很多这样的混合模式,因此您必须自己实现它们。不幸的是,它将比本机C实现慢得多,但这是可能的。只需加载两个图像并在整个基础图像上迭代,应用混合模式将每个像素从顶部图像复制到基础图像上。加法/减法/乘法非常简单,普通的alpha混合也相当简单(尽管alpha混合是内置的,只要您正确使用
imagealphablending
)。我不知道颜色燃烧或颜色减淡的算法,但我相信快速搜索会找到如何做到这一点。至于overlay,如果在复制顶部图像之前使用
imagelayereffect
,它实际上是内置在GD中的

为了叠加效果,我从中提取了这段代码。我还为乘法效果添加了一些替代代码,向您展示了如何将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);     
?>

GD中内置的混合模式不多,因此您必须自己实现它们。不幸的是,它将比本机C实现慢得多,但这是可能的。只需加载两个图像并在整个基础图像上迭代,应用混合模式将每个像素从顶部图像复制到基础图像上。加法/减法/乘法非常简单,普通的alpha混合也相当简单(尽管alpha混合是内置的,只要您正确使用
imagealphablending
)。我不知道颜色燃烧或颜色减淡的算法,但我相信快速搜索会找到如何做到这一点。至于overlay,如果在复制顶部图像之前使用
imagelayereffect
,它实际上是内置在GD中的

为了叠加效果,我从中提取了这段代码。我还为乘法效果添加了一些替代代码,向您展示了如何将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);     
?>

感谢@ultranaut的编辑。感谢@ultranaut的编辑。感谢@meustrus。。我在php类上发现了一些算法。但是我不明白。我想学它。一步一步地,我添加了一些代码,向您展示如何实现叠加和多重混合模式。非常感谢@meustrus。。你的描述对学习这些代码很有帮助。。小心,梅斯特鲁斯……倍增工作完美无瑕。非常感谢。有人知道它在其他混合模式下是如何工作的?只是在谷歌上快速搜索了一下,发现了这个:。因此,对于颜色燃烧(1-(1-Target)/混合,您可以对每种颜色执行此操作:
'red'=>intval(255.0-(255.0-$topColor['red'])/$baseColor['red'])
谢谢@meustrus。。我在php类上发现了一些算法。但是我不明白。我想学它。一步一步地,我添加了一些代码,向您展示如何实现叠加和多重混合模式。非常感谢@meustrus。。你的描述对学习这些代码很有帮助。。小心,梅斯特鲁斯……倍增工作完美无瑕。非常感谢。有人知道它在其他混合模式下是如何工作的?只是在谷歌上快速搜索了一下,发现了这个:。因此,对于颜色燃烧(1-(1-Target)/混合,您可以对每种颜色执行此操作:
红色=>intval(255.0-(255.0-$topColor['red'])/$baseColor['red'])