Ejemplo n.º 1
0
 private function watermarkPrint_imagick($src, $watermark, $dest_x, $dest_y, $quality, $transparency, $watermarkImgInfo)
 {
     try {
         // Open the original image
         $img = new Imagick($src);
         // Open the watermark
         $watermark = new Imagick($watermark);
         // Set transparency
         if (strtoupper($watermark->getImageFormat()) !== 'PNG') {
             $watermark->setImageOpacity($transparency / 100);
         }
         // Overlay the watermark on the original image
         $img->compositeImage($watermark, imagick::COMPOSITE_OVER, $dest_x, $dest_y);
         // Set quality
         if (strtoupper($img->getImageFormat()) === 'JPEG') {
             $img->setImageCompression(imagick::COMPRESSION_JPEG);
             $img->setCompressionQuality($quality);
         }
         $result = $img->writeImage($src);
         $img->clear();
         $img->destroy();
         $watermark->clear();
         $watermark->destroy();
         return $result ? true : false;
     } catch (Exception $e) {
         return false;
     }
 }
Ejemplo n.º 2
0
 protected function _watermark($filename, $position, $padding = 5)
 {
     extract(parent::_watermark($filename, $position, $padding));
     $wmimage = new \Imagick();
     $wmimage->readImage($filename);
     $wmimage->setImageOpacity($this->config['watermark_alpha'] / 100);
     $this->imagick->compositeImage($wmimage, \Imagick::COMPOSITE_DEFAULT, $x, $y);
 }
Ejemplo n.º 3
0
 /**
  * Overlay an image onto the current image.
  *
  * @param  string $image
  * @param  int    $x
  * @param  int    $y
  * @return Imagick
  */
 public function overlay($image, $x = 0, $y = 0)
 {
     $overlayImage = new \Imagick($image);
     if ($this->opacity < 1) {
         $overlayImage->setImageOpacity($this->opacity);
     }
     $this->image->resource()->compositeImage($overlayImage, $this->overlay, $x, $y);
     return $this;
 }
Ejemplo n.º 4
0
 public function watermark($waterImage, $pos = 5, $Opacity = 0.2)
 {
     $water = new \Imagick($waterImage);
     $water->setImageOpacity($Opacity);
     $dw = new \ImagickDraw();
     $dw->setGravity($pos);
     $dw->composite($water->getImageCompose(), 0, 0, 50, 0, $water);
     $this->_handle->drawImage($dw);
     $water->destroy();
     $dw->destroy();
 }
 /**
  * Crops Image.
  *
  * @since 3.5.0
  * @access public
  *
  * @param string|int $src The source file or Attachment ID.
  * @param int $src_x The start x position to crop from.
  * @param int $src_y The start y position to crop from.
  * @param int $src_w The width to crop.
  * @param int $src_h The height to crop.
  * @param int $dst_w Optional. The destination width.
  * @param int $dst_h Optional. The destination height.
  * @param boolean $src_abs Optional. If the source crop points are absolute.
  * @return boolean|WP_Error
  */
 public function crop($src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false)
 {
     $ar = $src_w / $src_h;
     $dst_ar = $dst_w / $dst_h;
     if (isset($_GET['pte-fit-crop-color']) && abs($ar - $dst_ar) > 0.01) {
         PteLogger::debug(sprintf("IMAGICK - AR: '%f'\tOAR: '%f'", $ar, $dst_ar));
         // Crop the image to the correct aspect ratio...
         if ($dst_ar > $ar) {
             // constrain to the dst_h
             $tmp_dst_h = $dst_h;
             $tmp_dst_w = $dst_h * $ar;
             $tmp_dst_y = 0;
             $tmp_dst_x = $dst_w / 2 - $tmp_dst_w / 2;
         } else {
             $tmp_dst_w = $dst_w;
             $tmp_dst_h = $dst_w / $ar;
             $tmp_dst_x = 0;
             $tmp_dst_y = $dst_h / 2 - $tmp_dst_h / 2;
         }
         //$color = this::getImagickPixel( $_GET['pte-fit-crop-color'] );
         if (preg_match("/^#[a-fA-F0-9]{6}\$/", $_GET['pte-fit-crop-color'])) {
             $color = new ImagickPixel($_GET['pte-fit-crop-color']);
         }
         //else {
         //    PteLogger::debug( "setting transparent/white" );
         //    $color = new ImagickPixel( 'white' );
         //    //$color->setColorValue( Imagick::COLOR_ALPHA, 0 );
         //}
         try {
             // crop the original image
             $this->image->cropImage($src_w, $src_h, $src_x, $src_y);
             $this->image->scaleImage($tmp_dst_w, $tmp_dst_h);
             // Create a new image and then compose the old one onto it.
             $img = new Imagick();
             $img->newImage($dst_w, $dst_h, isset($color) ? $color : 'white');
             $img->setImageFormat($this->image->getImageFormat());
             if (!isset($color)) {
                 $img->setImageOpacity(0.0);
             }
             $img->compositeImage($this->image, Imagick::COMPOSITE_DEFAULT, $tmp_dst_x, $tmp_dst_y);
             $img->flattenImages();
             $this->image = $img;
         } catch (Exception $e) {
             return new WP_Error('image_crop_error', __('Image crop failed.'), $this->file);
         }
         return $this->update_size();
     }
     return parent::crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
 }
Ejemplo n.º 6
0
 public function watermark($file, $mark_image, $set)
 {
     $image = new Imagick();
     $image->readImage($file);
     $watermark = new Imagick();
     $watermark->readImage($mark_image);
     $opacity = isset($set['wm_opacity']) ? (int) $set['wm_opacity'] : 100;
     $watermark->setImageOpacity($opacity / 100);
     $image->compositeImage($watermark, imagick::COMPOSITE_DEFAULT, $set['dest_x'], $set['dest_y']);
     $image->writeImage();
     $image->clear();
     $image->destroy();
     $watermark->clear();
     $watermark->destroy();
 }
 /**
  * Crops Image.
  *
  * @since 3.5.0
  * @access public
  *
  * @param string|int $src The source file or Attachment ID.
  * @param int $src_x The start x position to crop from.
  * @param int $src_y The start y position to crop from.
  * @param int $src_w The width to crop.
  * @param int $src_h The height to crop.
  * @param int $dst_w Optional. The destination width.
  * @param int $dst_h Optional. The destination height.
  * @param boolean $src_abs Optional. If the source crop points are absolute.
  * @return boolean|WP_Error
  */
 public function crop($src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false)
 {
     if (pte_is_crop_border_enabled($src_w, $src_h, $dst_w, $dst_h)) {
         // Crop the image to the correct aspect ratio...
         $ar = $src_w / $src_h;
         $dst_ar = $dst_w / $dst_h;
         if ($dst_ar > $ar) {
             // constrain to the dst_h
             $tmp_dst_h = $dst_h;
             $tmp_dst_w = $dst_h * $ar;
             $tmp_dst_y = 0;
             $tmp_dst_x = $dst_w / 2 - $tmp_dst_w / 2;
         } else {
             $tmp_dst_w = $dst_w;
             $tmp_dst_h = $dst_w / $ar;
             $tmp_dst_x = 0;
             $tmp_dst_y = $dst_h / 2 - $tmp_dst_h / 2;
         }
         if (pte_is_crop_border_opaque()) {
             $color = new ImagickPixel($_GET['pte-fit-crop-color']);
         }
         try {
             // crop the original image
             $this->image->cropImage($src_w, $src_h, $src_x, $src_y);
             $this->image->scaleImage($tmp_dst_w, $tmp_dst_h);
             // Create a new image and then compose the old one onto it.
             $img = new Imagick();
             $img->newImage($dst_w, $dst_h, isset($color) ? $color : 'white');
             $img->setImageFormat($this->image->getImageFormat());
             if (!isset($color)) {
                 $img->setImageOpacity(0.0);
             }
             $img->compositeImage($this->image, Imagick::COMPOSITE_DEFAULT, $tmp_dst_x, $tmp_dst_y);
             $img->flattenImages();
             $this->image = $img;
         } catch (Exception $e) {
             return new WP_Error('image_crop_error', __('Image crop failed.'), $this->file);
         }
         return $this->update_size();
     }
     return parent::crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
 }
 public function run($file)
 {
     $image = new Imagick();
     $image->readImage($file);
     $watermark = new Imagick();
     $watermark->readImage($this->settings['overlay_path']);
     if (isset($this->settings['opacity']) === true && $this->settings['opacity'] != false) {
         $watermark->setImageOpacity($this->settings['opacity']);
     }
     /*
     		// how big are the images?
     		$iWidth = $image->getImageWidth();
     		$iHeight = $image->getImageHeight();
     		$wWidth = $watermark->getImageWidth();
     		$wHeight = $watermark->getImageHeight();
     
     		if ($iHeight < $wHeight || $iWidth < $wWidth) {
     		    // resize the watermark
     		    $watermark->scaleImage($iWidth, $iHeight);
     
     		    // get new size
     		    $wWidth = $watermark->getImageWidth();
     		    $wHeight = $watermark->getImageHeight();
     		}
     
     		// calculate the position
     		$x = ($iWidth - $wWidth) / 2;
     		$y = ($iHeight - $wHeight) / 2;
     */
     $image->compositeImage($watermark, imagick::COMPOSITE_OVER, $this->settings['horizontal_offset'], $this->settings['vertical_offset']);
     $image->writeImage($file);
     $image->clear();
     $image->destroy();
     $watermark->clear();
     $watermark->destroy();
     return TRUE;
 }
Ejemplo n.º 9
0
 public function watermark($image = false, $parms = false)
 {
     if (!$image) {
         $image = CMS_Images::$default_watermark_image;
     }
     if (!$parms) {
         $parms = CMS_Images::$default_watermark_parms;
     }
     if (is_string($image)) {
         $image = preg_replace('{^/?}', '', $image);
         $watermark = new Imagick($image);
         list($w, $h) = CMS_Images::size($image);
     } elseif (is_object($image) && $image instanceof CMS_Image_Imagick) {
         $watermark =& $image->ih;
         $w = $image->width();
         $h = $image->height();
     }
     $_w = $this->width();
     $_h = $this->height();
     list($nl, $nt, $opacity) = $this->watermark_parms($parms, $_w, $_h, $w, $h);
     if ($opacity != 1) {
         $watermark->setImageOpacity($opacity);
     }
     $this->ih->compositeImage($watermark, Imagick::COMPOSITE_ATOP, $nl, $nt);
     return $this;
 }
Ejemplo n.º 10
0
 function waterMarkImg()
 {
     if (empty($this->param['water_mark_image']) || !file_exists($this->param['water_mark_image'])) {
         return false;
     }
     $bg_h = $this->getHeight();
     $bg_w = $this->getWidth();
     $water_img = new Imagick($this->param['water_mark_image']);
     $water_h = $water_img->getImageHeight();
     $water_w = $water_img->getImageWidth();
     //if($bg_h < $water_h || $bg_w < $water_w )
     //{
     //   return false;
     //}
     if ($this->param['water_mark_opacity']) {
         $water_img->setImageOpacity($this->param['water_mark_opacity'] / 100);
     }
     $draw = new ImagickDraw();
     switch ($this->param['water_mark_pos']) {
         case 0:
         case 1:
             $gravity = Imagick::GRAVITY_NORTHWEST;
             //'NorthWest';
             break;
         case 2:
             $gravity = Imagick::GRAVITY_NORTH;
             //'North';
             break;
         case 3:
             $gravity = Imagick::GRAVITY_NORTHEAST;
             //'NorthEast';
             break;
         case 4:
             $gravity = Imagick::GRAVITY_WEST;
             //'West';
             break;
         case 5:
             $gravity = Imagick::GRAVITY_CENTER;
             //'Center';
             break;
         case 6:
             $gravity = Imagick::GRAVITY_EAST;
             //'East';
             break;
         case 7:
             $gravity = Imagick::GRAVITY_SOUTHWEST;
             //'SouthWest';
             break;
         case 8:
             $gravity = Imagick::GRAVITY_SOUTH;
             //'South';
             break;
         case 9:
             $gravity = Imagick::GRAVITY_SOUTHEAST;
             break;
     }
     $draw->setGravity($gravity);
     $draw->composite($water_img->getImageCompose(), 0, 0, 50, 0, $water_img);
     if ($this->image_type == 'GIF') {
         $color_transparent = new ImagickPixel("transparent");
         //透明色
         $dest = new Imagick();
         foreach ($this->image as $frame) {
             $page = $frame->getImagePage();
             $tmp = new Imagick();
             $tmp->newImage($page['width'], $page['height'], $color_transparent, 'gif');
             $tmp->compositeImage($frame, Imagick::COMPOSITE_OVER, $page['x'], $page['y']);
             $tmp->drawImage($draw);
             $dest->addImage($tmp);
             $dest->setImagePage($tmp->getImageWidth(), $tmp->getImageHeight(), 0, 0);
             $dest->setImageDelay($frame->getImageDelay());
             $dest->setImageDispose($frame->getImageDispose());
         }
         $dest->coalesceImages();
         $this->image->destroy();
         $this->image = $dest;
     } else {
         $this->image->drawImage($draw);
     }
 }
Ejemplo n.º 11
0
if (isPost()) {
    // Get post parameters
    $originX = post('originX');
    $originY = post('originY');
    $transparency = post('transparency');
    $watermarkResize = post('markMin');
    $originalImagePath = post('originalImage');
    $watermarkImagePath = post('watermarkImage');
    // Open images
    $original = new Imagick($originalImagePath);
    $watermark = new Imagick($watermarkImagePath);
    // Set opacity for images with alpha channel and without
    if ($watermark->getImageAlphaChannel()) {
        $watermark->evaluateImage(Imagick::EVALUATE_DIVIDE, 1.0 / $transparency, Imagick::CHANNEL_ALPHA);
    } else {
        $watermark->setImageOpacity($transparency);
    }
    // Resize image
    if ($watermarkResize > 1) {
        $watermark->resizeImage($watermark->getImageWidth() / $watermarkResize, $watermark->getImageHeight() / $watermarkResize, Imagick::FILTER_LANCZOS, 1);
    }
    // Watermark block
    $isPattern = post('isPattern');
    if (bool($isPattern)) {
        // Get watermark`s offset from original image
        $initialX = (int) str_replace('px', '', post('x'));
        $initialY = (int) str_replace('px', '', post('y'));
        // Get watermark sizes
        $watermarkWidth = $watermark->getImageWidth();
        $watermarkHeight = $watermark->getImageHeight();
        // Get original image sizes
Ejemplo n.º 12
0
 /**
  * Does a copy merge of two image resources
  *
  * @param Imagick $dst_im
  * @param Imagick $src_im
  * @param int $dst_x
  * @param int $dst_y
  * @param int $src_x
  * @param int $src_y
  * @param int $src_w
  * @param int $src_h
  * @param int $pct
  * @return bool
  */
 function zp_imageMerge($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
 {
     $src_im->cropImage($src_w, $src_h, $src_x, $src_y);
     $src_im->setImageOpacity($pct / 100);
     return $dst_im->compositeImage($src_im, Imagick::COMPOSITE_OVER, $dst_x, $dst_y);
 }
Ejemplo n.º 13
0
 /**
  * 加给图片加水印
  * @param string $groundImage 要加水印地址
  * @param int $waterPos 水印位置
  * @param string $waterImage 水印图片地址
  * @param string $waterText 文本文字
  * @param int $textFont 文字大小
  * @param string $textColor 文字颜色
  * @param int $minWidth 小于此值不加水印
  * @param int $minHeight 小于此值不加水印
  * @param float $alpha 透明度
  * @return FALSE
  */
 public static function addWaterMark($groundImage, $waterPos = 0, $waterImage = "", $waterText = "", $textFont = 15, $textColor = "#FF0000", $minWidth = 100, $minHeight = 100, $alpha = 0.9)
 {
     if (!class_exists('\\Imagick', false)) {
         return self::addWaterMark2($groundImage, $waterPos, $waterImage, $waterText, $textFont, $textColor, $minWidth, $minHeight, $alpha);
     }
     if (empty($waterText) and !is_file($waterImage)) {
         return false;
     }
     $bg = null;
     $bg_h = $bg_w = $water_h = $water_w = 0;
     //获取背景图的高,宽
     if (is_file($groundImage) && !empty($groundImage)) {
         $bg = new \Imagick();
         $bg->readImage($groundImage);
         $bg_h = $bg->getImageHeight();
         $bg_w = $bg->getImageWidth();
     }
     //获取水印图的高,宽
     $water = new \Imagick($waterImage);
     $water_h = $water->getImageHeight();
     $water_w = $water->getImageWidth();
     //如果背景图的高宽小于水印图的高宽或指定的高和宽则不加水印
     if ($bg_h < $minHeight || $bg_w < $minWidth || $bg_h < $water_h || $bg_w < $water_w) {
         return false;
     }
     //加水印
     $dw = new \ImagickDraw();
     //加图片水印
     if (is_file($waterImage)) {
         $water->setImageOpacity($alpha);
         $dw->setGravity($waterPos);
         $dw->composite($water->getImageCompose(), 0, 0, 50, 0, $water);
         $bg->drawImage($dw);
         if (!$bg->writeImage($groundImage)) {
             return false;
         }
     } else {
         //加文字水印
         $dw->setFontSize($textFont);
         $dw->setFillColor($textColor);
         $dw->setGravity($waterPos);
         $dw->setFillAlpha($alpha);
         $dw->annotation(0, 0, $waterText);
         $bg->drawImage($dw);
         if (!$bg->writeImage($groundImage)) {
             return false;
         }
     }
     return true;
 }
Ejemplo n.º 14
0
 /**
  * resize an image
  *
  * @param string  $from      file to convert
  * @param string  $to        convert to what
  * @param int     $width     width to convert to
  * @param int     $height    height to convert to
  * @param boolean $keepratio keep image ratio, or force an exact resize
  *
  * @return null
  */
 static function resize($from, $to, $width, $height, $keepratio = true)
 {
     if (!file_exists($from) && @fopen($from, 'r') != true) {
         return false;
     }
     switch (@$GLOBALS['DBVARS']['graphics-method']) {
         case 'imagick':
             // {
             $thumb = new Imagick();
             $thumb->read($from);
             $thumb->setImageOpacity(1.0);
             $thumb->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1, true);
             $thumb->writeImage($to);
             $thumb->clear();
             $thumb->destroy();
             break;
             // }
         // }
         default:
             // { fallback to GD
             $extFrom = CoreGraphics::getType($from);
             switch (preg_replace('/.*\\./', '', $to)) {
                 case 'png':
                     // {
                     $extTo = 'png';
                     break;
                     // }
                 // }
                 default:
                     $extTo = 'jpeg';
             }
             $size = getimagesize($from);
             if ($size === false) {
                 return false;
             }
             $load = 'imagecreatefrom' . $extFrom;
             $save = 'image' . $extTo;
             if (!function_exists($load) || !function_exists($save)) {
                 return false;
             }
             if (strpos($from, '/') !== 0) {
                 // external image
                 $tmp = USERBASE . '/ww.cache/' . md5($from) . '.' . $extFrom;
                 if (!file_exists($tmp)) {
                     copy($from, $tmp);
                 }
                 $im = $load($tmp);
                 unlink($tmp);
             } else {
                 $im = $load($from);
             }
             if ($keepratio) {
                 $multx = $size[0] / $width;
                 $multy = $size[1] / $height;
                 if ($multx > $multy) {
                     $mult = $multx;
                 } else {
                     $mult = $multy;
                 }
                 $width = $size[0] / $mult;
                 $height = $size[1] / $mult;
             }
             $imresized = imagecreatetruecolor($width, $height);
             imagealphablending($imresized, false);
             imagecopyresampled($imresized, $im, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
             imagesavealpha($imresized, true);
             $save($imresized, $to, $extTo == 'jpeg' ? 100 : 9);
             imagedestroy($imresized);
             imagedestroy($im);
             // }
     }
     return true;
 }
Ejemplo n.º 15
0
	/**
	 * Apply watermark to image.
	 *
	 * @param	int		$attachment_id	Attachment ID
	 * @param	string	$image_path		Path to the file
	 * @param	string	$image_size		Image size
	 * @param	array	$upload_dir		Upload media data
	 * @return	void
	 */
	public function do_watermark( $attachment_id, $image_path, $image_size, $upload_dir ) {
		$options = apply_filters( 'iw_watermark_options', $this->options );

		// get image mime type
		$mime = wp_check_filetype( $image_path );

		// get watermark path
		$watermark_file = wp_get_attachment_metadata( $options['watermark_image']['url'], true );
		$watermark_path = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $watermark_file['file'];

		// imagick extension
		if ( $this->extension === 'imagick' ) {
			// create image resource
			$image = new Imagick( $image_path );

			// create watermark resource
			$watermark = new Imagick( $watermark_path );

			// set transparency
			$image->setImageOpacity( round( 1 - (float)( $options['watermark_image']['transparent'] / 100 ), 2 ) );

			// set compression quality
			if ( $mime['type'] === 'image/jpeg' ) {
				$image->setImageCompressionQuality( $options['watermark_image']['quality'] );
				$image->setImageCompression( imagick::COMPRESSION_JPEG );
			} else
				$image->setImageCompressionQuality( $options['watermark_image']['quality'] );

			// set image output to progressive
			if ( $options['watermark_image']['jpeg_format'] === 'progressive' )
				$image->setImageInterlaceScheme( Imagick::INTERLACE_PLANE );

			// get image dimensions
			$image_dim = $image->getImageGeometry();

			// get watermark dimensions
			$watermark_dim = $watermark->getImageGeometry();

			// calculate watermark new dimensions
			list( $width, $height ) = $this->calculate_watermark_dimensions( $image_dim['width'], $image_dim['height'], $watermark_dim['width'], $watermark_dim['height'], $options );

			// resize watermark
			$watermark->resizeImage( $width, $height, imagick::FILTER_CATROM, 1 );

			// calculate image coordinates
			list( $dest_x, $dest_y ) = $this->calculate_image_coordinates( $image_dim['width'], $image_dim['height'], $width, $height, $options );

			// combine two images together
			$image->compositeImage( $watermark, Imagick::COMPOSITE_OVERLAY, $dest_x, $dest_y, Imagick::CHANNEL_ALL );

			// save watermarked image
			$image->writeImage( $image_path );

			// clear image memory
			$image->clear();
			$image->destroy();
			$image = null;

			// clear watermark memory
			$watermark->clear();
			$watermark->destroy();
			$watermark = null;
		// gd extension
		} else {
			// get image resource
			$image = $this->get_image_resource( $image_path, $mime['type'] );

			if ( $image !== false ) {
				// add watermark image to image
				$image = $this->add_watermark_image( $image, $options, $upload_dir );

				if ( $image !== false ) {
					// save watermarked image
					$this->save_image_file( $image, $mime['type'], $image_path, $options['watermark_image']['quality'] );

					// clear watermark memory
					imagedestroy( $image );

					$image = null;
				}
			}
		}
	}
Ejemplo n.º 16
0
 /**
  * 加给图片加水印
  *
  * @param strimg $groundImage 要加水印地址
  * @param int $waterPos 水印位置
  * @param string $waterImage 水印图片地址
  * @param string $waterText 文本文字
  * @param int $textFont 文字大小
  * @param string $textColor 文字颜色
  * @param int $minWidth 小于此值不加水印
  * @param int $minHeight 小于此值不加水印
  * @param float $alpha 透明度
  * @return FALSE
  */
 public static function waterMark($groundImage, $waterPos = 0, $waterImage = "", $waterText = "", $textFont = 15, $textColor = "#FF0000", $minWidth = '100', $minHeight = '100', $alpha = 0.9)
 {
     $isWaterImg = FALSE;
     $bg_h = $bg_w = $water_h = $water_w = 0;
     //获取背景图的高,宽
     if (is_file($groundImage) && !empty($groundImage)) {
         $bg = new Imagick();
         $bg->readImage($groundImage);
         $bg_h = $bg->getImageHeight();
         $bg_w = $bg->getImageWidth();
     }
     //获取水印图的高,宽
     if (is_file($waterImage) && !empty($waterImage)) {
         $water = new Imagick($waterImage);
         $water_h = $water->getImageHeight();
         $water_w = $water->getImageWidth();
     }
     //如果背景图的高宽小于水印图的高宽或指定的高和宽则不加水印
     if ($bg_h < $minHeight || $bg_w < $minWidth || $bg_h < $water_h || $bg_w < $water_w) {
         return;
     } else {
         $isWaterImg = TRUE;
     }
     //加水印
     if ($isWaterImg) {
         $dw = new ImagickDraw();
         //加图片水印
         if (is_file($waterImage)) {
             $water->setImageOpacity($alpha);
             $dw->setGravity($waterPos);
             $dw->composite($water->getImageCompose(), 0, 0, 50, 0, $water);
             $bg->drawImage($dw);
             if (!$bg->writeImage($groundImage)) {
                 return FALSE;
             }
         } else {
             //加文字水印
             $dw->setFontSize($textFont);
             $dw->setFillColor($textColor);
             $dw->setGravity($waterPos);
             $dw->setFillAlpha($alpha);
             $dw->annotation(0, 0, $waterText);
             $bg->drawImage($dw);
             if (!$bg->writeImage($groundImage)) {
                 return FALSE;
             }
         }
     }
 }
Ejemplo n.º 17
0
 protected function _watermark($image, $offset_x, $offset_y, $opacity)
 {
     $opacity = $opacity / 100;
     $watermark = new \Imagick();
     $watermark->readImageBlob($image->render());
     $watermark->setImageOpacity($opacity);
     $this->_image->setIteratorIndex(0);
     while (true) {
         $this->_image->compositeImage($watermark, constant("Imagick::COMPOSITE_OVER"), $offset_x, $offset_y);
         if (!$this->_image->nextImage()) {
             break;
         }
     }
     $watermark->clear();
     $watermark->destroy();
 }
Ejemplo n.º 18
0
 /**
  * 添加图片水印 
  * 有个别水印图片打不漂亮;貌似对jpg图片打出来还不错
  * @see Lib_Image_Abstract::watermarkImage()
  * @param string $watermarkPath 水印图片(最好是透明背景的png图片)
  * @param string $place 水印位置;具体对应参数如下(可参见常量)
  *         northwest            northeast 
  *                     center
  *         southwest            southeast
  * @param integer $x 水印位置 ; x(水平坐标轴)轴偏移量
  * @param integer $y 水印位置 ; y(垂直坐标轴)轴偏移量
  * @param integer $alpha 水印透明度。取值0(全透明)-100(完全不透明) ;默认52:半透明
  * @param integer $angle 水印图片旋转角度  TODO
  * @return boolean
  */
 public function watermarkImage($watermarkPath, $place = 'southeast', $x = 18, $y = 18, $alpha = 52, $angle = 0)
 {
     if (!is_file($watermarkPath)) {
         $this->_error = '水印文件不存在';
         $this->_im = null;
         return false;
     }
     if (!$this->_isExistIm()) {
         return false;
     }
     try {
         $watermark = new Imagick();
         $watermark->readImage($watermarkPath);
         $alpha = $alpha / 100;
         $watermark->setImageOpacity($alpha);
         // 设置透明度,不知道为什么这里如果设了透明度,有些png水印打出来的图片就不漂亮了
         // 在ubuntu 64 位上还是一样
         $watermarkW = $watermark->getimagewidth();
         $watermarkH = $watermark->getimageheight();
         switch ($place) {
             case self::PLACE_NORTHWEST:
                 break;
             case self::PLACE_NORTHEAST:
                 $x = $this->_sourceWidth - ($watermarkW + $x);
                 break;
             case self::PLACE_CENTER:
                 $x = intval(($this->_sourceWidth - $watermarkW) / 2);
                 $y = intval(($this->_sourceHeight - $watermarkH) / 2);
                 break;
             case self::PLACE_SOUTHWEST:
                 $y = $this->_sourceHeight - ($watermarkH + $y);
                 break;
             case self::PLACE_SOUTHEAST:
                 $x = $this->_sourceWidth - ($watermarkW + $x);
                 $y = $this->_sourceHeight - ($watermarkH + $y);
                 break;
             default:
                 $x = $this->_sourceWidth - ($watermarkW + $x);
                 $y = $this->_sourceHeight - ($watermarkH + $y);
                 break;
         }
         $compose = $watermark->getImageCompose();
         // 方法一
         //return $this->_im->compositeimage($watermark, $compose, $x, $y);
         // 方法二
         $draw = new ImagickDraw();
         $draw->composite($compose, $x, $y, $watermarkW, $watermarkH, $watermark);
         if ($this->_sourceType == self::TYPE_GIF) {
             $this->_im = $this->_im->coalesceImages();
             $flag = true;
             foreach ($this->_im as $frame) {
                 $boolean = $frame->drawImage($draw);
                 if (!$boolean) {
                     $flag = false;
                     $this->_im = null;
                     $this->_error = '添加水印出错!';
                     break;
                 }
             }
             return $flag;
         }
         return $this->_im->drawImage($draw);
     } catch (Exception $e) {
         $this->_error = '处理水印图片出错';
         $this->_im = null;
         return false;
     }
 }
Ejemplo n.º 19
0
 /**
  * Draws the triangle strip for the signature
  *
  * @param string $hexColour Hexadecimal colour value for the whole card
  */
 public function drawTriangleStrip($hexColour)
 {
     // The base for the triangles strip, to be drawn over the plain
     $darkTriangles = isset($_GET['darktriangles']);
     $backArea = new ImagickDraw();
     $backArea->setFillColor(new ImagickPixel($hexColour));
     $backArea->rectangle(self::SIG_MARGIN + self::SIG_STROKE_WIDTH, self::SIG_MARGIN + self::SIG_STROKE_WIDTH + 1, $this->baseWidth - self::SIG_STROKE_WIDTH + self::SIG_STROKE_WIDTH / 2 + 1, self::TRIANGLE_STRIP_HEIGHT - self::SIG_STROKE_WIDTH + self::SIG_ROUNDING * 4);
     $this->canvas->drawImage($backArea);
     $originalTriangles = new Imagick(self::IMG_TRIANGLES);
     $originalTriangles->cropImage($this->baseWidth, $this->baseHeight, $this->baseWidth / 2, $this->baseHeight / 2);
     $triangles = new Imagick();
     $triangles->newImage($this->baseWidth - self::SIG_STROKE_WIDTH * 2 - 2, self::TRIANGLE_STRIP_HEIGHT + self::SIG_STROKE_WIDTH, new ImagickPixel($hexColour));
     $triangles = $triangles->textureImage($originalTriangles);
     // The gradient to draw over the triangles
     $trianglesGradient1 = new Imagick();
     $trianglesGradient1->newPseudoImage($this->baseWidth - self::SIG_STROKE_WIDTH * 2 - 2, self::TRIANGLE_STRIP_HEIGHT + self::SIG_STROKE_WIDTH, 'gradient:' . 'none' . '-' . $hexColour);
     $trianglesGradient1->setImageOpacity(0.6);
     // The second gradient to draw over the triangles
     $trianglesGradient2 = new Imagick();
     $trianglesGradient2->newPseudoImage($this->baseWidth - self::SIG_STROKE_WIDTH * 2 - 2, self::TRIANGLE_STRIP_HEIGHT + self::SIG_STROKE_WIDTH, 'gradient:' . '#4a4a4a' . '-' . '#313131');
     // Composite the black and white gradient onto the triangles
     $triangles->compositeImage($trianglesGradient2, Imagick::COMPOSITE_OVERLAY, 0, 0);
     $triangles->setImageOpacity($darkTriangles ? 0.2 : 0.1);
     // Composite the triangles onto the base
     $this->canvas->compositeImage($triangles, Imagick::COMPOSITE_DEFAULT, self::SIG_MARGIN + self::SIG_STROKE_WIDTH + 1, self::SIG_MARGIN + self::SIG_STROKE_WIDTH * 1.5);
     // Composite the triangles gradient onto the base
     $this->canvas->compositeImage($trianglesGradient1, Imagick::COMPOSITE_DEFAULT, self::SIG_MARGIN + self::SIG_STROKE_WIDTH + 1, self::SIG_MARGIN + self::SIG_STROKE_WIDTH * 1.5);
 }
Ejemplo n.º 20
0
 public function watermark($options)
 {
     // Make sure the resource handle is valid.
     if (!$this->isLoaded()) {
         throw new LogicException('No valid image was loaded.');
     }
     if ($options->opacity > 1) {
         $options->opacity = $options->opacity / 100;
     }
     if ($options->type == 'text') {
         if (isset($options->text)) {
             $this->watermarkText($options);
         }
     } else {
         if (isset($options->image)) {
             $watermark = new Imagick($options->image);
             $width = $this->getWidth();
             $height = $this->getHeight();
             $mw = $watermark->getImageWidth();
             $mh = $watermark->getImageHeight();
             switch ($options->position) {
                 default:
                 case 'center':
                     $x = floor(($width - $mw) / 2);
                     $y = floor(($height - $mh) / 2);
                     break;
                 case 'top-left':
                     $x = $options->margin;
                     $y = floor($mh / 2) + $options->margin;
                     break;
                 case 'top-right':
                     $x = $width - $mw - $options->margin;
                     $y = floor($mh / 2) + $options->margin;
                     break;
                 case 'center-left':
                     $x = 0 + $options->margin;
                     $y = floor(($height - $mh) / 2);
                     break;
                 case 'center-right':
                     $x = $width - $mw - $options->margin;
                     $y = floor(($height - $mh) / 2);
                     break;
                 case 'top-center':
                     $x = floor(($width - $mw) / 2);
                     $y = floor($mh / 2) + $options->margin;
                     break;
                 case 'bottom-center':
                     $x = floor(($width - $mw) / 2);
                     $y = $height - $mh - $options->margin;
                     break;
                 case 'bottom-left':
                     $x = 0 + $options->margin;
                     $y = $height - $mh - $options->margin;
                     break;
                 case 'bottom-right':
                     $x = $width - $mw - $options->margin;
                     $y = $height - $mh - $options->margin;
                     break;
             }
             if ($watermark->getImageFormat() == 'PNG') {
                 $watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, (double) $options->opacity, Imagick::CHANNEL_ALPHA);
             } else {
                 $watermark->setImageOpacity((double) $options->opacity);
             }
             $this->handle->compositeImage($watermark, imagick::COMPOSITE_OVER, $x, $y);
         }
     }
     return $this;
 }
Ejemplo n.º 21
0
 /**
  * Changes the image's opacity
  *
  * @param int $opacity
  *
  * @return Imagick
  */
 public function opacity($opacity)
 {
     $this->image->setImageOpacity($opacity);
     $this->operations[] = 'opacity' . $opacity;
     return $this;
 }
Ejemplo n.º 22
0
 /**
  * @param string $file
  * @param int    $offsetX
  * @param int    $offsetY
  * @param float  $opacity
  *
  * @return static
  * @throws \ManaPHP\Image\Adapter\Exception
  */
 public function watermark($file, $offsetX = 0, $offsetY = 0, $opacity = 1.0)
 {
     $watermark = new \Imagick($this->alias->resolve($file));
     if ($watermark->getImageAlphaChannel() === \Imagick::ALPHACHANNEL_UNDEFINED) {
         $watermark->setImageOpacity($opacity);
     }
     if ($watermark->getNumberImages() !== 1) {
         throw new ImagickException('not support multiple iterations: `:file`', ['file' => $file]);
     }
     if (!$this->_image->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY)) {
         throw new ImagickException('Imagick::compositeImage Failed');
     }
     $watermark->clear();
     $watermark->destroy();
     return $this;
 }
Ejemplo n.º 23
0
<?php

/* Create the object */
$image = new Imagick('images/Ok-icon.png');
/* Set the opacity */
$image->setImageOpacity(0.1);
/* output the image */
//header('Content-type: image/png');
//$image->getFilename();
//echo "prueba";
//instead
header('Content-type: image/png');
$image->setImageFormat("png");
$image->setImageOpacity(0.1);
//echo $image;
//phpinfo();
$fileDst = "images/test_1.png";
if ($f = fopen($fileDst, "w")) {
    $image->writeImageFile($f);
}
//echo $image;
Ejemplo n.º 24
0
 /**
  * Создание превью средствами imagick
  * @param $src_file исходный файл
  * @param $dst_file файл с результатом
  * @param max_w максимальная ширина
  * @param max_h максимальная высота
  * @return bool
  */
 private function image_constrain_imagick($src_file, $dst_file, $max_w, $max_h, $watermark = null, $watermark_offet_x = 0, $watermark_offet_y = 0, $watermark_opacity = 1, $sharpen = 0.2)
 {
     $thumb = new Imagick();
     // Читаем изображение
     if (!$thumb->readImage($src_file)) {
         return false;
     }
     // Размеры исходного изображения
     $src_w = $thumb->getImageWidth();
     $src_h = $thumb->getImageHeight();
     // Нужно ли обрезать?
     if (!$watermark && $src_w <= $max_w && $src_h <= $max_h) {
         // Нет - просто скопируем файл
         if (!copy($src_file, $dst_file)) {
             return false;
         }
         return true;
     }
     // Размеры превью при пропорциональном уменьшении
     list($dst_w, $dst_h) = $this->calc_contrain_size($src_w, $src_h, $max_w, $max_h);
     // Уменьшаем
     $thumb->thumbnailImage($dst_w, $dst_h);
     // Устанавливаем водяной знак
     if ($watermark && is_readable($watermark)) {
         $overlay = new Imagick($watermark);
         //$overlay->setImageOpacity(0.9);
         $overlay->setImageOpacity($watermark_opacity);
         $overlay_compose = $overlay->getImageCompose();
         // Get the size of overlay
         $owidth = $overlay->getImageWidth();
         $oheight = $overlay->getImageHeight();
         $watermark_x = min(($dst_w - $owidth) * $watermark_offet_x / 100, $dst_w);
         $watermark_y = min(($dst_h - $oheight) * $watermark_offet_y / 100, $dst_h);
     }
     // Анимированные gif требуют прохода по фреймам
     foreach ($thumb as $frame) {
         // Уменьшаем
         $frame->thumbnailImage($dst_w, $dst_h);
         /* Set the virtual canvas to correct size */
         $frame->setImagePage($dst_w, $dst_h, 0, 0);
         // Наводим резкость
         if ($sharpen > 0) {
             $thumb->adaptiveSharpenImage($sharpen, $sharpen);
         }
         if (isset($overlay) && is_object($overlay)) {
             $frame->compositeImage($overlay, $overlay_compose, $watermark_x, $watermark_y, imagick::COLOR_ALPHA);
         }
     }
     // Убираем комменты и т.п. из картинки
     $thumb->stripImage();
     //		$thumb->setImageCompressionQuality(100);
     // Записываем картинку
     if (!$thumb->writeImages($dst_file, true)) {
         return false;
     }
     // Уборка
     $thumb->destroy();
     if (isset($overlay) && is_object($overlay)) {
         $overlay->destroy();
     }
     return true;
 }
Ejemplo n.º 25
0
 case 0:
     echo "DNI NOT FOUND! {$file}\n";
     break;
 case 1:
     //Comprovar si existeix el camp jpegPhoto
     $info = ldap_get_entries($ds, $sr);
     //echo "Data for ".$info["count"]." items returned:<p>";
     echo " dn is: " . $info[0]["dn"] . " ";
     //echo "array: ". print_r($info[0]) ."\n";
     if (in_array("jpegphoto", $info[0])) {
         echo "PHOTO OK!\n";
     } else {
         echo "PHOTO NOT FOUND! Adding photo... ";
         if (class_exists('Imagick')) {
             $im = new Imagick($file);
             $im->setImageOpacity(1.0);
             //$im->resizeImage(147,200,Imagick::FILTER_UNDEFINED,0.5,TRUE);
             //$im->setCompressionQuality(90);
             $im->setImageFormat('jpeg');
             $attrs['jpegphoto'] = $im->getImageBlob();
         } else {
             echo "ERROR!";
         }
         $ret1 = ldap_mod_add($ds, $info[0]["dn"], $attrs);
         if ($ret1) {
             echo "PHOTO CORRECTLY ADDED: {$ret1}\n";
         } else {
             echo "Error adding photo: {$ret1}" . ldap_error($ds) . " \n";
         }
     }
     /*
Ejemplo n.º 26
0
function draw_figure($poster, $path, $y)
{
    $im = new Imagick($path);
    $im->modulateImage(100, 0, 100);
    $im->contrastImage(true);
    $im->contrastImage(true);
    $im->contrastImage(true);
    $im->gaussianBlurImage(5, 0.5);
    if ($im->getImageHeight() > 100) {
        $im->scaleImage($im->getImageWidth() * 100 / $im->getImageHeight(), 100);
    }
    if ($im->getImageWidth() > 125) {
        $im->scaleImage(125, $im->getImageHeight() * 125 / $im->getImageWidth());
    }
    $noise_layer2 = new Imagick();
    $noise_layer2->newImage($im->getImageWidth(), $im->getImageHeight(), 'none', 'png');
    $noise_layer2->addNoiseImage(imagick::NOISE_RANDOM);
    $noise_layer2->setImageOpacity(0.5);
    $noise_layer1 = new Imagick();
    $noise_layer1->newImage($im->getImageWidth(), $im->getImageHeight(), 'none', 'png');
    $noise_layer1->addNoiseImage(imagick::NOISE_RANDOM);
    $noise_layer1->modulateImage(100, 0, 100);
    $noise_layer1->setImageOpacity(0.3);
    $x = ($poster->getImageWidth() - $im->getImageWidth()) / 2;
    $poster->compositeImage($im, imagick::COMPOSITE_COLORBURN, $x, $y);
    $poster->compositeImage($noise_layer1, imagick::COMPOSITE_LIGHTEN, $x, $y);
    $poster->compositeImage($noise_layer2, imagick::COMPOSITE_SOFTLIGHT, $x, $y);
}