avalanche123/Imagine — pull request #215 by AlloVince (2013-04-12 08:35:52+00:00)
Title: add Opacity parameter & blending callback handle for image->paste()

I am trying to make same effect like Photoshop layer blending options, but there is no way to change layer paste opacity in Imagine. I added 2 parameters to image->paste() to handle this:

~~~~
use Imagine\Image\Point;
use Imagine\Image\Color;

class Blending {
public static function overlay($dstImage, $srcImage, $dstSource, $srcSource)
{
$width = $dstImage->getSize()->getWidth();
$height = $dstImage->getSize()->getHeight();
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$point = new Point($x, $y);
$color = $dstImage->getColorAt($point);
$dR = $color->getRed();
$dG = $color->getGreen();
$dB = $color->getBlue();
$color = $srcImage->getColorAt($point);
$sR = $color->getRed();
$sG = $color->getGreen();
$sB = $color->getBlue();
$dstImage->draw()->dot($point, new Color(array($dR * $sR / 255, $dG * $sG / 255, $dB * $sB / 255)));
}
}
}

public static function overlayGD($dstImage, $srcImage, $dstSource, $srcSource)
{
$width = imagesx($dstSource);
$height = imagesy($dstSource);
$layer = imagecreatetruecolor($width, $height);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$color = imagecolorat($dstSource, $x, $y);
$dR = ($color >> 16) & 0xFF;
$dG = ($color >> 8) & 0xFF;
$dB = $color & 0xFF;
$color = imagecolorat($srcSource, $x, $y);
$sR = ($color >> 16) & 0xFF;
$sG = ($color >> 8) & 0xFF;
$sB = $color & 0xFF;

imagesetpixel($dstSource, $x, $y, imagecolorallocate($srcSource, $dR * $sR / 255, $dG * $sG / 255, $dB * $sB / 255));
}
}
}
}

$imagine = new Imagine\Gd\Imagine();
$image = $imagine->open(__DIR__ . '/A.png');
$layer = $imagine->open(__DIR__ . '/B.jpg');

//Handle layer blending by Imagine API
$image->paste($layer, new Imagine\Image\Point(0, 0), 100, 'Blending::overlay');

//Or we could use GD lib original way
$image->paste($layer, new Imagine\Image\Point(0, 0), 100, 'Blending::overlayGd');
~~~~