Share a class library that encapsulates PHP extension php_imagick.

Source: Internet
Author: User
A previous article has introduced the PHP extension php_imagick. you can see from the manual that imagick provides an extremely rich way to process images. The following describes a class library that encapsulates php_imagick: classlib_imag.

A previous article has introduced the PHP extension php_imagick. you can see from the manual that imagick provides an extremely rich way to process images. The following 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 the image public function open ($ path) {$ this-> image = new Imagick ($ path); if ($ this-> image) {$ this-> type = strtolower ($ this-> image-> getImageFormat ();} 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-> 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: The Adaptive size method 'Force': forces the image to be deformed to $ width X $ height 'scale ': proportional In the security box, scale the image within $ width X $ height. after the output is scaled, the image size is not completely equal to $ width X $ height 'scale _ fill ': scale the image in the security box $ width X $ height proportionally. the color is filled with no pixels in the security box. you can use this parameter to set the background fill color $ bg_color = array (255,255,255) (red, green, blue, transparency) transparency (0-127 transparent) Other: when the smart mode scales the image and loads the middle part of the image $ width X $ height pixel size $ fit = 'force', 'scale', 'scale _ fill: when the complete image $ fit = Image azimuth value is output, the correspondence between letters in the specified position and the image is as follows: north_west north north_eastwest center eastsouth_west south south_east */ Public function resize_to ($ width = 100, $ height = 100, $ 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_wi Dth * $ 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-> setIm AgePage ($ 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-> cropImag E ($ 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 the watermark image 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;} els E {$ this-> image-> drawImage ($ draw) ;}// add the 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-> setTe XtUnderColor ($ 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 it 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 out Put ($ 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 value is the same as the source type. public function set_type ($ type = 'PNG ') {$ this-> type = $ type; $ this-> image-> setImageFormat ($ Type) ;}// obtain the source image type public function get_type () {return $ this-> type ;}// whether the current object is an image public function is_image () {if ($ this-> image) return true; elsereturn false;} public function thumbnail ($ width = 100, $ height = 100, $ fit = true) {$ this-> image-> thumbnailImage ($ width, $ height, $ fit );} // Generate a thumbnail $ fit will keep the proportion when it is true and generate a thumbnail in the security box $ width X $ height/* Add a border $ width: left and right border width $ height: border width $ color: RGB color 'rgb (255, 0, 0 )' Or the hexadecimal color '# ff0000' or the color word 'white'/'red '... */public function border ($ width, $ height, $ color = 'rgb (220,220,220) ') {$ 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 fuzzy 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 public function level ($ black_point, $ gamma, $ white_point) {$ this-> image-> levelImage ($ black_point, $ gamma, $ white_point);} // adjust the color level public function modulate ($ brightness, $ saturation, $ hue) {$ this-> image-> modulateImage ($ brightness, $ saturation, $ hue);} // adjusts the brightness, saturation, and tone of the 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 () ;}// vertical flip}

Tutorial:
1. cut the image:
$image = new lib_image_imagick();$image->open('test.jpg');$image->cropimage(200, 120);$image->save_to('test1.jpg');

2. generate a thumbnail:
$image = new lib_image_imagick();$image->open('test.jpg');$image->thumbnailImage(50, 50);$image->save_to('test_small.jpg');

3. add an image watermark:
$image = new lib_image_imagick();$image->open('test.jpg');$image->add_watermark('test_small.jpg');$image->save_to('test_water.jpg');

4. add a text watermark:
$image = new lib_image_imagick();$image->open('test.jpg');$image->add_text('http://www.phpddt.com',50,50,0,array('font'=>'ARIAL.TTF','font_size'=>14,'fill_color'=>'white','under_color'=>'black'));$image->output();

5. change the image size:
$image = new lib_image_imagick();$image->open('test.jpg');$image->resize_to(800,800);$image->output();

In addition, you can also flip, add borders, Blur, and other effects.

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.