- /**
- * Image zoom watermark class
- *
- */
- class cls_photo
- {
- protected $waterrate = 0.2; //The ratio of the watermark icon on the image
- protected $width = 300; //The default width of the thumbnail
- protected $height = 200; //Default height of thumbnail
- protected $padding = 5; //Distance from watermark image to edge
- protected $water_mark = "./water.png";
- protected $water_mark_pos = 5; //Watermark image position ( 1=upper left corner, 2=upper right corner, 3=lower left corner, 4=lower right corner, 5 center)
- protected $watermode = 0; // 0 does not print watermark when thumbnail 1 prints watermark when thumbnail
- protected $magick_handle; //Picture operation handle
- protected $format = array ( 'jpg', 'gif', 'png', 'jpeg' ); // Picture file format limitation
- protected $smallpic_mode = 2; // Default mode 0 is not generated Thumbnail, 1 is crop scaling, 2 is proportional scaling, 3 is scaling fill mode
-
- /**
- * Set image parameters
- *
- * @param $arg Image parameters can be put into the array multiple times as follows
- * @param $protected parameter value
- * array(
- * 'waterrate'=>0.2,
- * 'water_mark '=>'./water.png',
- * 'water_mark_pos'=>4,
- * 'smallpic_mode'=>1
- * );
- * @return ture/false
- */
- public function set_args ( $arg,$val="" )
- {
- $params = array ( 'waterrate','water_mark','water_mark_pos','smallpic_mode','watermode','width','height' );
- if ( is_array ( $arg ) )
- {
- foreach ( $arg as $k => ;$v )
- {
- if ( in_array ( $k,$params ) )
- {
- $this->$k = $v;
- }
- }
- }
- else
- {
- if ( empty ( $val ) )
- {
- return false;
- }
- else
- {
- if ( in_array ( $arg,$params ) )
- {
- $this->$arg = $val;
- }
- }
- }
- return true;
- }
-
- /**
- * Image scaling
- *
- * @param $src_file source file path
- * @param $dst_file destination file path
- * @return thumbnail image path/false
- */
- public function scale ( $src_file,$dst_file="" )
- {
- $dst_width = $this->width;
- $dst_height = $this->height;
- $mode = $this->smallpic_mode;
- $magic_water_handle = NewMagickWand();
- if ( !MagicReadImage ( $magic_water_handle, $src_file ) ) return false;
-
- //Type
- $srcext = strtolower ( MagickGetImageFormat ( $magic_water_handle ) );
- if ( $srcext=='bmp' )
- {
- $srcext = 'jpeg';
- }
- if ( !in_array ( $srcext,$this->format ) ) return false;
- //size
- $src_width = MagickGetImageWidth ( $magic_water_handle );
- $src_height = MagickGetImageHeight ( $magic_water_handle );
-
- //Crop zoom mode
- if ( $mode == 1 )
- {
- $pos_x=$pos_y = 0; //Crop Cut temporary position
- $src_widthc = $src_width;//Cut temporary width
- $src_heightc = $src_height;//Cut temporary height
- if ( $src_width/$src_height>$dst_width/$dst_height )
- {
- $src_widthc = $ SRC_HEIGHT*$ dst_width /$ dst_height;
- $ POS_X = ($ src_width-$ src_widthc) /2; dst_height/$ dst_width;
- $ POS_Y = ($ SRC_HEIGHT -$src_heightc ) /2;
- }
- MagickCropImage ( $magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y );//Crop
- //Because after the MagickCropImage function, the Gif image changes, but the canvas remains unchanged
- $ this->magick_handle = NewMagickWand();
- MagickNewImage ( $this->magick_handle,$src_widthc,$src_heightc,'#ffffff' );
- MagickSetFormat ( $this->magick_handle,$srcext );
- MagickCompositeImage ( $ this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0 );
- //Scale
- MagickScaleImage ( $this->magick_handle, $dst_width, $dst_height );
-
- }
- //Proportional scaling mode
- if ( $ mode == 2 )
- {
- if ( $src_width/$src_height>$dst_width/$dst_height )
- {
- $dst_height=$dst_width*$src_height/$src_width;
- }
- else
- {
- $dst_width=$dst_height * $src_width/$src_height;
- }
- $this->magick_handle=$magic_water_handle;//Replace
- MagickScaleImage ( $this->magick_handle, $dst_width, $dst_height );//Scale
- }
- //Scale fill mode
- if ( $mode == 3 )
- {
- if ( $src_width/$src_height>$dst_width/$dst_height )
- {
- $dst_heightc=$dst_width*$src_height/$src_width;
- $dst_widthc=$dst_width;
- }
- else
- {
- $dst_widthc=$dst_height*$src_width/$src_height;
- $dst_heightc=$dst_height;
- }
- MagickScaleImage ( $magic_water_handle, $dst_widthc, $dst_heightc );//缩放
- $this->magick_handle = NewMagickWand();
- MagickNewImage ( $this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor );
- MagickSetFormat ( $this->magick_handle,$srcext );
- MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp, ( $dst_width-$dst_widthc ) /2, ( $dst_height-$dst_heightc ) /2 );
- }
- //打水印
- if ( $this->watermode == 1 )
- {
- $this->set_mark();
- }
- if ( empty ( $dst_file ) )
- {
- //建立临时文件
- $dst_file = tempnam ( $_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG" );
- }
- MagickWriteImage ( $this->magick_handle, $dst_file );
- return $dst_file;
- }
-
- /**
- * Watermarking
- *
- * @param $src_file The path of the image to be watermarked
- * @param $dst_file The file saving path for producing watermarks. If it is empty, a random temporary file will be produced
- * @return The path of the watermark file/false
- */
- public function water_mark ( $src_file,$dst_file="" )
- {
- $this->magick_handle = NewMagickWand();
- if ( !MagickReadImage ( $this->magick_handle, $src_file ) )
- return false;
- $this->set_mark();
- if ( empty ( $dst_file ) )
- {
- //建立临时文件
- $dst_file = tempnam ( $_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG" );
- }
- MagickWriteImage ( $this->magick_handle, $dst_file );
- return $dst_file;
- }
-
- /**
- * Internal interface
- * Watermark pictures
- *
- */
- protected function set_mark()
- {
-
- //尺寸
- $dst_width = MagickGetImageWidth ( $this->magick_handle );
- $dst_height = MagickGetImageHeight ( $this->magick_handle );
- //处理水印图
- if ( $this->water_mark && is_file ( $this->water_mark ) )
- {
- $magic_water_handle = NewMagickWand();
- MagickRemoveImage ( $magic_water_handle );
- if ( MagickReadImage ( $magic_water_handle, $this->water_mark ) )
- {
- MagickScaleImage ( $magic_water_handle, $dst_width*$this->waterrate, $dst_width*$this->waterrate*MagickGetImageHeight ( $magic_water_handle ) /MagickGetImageWidth ( $magic_water_handle ) );//缩放水印到图片的1/5
- if ( $this->water_mark_pos == 1 )
- {
- $left = $this->padding;
- $top = $this->padding;
- }
- elseif ( $this->water_mark_pos == 2 )
- {
- $left = $dst_width-$this->padding-MagickGetImageWidth ( $magic_water_handle );
- $top = $this->padding;
- }
- elseif ( $this->water_mark_pos == 3 )
- {
- $left = $this->padding;
- $top = $dst_height -$this->padding-MagickGetImageHeight ( $magic_water_handle );
- }
- elseif ( $this->water_mark_pos == 4 )
- {
- $left = $dst_width-$this->padding-MagickGetImageWidth ( $magic_water_handle );
- $top =$dst_height -$this->padding-MagickGetImageHeight ( $magic_water_handle );
- }
- elseif ( $this->water_mark_pos == 5 )
- {
- $left = ( $dst_width-MagickGetImageWidth ( $magic_water_handle ) ) /2;
- $top = ( $dst_height -MagickGetImageHeight ( $magic_water_handle ) ) /2;
- }
- MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top );
- }
- }
- }
- }
复制代码
|