public function blend(&$image1, $image2, $type = 'normal', $opacity = 1.0, $position = 'top-left', $offsetX = 0, $offsetY = 0)
{
// Turn into position object
$position = new Position($position, $offsetX, $offsetY);
// Position is for $image2. $image1 is canvas.
list($offsetX, $offsetY) = $position->getXY($image1->getWidth(), $image1->getHeight(), $image2->getWidth(), $image2->getHeight());
// Check if it overlaps
if ($offsetX >= $image1->getWidth() or $offsetX + $image2->getWidth() <= 0 or $offsetY >= $image1->getHeight() or $offsetY + $image2->getHeight() <= 0) {
throw new \Exception('Invalid blending. Image 2 is outside the canvas.');
}
// Loop start X
$loopStartX = 0;
$canvasStartX = $offsetX;
if ($canvasStartX < 0) {
$diff = 0 - $canvasStartX;
$loopStartX += $diff;
}
// Loop start Y
$loopStartY = 0;
$canvasStartY = $offsetY;
if ($canvasStartY < 0) {
$diff = 0 - $canvasStartY;
$loopStartY += $diff;
}
if ($opacity !== 1) {
$this->opacity($image2, $opacity);
}
$type = strtolower($type);
if ($type === 'normal') {
$image1->getCore()->compositeImage($image2->getCore(), \Imagick::COMPOSITE_OVER, $loopStartX + $offsetX, $loopStartY + $offsetY);
} else {
if ($type === 'multiply') {
$image1->getCore()->compositeImage($image2->getCore(), \Imagick::COMPOSITE_MULTIPLY, $loopStartX + $offsetX, $loopStartY + $offsetY);
} else {
if ($type === 'overlay') {
$image1->getCore()->compositeImage($image2->getCore(), \Imagick::COMPOSITE_OVERLAY, $loopStartX + $offsetX, $loopStartY + $offsetY);
} else {
if ($type === 'screen') {
$image1->getCore()->compositeImage($image2->getCore(), \Imagick::COMPOSITE_SCREEN, $loopStartX + $offsetX, $loopStartY + $offsetY);
} else {
throw new \Exception(sprintf('Invalid blend type "%s".', $type));
}
}
}
}
return $this;
}