Php_imagick to achieve picture shearing, rotating, sharpening, subtractive or adding special effects _php Tips

Source: Internet
Author: User
Tags imagemagick

This article illustrates the method of Php_imagick to realize picture shearing, rotating, sharpening, subtractive or adding special effects. Share to everyone for your reference. The specific analysis is as follows:

A php extension that allows PHP to invoke ImageMagick functionality. Using this extension enables PHP to have the same functionality as ImageMagick.

ImageMagick is a powerful, stable, and free toolset and development kit that can be used to read, write, and process picture files in more than 185 basic formats, including popular TIFF, JPEG, GIF, PNG, PDF, and PHOTOCD formats. With ImageMagick, you can dynamically generate pictures based on the needs of your Web application, and you can resize, rotate, sharpen, subtractive, or add special effects to one (or a group) of pictures and save the results of the operation in the same format or in other formats.

Php_imagick is a PHP expansion package for image processing, which can be done to change the size of the picture, rotate, sharpen, subtractive or add special effects, and other operations.

Install Imagick extensions under Windows:

1. Download ImageMagick and install

Http://image_magick.veidrodis.com/image_magick/binaries/ImageMagick-6.6.2-10-Q16-windows-dll.exe

2, download Php_imagick.dll

Http://valokuva.org/outside-blog-content/imagick-windows-builds/php53/imagick-2.3.0-dev/vc9_nts/php_imagick.dll

If you are using thread-safe php, please download

Http://valokuva.org/outside-blog-content/imagick-windows-builds/php53/imagick-2.3.0-dev/vc9_zts/php_ Imagick.dll

3, set up

Add in PHP.ini

Extension=php_imagick.dll, restart Web server

Second, install Imagick under Linux expansion:

1.yum installation ImageMagick

Yum Install ImageMagick Imagemagick-devel

2. Test whether the installation was successful

Convert-version

3. Install Imagick Extension

01.wget Http://pecl.php.net/get/imagick-3.1.0RC2.tgz02.tar XZVF imagick-3.1.0rc2.tgz03.cd Imagick-3.1.0rc204.phpize05.. /configure06.make07.make Install

4. Edit the php.ini file and add the following code at the end of the file

Extension=imagick.so

5. Restart the Apache server

Service httpd Restart

Third, the case

1. Border processing

Copy Code code as follows:
Header (' Content-type:image/jpeg ');
$image = new Imagick (' test.jpg ');
$color =new Imagickpixel ();
$color->setcolor ("RGB (220,220,220)");
$image->borderimage ($color, 5,4);
$image->blurimage (5,5,imagick::channel_green);
Echo $image;



Let's look at a simple example first.

Php_imagick Program Example

1. Create a thumbnail and display it

Copy Code code as follows:
<?php
Header (' Content-type:image/jpeg ');
$image = new Imagick (' image.jpg ');
If 0 is provided as a width or height parameter,//aspect ratio is maintained
$image->thumbnailimage (100, 0);
Echo $image;
?>



2. Create a thumbnail in the directory and save
Copy Code code as follows:
<?php
$images = new Imagick (glob) (' images/*. JPG '));
foreach ($images as $image) {
Providing 0 forces thumbnailimage to maintain aspect ratio
$image->thumbnailimage (1024,0);
}
$images->writeimages ();
?>



3. Thumbnail gif animated picture
Copy Code code as follows:
<?php
/* Create A new Imagick object and read in GIF */
$im = new Imagick ("Example.gif");
* Resize All frames * *
foreach ($im as $frame) {
/* 50x50 Frames * *
$frame->thumbnailimage (50, 50);
/* Set the virtual canvas to correct size * *
$frame->setimagepage (50, 50, 0, 0);
}/* Notice writeimages instead of writeimage * *
$im->writeimages ("Example_small.gif", true);
?>



Now let's get to the point.

Example:

Trim/Generate thumbnails/Add watermarks, automatically detect and process GIF

Call Mode:

Copy Code code as follows:
Include ' imagick.class.php ';


$image = new Lib_image_imagick ();


$image-&gt;open (' a.gif ');


$image-&gt;resize_to ("Scale_fill");


$image-&gt;add_text (' 1024i.com ', 10, 20);


$image-&gt;add_watermark (' 1024i.gif ', 10, 50);


$image-&gt;save_to (' x.gif ');


imagick.class.php

&lt;?php


Class Lib_image_imagick


{


Private $image = null;


Private $type = null;


Constructors


Public Function __construct () {}





destructor


Public Function __destruct ()


{


if ($this-&gt;image!==null) $this-&gt;image-&gt;destroy ();


}


Loading images


Public function open ($path)


{


$this-&gt;image = new Imagick ($path);


if ($this-&gt;image)


{


$this-&gt;type = Strtolower ($this-&gt;image-&gt;getimageformat ());


}


return $this-&gt;image;


}





Public function crop ($x =0, $y =0, $width =null, $height =null)


{


if ($width ==null) $width = $this-&gt;image-&gt;getimagewidth ()-$x;


if ($height ==null) $height = $this-&gt;image-&gt;getimageheight ()-$y;


if ($width &lt;=0 | | | $height &lt;=0) return;





if ($this-&gt;type== ' gif ')


{


$image = $this-&gt;image;


$canvas = new Imagick ();





$images = $image-&gt;coalesceimages ();


foreach ($images as $frame) {


$img = new Imagick ();


$img-&gt;readimageblob ($frame);


$img-&gt;cropimage ($width, $height, $x, $y);


$canvas-&gt;addimage ($IMG);


$canvas-&gt;setimagedelay ($img-&gt;getimagedelay ());


$canvas-&gt;setimagepage ($width, $height, 0, 0);


}





$image-&gt;destroy ();


$this-&gt;image = $canvas;


}


Else


{


$this-&gt;image-&gt;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-&gt;type== ' gif ')


{


$image = $this-&gt;image;


$canvas = new Imagick ();





$images = $image-&gt;coalesceimages ();


foreach ($images as $frame) {


$img = new Imagick ();


$img-&gt;readimageblob ($frame);


$img-&gt;thumbnailimage ($width, $height, false);


$canvas-&gt;addimage ($IMG);


$canvas-&gt;setimagedelay ($img-&gt;getimagedelay ());


}


$image-&gt;destroy ();


$this-&gt;image = $canvas;


}


Else


{


$this-&gt;image-&gt;thumbnailimage ($width, $height, false);


}


Break


Case ' scale ':


if ($this-&gt;type== ' gif ')


{


$image = $this-&gt;image;


$images = $image-&gt;coalesceimages ();


$canvas = new Imagick ();


foreach ($images as $frame) {


$img = new Imagick ();


$img-&gt;readimageblob ($frame);


$img-&gt;thumbnailimage ($width, $height, true);


$canvas-&gt;addimage ($IMG);


$canvas-&gt;setimagedelay ($img-&gt;getimagedelay ());


}


$image-&gt;destroy ();


$this-&gt;image = $canvas;


}


Else


{


$this-&gt;image-&gt;thumbnailimage ($width, $height, true);


}


Break


Case ' Scale_fill ':


$size = $this-&gt;image-&gt;getimagepage ();


$SRC _width = $size [' width '];


$SRC _height = $size [' height '];





$x = 0;


$y = 0;





$DST _width = $width;


$DST _height = $height;


if ($src _width* $height &gt; $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-&gt;image;


$canvas = new Imagick ();





$color = ' Rgba ('. $fill _color[0]. ', '. $fill _color[1]. ', '. $fill _color[2]. ', '. $fill _color[3]. '


if ($this-&gt;type== ' gif ')


{


$images = $image-&gt;coalesceimages ();


foreach ($images as $frame)


{


$frame-&gt;thumbnailimage ($width, $height, true);


$draw = new Imagickdraw ();


$draw-&gt;composite ($frame-&gt;getimagecompose (), $x, $y, $dst _width, $dst _height, $frame);


$img = new Imagick ();


$img-&gt;newimage ($width, $height, $color, ' gif ');


$img-&gt;drawimage ($draw);


$canvas-&gt;addimage ($IMG);


$canvas-&gt;setimagedelay ($img-&gt;getimagedelay ());


$canvas-&gt;setimagepage ($width, $height, 0, 0);


}


}


Else


{


$image-&gt;thumbnailimage ($width, $height, true);





$draw = new Imagickdraw ();


$draw-&gt;composite ($image-&gt;getimagecompose (), $x, $y, $dst _width, $dst _height, $image);





$canvas-&gt;newimage ($width, $height, $color, $this-&gt;get_type ());


$canvas-&gt;drawimage ($draw);


$canvas-&gt;setimagepage ($width, $height, 0, 0);


}


$image-&gt;destroy ();


$this-&gt;image = $canvas;


Break


Default


$size = $this-&gt;image-&gt;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 &gt; $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-&gt;image;


$canvas = new Imagick ();





if ($this-&gt;type== ' gif ')


{


$images = $image-&gt;coalesceimages ();


foreach ($images as $frame) {


$img = new Imagick ();


$img-&gt;readimageblob ($frame);


$img-&gt;cropimage ($crop _w, $crop _h, $crop _x, $crop _y);


$img-&gt;thumbnailimage ($width, $height, true);





$canvas-&gt;addimage ($IMG);


$canvas-&gt;setimagedelay ($img-&gt;getimagedelay ());


$canvas-&gt;setimagepage ($width, $height, 0, 0);


}


}


Else


{


$image-&gt;cropimage ($crop _w, $crop _h, $crop _x, $crop _y);


$image-&gt;thumbnailimage ($width, $height, true);


$canvas-&gt;addimage ($image);


$canvas-&gt;setimagepage ($width, $height, 0, 0);


}


$image-&gt;destroy ();


$this-&gt;image = $canvas;


}





}





Add a watermark Picture


Public Function Add_watermark ($path, $x = 0, $y = 0)


{


$watermark = new Imagick ($path);


$draw = new Imagickdraw ();


$draw-&gt;composite ($watermark-&gt;getimagecompose (), $x, $y, $watermark-&gt;getimagewidth (), $watermark-&gt; Getimageheight (), $watermark);


if ($this-&gt;type== ' gif ')


{


$image = $this-&gt;image;


$canvas = new Imagick ();


$images = $image-&gt;coalesceimages ();


foreach ($image as $frame)


{


$img = new Imagick ();


$img-&gt;readimageblob ($frame);


$img-&gt;drawimage ($draw);





$canvas-&gt;addimage ($IMG);


$canvas-&gt;setimagedelay ($img-&gt;getimagedelay ());


}


$image-&gt;destroy ();


$this-&gt;image = $canvas;


}


Else


{


$this-&gt;image-&gt;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-&gt;setfont ($style [' font ']);


if (Isset ($style [' font_size ']) $draw-&gt;setfontsize ($style [' font_size ']);


if (Isset ($style [' Fill_color ']) $draw-&gt;setfillcolor ($style [' Fill_color ']);


if (Isset ($style [' Under_color ']) $draw-&gt;settextundercolor ($style [' Under_color ']);





if ($this-&gt;type== ' gif ')


{


foreach ($this-&gt;image as $frame)


{


$frame-&gt;annotateimage ($draw, $x, $y, $angle, $text);


}


}


Else


{


$this-&gt;image-&gt;annotateimage ($draw, $x, $y, $angle, $text);


}


}





Save to the specified path


Public Function save_to ($path)


{


if ($this-&gt;type== ' gif ')


{


$this-&gt;image-&gt;writeimages ($path, true);


}


Else


{


$this-&gt;image-&gt;writeimage ($path);


}


}


Output image


Public function output ($header = True)


{


if ($header) header (' Content-type: '. $this-&gt;type);


echo $this-&gt;image-&gt;getimagesblob ();


}





Public Function Get_width ()


{


$size = $this-&gt;image-&gt;getimagepage ();


return $size [' width '];


}





Public Function Get_height ()


{


$size = $this-&gt;image-&gt;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-&gt;type = $type;


$this-&gt;image-&gt;setimageformat ($type);


}


Get Source Image Type


Public Function Get_type ()


{


return $this-&gt;type;


}





Whether the current object is a picture


Public Function Is_image ()


{


if ($this-&gt;image)


return true;


Else


return false;


}





Public function thumbnail ($width =, $height = $fit = True) {$this-&gt;image-&gt;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 color word ' white '/' red ' ...


*/


Public function border ($width, $height, $color = ' rgb (220, 220, 220) ')


{


$color =new Imagickpixel ();


$color-&gt;setcolor ($color);


$this-&gt;image-&gt;borderimage ($color, $width, $height);


}





Public function blur ($radius, $sigma) {$this-&gt;image-&gt;blurimage ($radius, $sigma);}//Blur


Public Function Gaussian_blur ($radius, $sigma) {$this-&gt;image-&gt;gaussianblurimage ($radius, $sigma);}//Gaussian Blur


Public Function Motion_blur ($radius, $sigma, $angle) {$this-&gt;image-&gt;motionblurimage ($radius, $sigma, $angle); /motion Blur


Public Function Radial_blur ($radius) {$this-&gt;image-&gt;radialblurimage ($radius);}//Radial blur


Public Function add_noise ($type =null) {$this-&gt;image-&gt;addnoiseimage ($type ==null?imagick::noise_impulse: $type );} Add a noise point





Public Function level ($black _point, $gamma, $white _point) {$this-&gt;image-&gt;levelimage ($black _point, $gamma, $ White_point);} Adjust color levels


The public function modulate ($brightness, $saturation, $hue) {$this-&gt;image-&gt;modulateimage ($brightness, $saturation , $hue);} Adjust brightness, saturation, hue


Public function Charcoal ($radius, $sigma) {$this-&gt;image-&gt;charcoalimage ($radius, $sigma);}//Sketch


Public Function Oil_paint ($radius) {$this-&gt;image-&gt;oilpaintimage ($radius);}//Oil painting effect





Public Function flop () {$this-&gt;image-&gt;flopimage ();}//Horizontal Flip


Public Function Flip () {$this-&gt;image-&gt;flipimage ();}//Flip vertically


}

I hope this article will help you with your PHP program design.

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.