<?php Class Lib_image_imagick { Private $image = null; Private $type = null; Constructors Public Function __construct () {} destructor Public Function __destruct () { if ($this->image!==null) $this->image->destroy (); } Loading images Public function open ($path) { $this->image = new Imagick ($path); if ($this->image) { $this->type = Strtolower ($this->image->getimageformat ()); } return $this->image; }
Public function crop ($x =0, $y =0, $width =null, $height =null) { if ($width ==null) $width = $this->image->getimagewidth ()-$x; if ($height ==null) $height = $this->image->getimageheight ()-$y; if ($width <=0 | | | $height <=0) return;
if ($this->type== ' gif ') { $image = $this->image; $canvas = new Imagick ();
$images = $image->coalesceimages (); foreach ($images as $frame) { $img = new Imagick (); $img->readimageblob ($frame); $img->cropimage ($width, $height, $x, $y); $canvas-> AddImage ($IMG); $canvas-> Setimagedelay ($img->getimagedelay ()); $canvas-> Setimagepage ($width, $height, 0, 0); } $image->destroy (); $this->image = $canvas; } Else { $this->image->cropimage ($width, $height, $x, $y); } } /* * Change image size $fit: Fit the size way ' Force ': Force the picture to form $width X $height size ' Scale ': scales the picture proportionally in the security box $width x $height, and the image size is not exactly equal to $width x after the output is scaled $height ' Scale_fill ': scales the picture within the security box $width X $height, with no pixel fill in the Security box, setting the background fill color when using this parameter $BG _color = Array (255,255,255) (red, green, blue, transparent) Degrees (0 opaque-127 fully transparent)) Other: Intelligent mode can scale an image and load the middle part of an image $width X $height pixel size $fit = ' force ', ' scale ', ' scale_fill ': Output full image $fit = Image Azimuth value, output specified position part image The correspondence between letters and images is as follows:
North_west North North_east
West Center East
South_west South South_east
*/ Public Function resize_to ($width = +, $height = $fit = ' Center ', $fill _color = Array (255,255,255,0)) {
Switch ($fit) { Case ' Force ': if ($this->type== ' gif ') { $image = $this->image; $canvas = new Imagick ();
$images = $image->coalesceimages (); foreach ($images as $frame) { $img = new Imagick (); $img->readimageblob ($frame); $img->thumbnailimage ($width, $height, false); $canvas->addimage ($IMG); $canvas->setimagedelay ($img->getimagedelay ()); } $image->destroy (); $this->image = $canvas; } Else { $this->image->thumbnailimage ($width, $height, false); } Break Case ' scale ': if ($this->type== ' gif ') { $image = $this->image; $images = $image->coalesceimages (); $canvas = new Imagick (); foreach ($images as $frame) { $img = new Imagick (); $img->readimageblob ($frame); $img->thumbnailimage ($width, $height, true); $canvas->addimage ($IMG); $canvas->setimagedelay ($img->getimagedelay ()); } $image->destroy (); $this->image = $canvas; } Else { $this->image->thumbnailimage ($width, $height, true); } Break Case ' Scale_fill ': $size = $this->image->getimagepage (); $SRC _width = $size [' width ']; $SRC _height = $size [' height ']; $x = 0; $y = 0;
$DST _width = $width; $DST _height = $height; if ($src _width* $height > $src _height* $width) { $DST _height = intval ($width * $src _height/$src _width); $y = Intval (($height-$DST _height)/2); } Else { $DST _width = intval ($height * $src _width/$src _height); $x = Intval (($width-$DST _width)/2); } $image = $this->image; $canvas = new Imagick ();
$color = ' Rgba ('. $fill _color[0]. ', '. $fill _color[1]. ', '. $fill _color[2]. ', '. $fill _color[3]. ' if ($this->type== ' gif ') { $images = $image->coalesceimages (); foreach ($images as $frame) { $frame->thumbnailimage ($width, $height, true); $draw = new Imagickdraw (); $draw->composite ($frame->getimagecompose (), $x, $y, $dst _width, $dst _height, $frame); $img = new Imagick (); $img->newimage ($width, $height, $color, ' gif '); $img->drawimage ($draw); $canvas->addimage ($IMG); $canvas->setimagedelay ($img->getimagedelay ()); $canvas->setimagepage ($width, $height, 0, 0); } } Else { $image->thumbnailimage ($width, $height, true);
$draw = new Imagickdraw (); $draw->composite ($image->getimagecompose (), $x, $y, $dst _width, $dst _height, $image);
$canvas->newimage ($width, $height, $color, $this->get_type ()); $canvas->drawimage ($draw); $canvas->setimagepage ($width, $height, 0, 0); } $image->destroy (); $this->image = $canvas; Break Default $size = $this->image->getimagepage (); $SRC _width = $size [' width ']; $SRC _height = $size [' height '];
$crop _x = 0; $crop _y = 0;
$crop _w = $src _width; $crop _h = $src _height; if ($src _width* $height > $src _height* $width) { $crop _w = intval ($src _height* $width/$height); } Else { $crop _h = intval ($src _width* $height/$width); }
Switch ($fit) { Case ' north_west ': $crop _x = 0; $crop _y = 0; Break Case ' North ': $crop _x = intval ($src _width-$crop _w)/2); $crop _y = 0; Break Case ' North_east ': $crop _x = $src _width-$crop _w; $crop _y = 0; Break Case of ' West ': $crop _x = 0; $crop _y = intval ($src _height-$crop _h)/2); Break Case ' center ': $crop _x = intval ($src _width-$crop _w)/2); $crop _y = intval ($src _height-$crop _h)/2); Break Case ' East ': $crop _x = $src _width-$crop _w; $crop _y = intval ($src _height-$crop _h)/2); Break Case ' south_west ': $crop _x = 0; $crop _y = $src _height-$crop _h; Break Case ' South ': $crop _x = intval ($src _width-$crop _w)/2); $crop _y = $src _height-$crop _h; Break Case ' South_east ': $crop _x = $src _width-$crop _w; $crop _y = $src _height-$crop _h; Break Default $crop _x = intval ($src _width-$crop _w)/2); $crop _y = intval ($src _height-$crop _h)/2); }
$image = $this->image; $canvas = new Imagick ();
if ($this->type== ' gif ') { $images = $image->coalesceimages (); foreach ($images as $frame) { $img = new Imagick (); $img->readimageblob ($frame); $img->cropimage ($crop _w, $crop _h, $crop _x, $crop _y); $img->thumbnailimage ($width, $height, true);
$canvas->addimage ($IMG); $canvas->setimagedelay ($img->getimagedelay ()); $canvas->setimagepage ($width, $height, 0, 0); } } Else { $image->cropimage ($crop _w, $crop _h, $crop _x, $crop _y); $image->thumbnailimage ($width, $height, true); $canvas->addimage ($image); $canvas->setimagepage ($width, $height, 0, 0); } $image->destroy (); $this->image = $canvas; }
}
Add a watermark Picture Public Function Add_watermark ($path, $x = 0, $y = 0) { $watermark = new Imagick ($path); $draw = new Imagickdraw (); $draw->composite ($watermark->getimagecompose (), $x, $y, $watermark->getimagewidth (), $watermark-> Getimageheight (), $watermark); if ($this->type== ' gif ') { $image = $this->image; $canvas = new Imagick (); $images = $image->coalesceimages (); foreach ($image as $frame) { $img = new Imagick (); $img->readimageblob ($frame); $img->drawimage ($draw);
$canvas->addimage ($IMG); $canvas->setimagedelay ($img->getimagedelay ()); } $image->destroy (); $this->image = $canvas; } Else { $this->image->drawimage ($draw); } } Add watermark Text Public Function Add_text ($text, $x = 0, $y = 0, $angle =0, $style =array ()) { $draw = new Imagickdraw (); if (Isset ($style [' Font '])) $draw->setfont ($style [' font ']); if (Isset ($style [' font_size ']) $draw->setfontsize ($style [' font_size ']); if (Isset ($style [' Fill_color ']) $draw->setfillcolor ($style [' Fill_color ']); if (Isset ($style [' Under_color ']) $draw->settextundercolor ($style [' Under_color ']);
if ($this->type== ' gif ') { foreach ($this->image as $frame) { $frame->annotateimage ($draw, $x, $y, $angle, $text); } } Else { $this->image->annotateimage ($draw, $x, $y, $angle, $text); } }
Save to the specified path Public Function save_to ($path) { if ($this->type== ' gif ') { $this->image->writeimages ($path, true); } Else { $this->image->writeimage ($path); } } Output image Public function output ($header = True) { if ($header) header (' Content-type: '. $this->type); echo $this->image->getimagesblob (); } Public Function Get_width () { $size = $this->image->getimagepage (); return $size [' width ']; }
Public Function Get_height () { $size = $this->image->getimagepage (); return $size [' height ']; } Sets the image type, which is the same as the source type by default Public Function Set_type ($type = ' png ') { $this->type = $type; $this->image->setimageformat ($type); } Get Source Image Type Public Function Get_type () { return $this->type; } Whether the current object is a picture Public Function Is_image () { if ($this->image) return true; Else return false; }
Public function thumbnail ($width =, $height = $fit = True) {$this->image->thumbnailimage ($width, $height, $fit);} Generate thumbnails $fit is true and the proportions are maintained and thumbnails are generated within the security box $width X $height /* Add a border $width: Left and right border width $height: Up and down border width $color: color: RGB color ' rgb ( 255,0,0) ' or 16-color ' #FF0000 ' or the color word ' white '/' red ' ... */ public function border ($width, $height, $color = ' rgb (all, All) ') { $color =new imagickpixel (); $color->setcolor ($color); $this->image->borderimage ($color, $width, $height); } public function blur ($radius, $sigma) {$this->image->blurimage ($radius, $sigma);}//Blur public function Gaussian_blur ($radius, $sigma) {$this->image->gaussianblurimage ($radius, $sigma);}// Gaussian blur public function Motion_blur ($radius, $sigma, $angle) {$this->image->motionblurimage ($radius, $ Sigma, $angle); Motion blur public function Radial_blur ($radius) {$this->image->radialblurimage ($radius);}//radial blur Public Function add_noise ($type =null) {$this->image->addnoiseimage ($type ==null?imagick::noise_impulse: $type );} Add a noise point
Public Function level ($black _point, $gamma, $white _point) {$this->image->levelimage ($black _point, $gamma, $ White_point);} Adjust color levels The public function modulate ($brightness, $saturation, $hue) {$this->image->modulateimage ($brightness, $saturation , $hue);} Adjust brightness, saturation, hue Public function Charcoal ($radius, $sigma) {$this->image->charcoalimage ($radius, $sigma);}//Sketch Public Function Oil_paint ($radius) {$this->image->oilpaintimage ($radius);}//Oil painting effect
Public Function flop () {$this->image->flopimage ();}//Horizontal Flip Public Function Flip () {$this->image->flipimage ();}//Flip vertically } |