public function watermark($file, $offsetX = 0, $offsetY = 0, $opacity = 1.0)
{
$file = $this->alias->resolve($file);
$maskImageInfo = getimagesize($file);
$maskWidth = $maskImageInfo[0];
/** @noinspection MultiAssignmentUsageInspection */
$maskHeight = $maskImageInfo[1];
/** @noinspection MultiAssignmentUsageInspection */
$maskType = $maskImageInfo[2];
if ($maskType === IMAGETYPE_GIF) {
$maskImage = imagecreatefromgif($file);
} elseif ($maskType === IMAGETYPE_JPEG) {
$maskImage = imagecreatefromjpeg($file);
} elseif ($maskType === IMAGETYPE_PNG) {
$maskImage = imagecreatefrompng($file);
} else {
throw new GdException('Installed GD does not support such images');
}
imagesavealpha($maskImage, true);
$image = imagecreatetruecolor($this->_width, $this->_height);
imagealphablending($image, false);
imagesavealpha($image, true);
if ($maskType !== IMAGETYPE_PNG) {
$filedColor = imagecolorallocatealpha($image, 127, 127, 127, (1 - $opacity) * 127);
} else {
$filedColor = imagecolorallocate($image, 127, 127, 127);
}
imagelayereffect($maskImage, IMG_EFFECT_OVERLAY);
imagefilledrectangle($maskImage, 0, 0, $maskWidth, $maskHeight, $filedColor);
imagealphablending($this->_image, true);
imagecopy($this->_image, $maskImage, $offsetX, $offsetY, 0, 0, $maskWidth, $maskHeight);
return $this;
}