- /**
- * Picture shrinkage Water printing type
- *
- * @version 1.0;
- *
- */
- Class Cls_photo
- {
- protected $waterrate = 0.2; The scale of the watermark icon on the picture
- protected $width = 300; Thumbnail default width
- protected $height = 200; Thumbnail default height
- protected $padding = 5; The distance from the watermark graph to the edge
- protected $water _mark = "./water.png";
- protected $water _mark_pos = 5;//watermark Picture position (1= upper left corner, 2 = upper right corner, 3 = lower left corner, 4 = lower right corner, 5 center)
- Protected $watermode = 0;//0 thumbnail does not hit watermark 1 thumbnail when drawing water
- Protected $magick _handle;//Picture manipulation handle
- Protected $format = array (' jpg ', ' gif ', ' PNG ', ' jpeg '); Picture file format Qualification
- protected $smallpic _mode = 2;//default mode 0 is not to generate thumbnails, 1 for crop scaling, 2 for scaling 3 for scale fill mode
- /**
- * Set Picture class parameters
- *
- * @param $arg Picture parameters can be placed in the array several times as follows
- * @param $protected parameter values
- * 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 Zoom
- *
- * @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 (! Magickreadimage ($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;//trim 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;
- }
- Else
- {
- $src _HEIGHTC = $src _width* $dst _height/$dst _width;
- $pos _y = ($src _height-$src _heightc)/2;
- }
- Magickcropimage ($magic _water_handle, $src _widthc, $src _heightc, $pos _x, $pos _y);//Cut
- Because the Gif image changes after the Magickcropimage function, the canvas does not change
- $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);
- Scaling
- Magickscaleimage ($this->magick_handle, $dst _width, $dst _height);
- }
- Proportional Zoom 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;//Replacement
- Magickscaleimage ($this->magick_handle, $dst _width, $dst _height);//Zoom
- }
- 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);//Zoom
- $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);
- }
- Play Watermark
- if ($this->watermode = = 1)
- {
- $this->set_mark ();
- }
- if (Empty ($dst _file))
- {
- Create a temporary file
- $DST _file = Tempnam ($_server["Sinasrv_cache_dir"], "tmp_img");
- }
- Magickwriteimage ($this->magick_handle, $dst _file);
- return $DST _file;
- }
- /**
- * Water Stamp
- *
- * @param $src _file The image path to be watermarked
- * @param $dst _file production watermark File save path, empty the production of random temporary files
- * @return Watermark File path/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))
- {
- Create a temporary file
- $DST _file = Tempnam ($_server["Sinasrv_cache_dir"], "tmp_img");
- }
- Magickwriteimage ($this->magick_handle, $dst _file);
- return $DST _file;
- }
- /**
- * Internal interface
- * Give the picture a water stamp
- *
- */
- protected function Set_mark ()
- {
- Size
- $DST _width = magickgetimagewidth ($this->magick_handle);
- $DST _height = magickgetimageheight ($this->magick_handle);
- Working with Watermark Maps
- 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)),//water release to the picture of 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);
- }
- }
- }
- }
Copy Code
|