protected function _do_watermark(Kohana_Image $image, $offset_x, $offset_y, $opacity) { // Convert the Image intance into an Imagick instance $watermark = new Imagick(); $watermark->readImageBlob($image->render(), $image->file); if ($watermark->getImageAlphaChannel() !== Imagick::ALPHACHANNEL_ACTIVATE) { // Force the image to have an alpha channel $watermark->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE); } if ($opacity < 100) { // NOTE: Using setImageOpacity will destroy current alpha channels! $watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity / 100, Imagick::CHANNEL_ALPHA); } // Match the colorspace between the two images before compositing // $watermark->setColorspace($this->im->getColorspace()); // Apply the watermark to the image return $this->im->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, $offset_x, $offset_y); }
/** * Execute a watermarking. * * @param Kohana_Image $image watermarking Kohana_Image * @param integer $offset_x offset from the left * @param integer $offset_y offset from the top * @param integer $opacity opacity of watermark * @return void */ protected function _do_watermark(Kohana_Image $watermark, $offset_x, $offset_y, $opacity) { if (empty(Image_GD::$_available_functions[Image_GD::IMAGELAYEREFFECT])) { throw new ErrorException('This method requires imagelayereffect, which is only available in the bundled version of GD'); } // Loads image if not yet loaded $this->_load_image(); // Create the watermark image resource $overlay = imagecreatefromstring($watermark->render()); imagesavealpha($overlay, TRUE); // Get the width and height of the watermark $width = imagesx($overlay); $height = imagesy($overlay); if ($opacity < 100) { // Convert an opacity range of 0-100 to 127-0 $opacity = round(abs($opacity * 127 / 100 - 127)); // Allocate transparent gray $color = imagecolorallocatealpha($overlay, 127, 127, 127, $opacity); // The transparent image will overlay the watermark imagelayereffect($overlay, IMG_EFFECT_OVERLAY); // Fill the background with the transparent color imagefilledrectangle($overlay, 0, 0, $width, $height, $color); } // Alpha blending must be enabled on the background! imagealphablending($this->_image, TRUE); if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height)) { // Destroy the overlay image imagedestroy($overlay); } }