private function _blendOverlay($canvas, $gd1, $gd2, $loopStartY, $loopEndY, $loopStartX, $loopEndX, $offsetX, $offsetY, $opacity)
{
for ($y = $loopStartY; $y <= $loopEndY; $y++) {
for ($x = $loopStartX; $x <= $loopEndX; $x++) {
$canvasX = $x + $offsetX;
$canvasY = $y + $offsetY;
$argb1 = imagecolorat($gd1, $canvasX, $canvasY);
$r1 = $argb1 >> 16 & 0xff;
$g1 = $argb1 >> 8 & 0xff;
$b1 = $argb1 & 0xff;
$argb2 = imagecolorat($gd2, $x, $y);
$a2 = $argb2 >> 24 & 0x7f;
// 127 in hex. These are binary operations.
$r2 = $argb2 >> 16 & 0xff;
$g2 = $argb2 >> 8 & 0xff;
$b2 = $argb2 & 0xff;
$r1 /= 255;
$r2 /= 255;
if ($r1 < 0.5) {
$r3 = 2 * ($r1 * $r2);
} else {
$r3 = 1 - 2 * (1 - $r1) * (1 - $r2);
}
$g1 /= 255;
$g2 /= 255;
if ($g1 < 0.5) {
$g3 = 2 * ($g1 * $g2);
} else {
$g3 = 1 - 2 * (1 - $g1) * (1 - $g2);
}
$b1 /= 255;
$b2 /= 255;
if ($b1 < 0.5) {
$b3 = 2 * ($b1 * $b2);
} else {
$b3 = 1 - 2 * (1 - $b1) * (1 - $b2);
}
$reverse = 127 - $a2;
$reverse = round($reverse * $opacity);
$argb3 = imagecolorallocatealpha($canvas, $r3 * 255, $g3 * 255, $b3 * 255, 127 - $reverse);
imagesetpixel($canvas, $canvasX, $canvasY, $argb3);
}
}
return $canvas;
}