- /**
- * 圖片縮放水印類
- *
- */
- class cls_photo
- {
- protected $waterrate = 0.2; //水印圖示在圖片上的比例
- protected $width = 300; //縮圖預設寬度
- protected $height = 200; //縮圖預設高度
- protected $padding = 5; //水印圖到邊的距離
- protected $water_mark = "./water.png";
- protected $water_mark_pos = 5;//水印圖片位置(1=左上角,2=右上角,3=左下角,4=右下角,5中央)
- protected $watermode = 0;// 0縮圖時不打浮水印1縮圖時打水印
- protected $magick_handle;//圖片操作句柄
- protected $format = array ( 'jpg','gif', 'png','jpeg' ); // 圖片檔案格式限定
- protected $smallpic_mode = 2;//預設模式0為不產生縮圖,1為裁切縮放,2為比例縮放3為縮放填滿模式
-
- /**
- * 設定圖片類別參數
- *
- * @param $arg 圖片參數多次可放入數組裡如下
- * @param $protected 參數值
- * 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 ) )
- {
- for ( $arg as $k =>$v )
- {
- if ( in_array ( $k,$params ) )
- {
- $this->$k = $v;
- }
- }
- }
- else
- {
- if ( empty ( $val ) )
- {
- return false;
- }
- else
- {
- if ( in_array ( $arg_array ($. params ) )
- {
- $this->$arg = $val;
- }
- }
- }
- return true;
- }
-
- /* *
- * 圖片縮放
- *
- * @param $src_file 來源檔案路徑
- * @param $dst_file 目標檔案路徑
- * @return 縮寫圖片路徑/false
- */
- public function scale ( $src_file,$dst_file="" )
- {
- $dst_width = $this->width;
- $dst_height = $this-this>height;
- $mode = $this->smallpic_mode;
- $magic_water_handle = NewMagickWand();
- if ( !MagickReadImage ( $magic_water_handle, $src_file ) ) return false;
- $srcext = strtolower ( MagickGetImageFormat ( $magic_water_handle ) );
- if ( $srcext=='bmp' )
- {
- $srcext = 'jpeg'; }format ) ) return false;
- //尺寸
- $src_width = MagickGetImageWidth ( $magic_water_handle );
- $src_heightWidth ( $magic_water_handle );
- $src_heightage = Magic.Imhage); 🎜> //裁切縮放模式
- if ( $mode == 1 )
- {
- $pos_x=$pos_y = 0;//裁切暫存位置
- $src_widthc = $src_width; /裁切臨時寬度
- $src_heightc = $src_height;//裁切臨時高度
- if ( $src_width/$src_height>$dst_width/$dst_height )
- {
- $height🎜 $dst_width/$dst_height;
- $pos_x = ( $src_width-$src_widthc ) /2;
-
- }
- else
- {
- $wheight dst_width;
- $pos_y = ( $src_height-$src_heightc ) /2;
- }
- MagickCropImage ( $magic_water_handle,$src_widthc,$src_heightc,$pos_water_handle,$src_widthc,$src_heightc,$pos_heightc,$posx,$/pos; //因為MagickCropImage函數後,Gif 影像改,但畫布不變
- $this->magick_handle = NewMagickWand();
- MagickNewImage ( $this->magick_handle,$src_widthc,$src_heffightc,'#ffffight' ) ;
- MagickSetFormat ( $this->magick_handle,$srcext );
- MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0 );
- ->magick_handle, $dst_width, $dst_height );
-
- }
- //比例縮放模式
- if ( $mode == 2 )
- {
- if ( $src_width/$ src_height>$dst_width/$dst_height )
- {
- $dst_height=$dst_width*$src_height/$src_width;
- }
- else }
- $this->magick_handle=$magic_water_handle;//替換
- MagickScaleImage ( $this->magick_handle, $dst_width, $dst_height ); > //縮放填滿模式
- if ( $mode == 3 )
- {
- if ( $src_width/$src_height>$dst_width/$dst_height )
- {
- $dst_height *$src_height/$src_width;
- $dst_widthc=$dst_width;
- }
- else
- {
- $dst_widthc=$dst_height*🎜> {
- $dst_widthc=$dst_height*$src_height* dst_height;
- }
- MagickScaleImage ( $magic_water_handle, $dst_widthc, $dst_heightc );//縮放
- $this->magick_handle = NewMagickWand();
- MagickNewImage ($this-dth_handwidw ->smallpic_bgcolor );
- MagickSetFormat ( $this->magick_handle,$srcext );
- MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverpositeOp, (Comst_Op. $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;
- }
-
- /**
- * 打浮水印
- *
- * @param $src_file 要打浮水印的圖片路徑
- * @param $dst_file 生產浮水印的檔案儲存路徑,為空則生產隨機暫存檔案
- * @return 浮水印檔案路徑/false
- function water_mark ( $src_file,$dst_file="" )
- {
- $this->magick_handle = NewMagickWand();
- if ( !MagickReadImage ( $this->magick_handle, $src_handle, $src_ ) ) return false;
- $this->set_mark();
- if (empty ( $dst_file ) )
- {
- //建立暫存檔案
- $dst_file = tempnam ( $ _SERVER["SINASRV_CHEDIRDIR "],"TMP_IMG" );
- }
- MagickWriteImage ( $this->magick_handle, $dst_file );
- return $dst_file;
- }
-
- return $dst_file;
- }
-
- / protected function set_mark()
- {
-
- //
- $dst_width = MagickGetImageWidth ( $this->magick_handle ); //處理浮水印圖
- if ( $this->water_mark && is_file ( $this->water_mark ) )
- {
- $magic_water_handle = NewMagickWand();
- MagickoveImage $ magic_water_handle );
- if ( MagickReadImage ( $magic_water_handle, $this->water_mark ) )
- {
- MagickScaleImage ( $magic_water_handle, $dst_width -thisd-wid waterrate *MagickGetImageHeight ( $magic_water_handle ) /MagickGetImageWidth ( $magic_water_handle ) );//將水印縮放到圖片的1/5
- if ( $this->water_mark_pos == 1 )
- if ( $this->water_mark_pos == 1 )
- $ 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 )
- {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 ( $top =$dst_height -$this->padding-MagickGetImageHeight ($sese $. ( $this->water_mark_pos == 5 )
- {
- $left = ( $dst_width-MagickGetImageWidth ( $ magic_water_handle ) ) /2;
- $top = ( $dst_height -MagickGet_He 2 ;
- }
- MagickCompositeImage ( $this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$頂部);
- }
- }
- }
- }
- >
- 複製程式碼
-
-
-
-
PHP
|