PHP Image processing Extension Php_imagick class library use

Source: Internet
Author: User
A handy class library for encapsulating PHP extensions Php_imagick

Imagick provides an extremely rich image processing method, which describes a class library that encapsulates Php_imagick:

Class Lib_image_imagick{private $image = Null;private $type = null;//constructor Public function __construct () {}/destructor public function __destruct () {if ($this->image!==null) $this->image->destroy ()}//Load image Public Function open ($path) { $this->image = new Imagick ($path), if ($this->image) {$this->type = strtolower ($this->image->getimagefo Rmat ());} return $this->image;} Public Function Cropimage ($width =0, $height =0) {if ($width <=0 | | $height <=0) return; $this->image-> Cropthumbnailimage ($width, $height);} Public Function thumbnailimage ($width =0, $height =0) {if ($width <=0) return; $this->image->thumbnailimage ($ width, NULL);} Public function crop ($x =0, $y =0, $width =null, $height =null) {if ($width ==null) $width = $this->image->getimagewidt    H ()-$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 size ' Force ': Forces the picture to form $width x $height size ' scale ': scales the picture proportionally in the safe box $width x $height, the image size is not exactly equal to the output after scaling $wi DTH X $height ' Scale_fill ': Scale the picture proportionally in the safe box $width X $height, where there is no pixels inside the security box fill color, you can set the background fill color when using this parameter $BG _color = Array (255,255,255) (red , green, blue, transparency) transparency (0 opaque-127 fully transparent)) Other: Smart mode can scale the image and load the middle part of the image $width X $height pixel size $fit = ' force ', ' scale ', ' Scale_fill ' When: Output full image $fi t = image orientation value, output to the specified position part of the image of the letter and the image of theThe relationship should be as follows: North_west North North_eastwest Center eastsouth_west South South_east*/public function Resiz E_to ($width = $height =, $fit = ' center ', $fill _color = Array (255,255,255,0)) {switch ($fit) {C                ASE ' 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 ' 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 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 ($dra        W, $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 ($pa    th, 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 '];} Set the image type, the default is the same as the source type public Function set_type ($type = ' png ') {$this->type = $type; $this->image->setimageformat ($type);}  Gets the 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;elsereturn false;} Public function thumbnail ($width = $height, $fit = True) {$this->image->thumbnailimage ($width, $height, $fit);}  Create thumbnails $fit true to maintain proportions and generate thumbnails in the safe box $width X $height/* Add a border $width: Left and Right border width $height: top and Bottom Border width $color: color: RGB color ' rgb (255,0,0) ' or 16 binary color ' #FF0000 ' or color word ' white '/' red ' ... */public function border ($width, $height, $color = ' rgb (up, up) ') {$color =n EW 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 noise Public Function level ($black _point, $gamma, $white _point) {$this->image->levelimage ($black _point, $gamma , $white _point);} Adjust levels 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 ();} Flip Public Function Flip () {$this->image->flipimage ();}//Flip vertically}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.