On PHP extension imagick_php tutorial

Source: Internet
Author: User
Tags imagemagick
PHP is usually built with the GD library, because it is built-in does not need to install additional plug-ins on the server, so it is easier to use, but if your program is the main function of processing images, then it is not recommended to use GD, because GD not only low efficiency and ability is relatively weak, occupying a lot of system resources, In addition GD's creatfrom also has the bug, but Imagick is a very good substitute, for this recently took one of my project from GD changed to Imagick, but after the change has appeared some situation here to share to everybody.

First of all, let's talk about what's on my side:

Situation one: Need to rewrite the image operation class

Situation Two: Imagick Multi-Threading causes CPU usage to burst to 100%

Here, incidentally, the installation method of Imagick in centos6.4:

1, installation ImageMagick

Copy the Code code as follows:
wget http://soft.vpser.net/web/imagemagick/ImageMagick-6.7.1-2.tar.gz
Tar zxvf imagemagick-6.7.1-2.tar.gz
CD imagemagick-6.7.1-2/
./configure--prefix=/usr/local/imagemagick--disable-openmp
Make && make install
Ldconfig

To test whether the ImageMagick works:

Copy the Code code as follows:
/usr/local/imagemagick/bin/convert-version

2. Install PHP Extension: Imagick

Copy the Code code as follows:
wget http://pecl.php.net/get/imagick-3.0.1.tgz
Tar zxvf imagick-3.0.1.tgz
CD IMAGICK-3.0.1/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config--with-imagick=/usr/local/imagemagick
Make && make install
Ldconfig
Vi/usr/local/php/etc/php.ini
Added: Extension = "imagick.so"

Restart LNMP
Copy the Code code as follows:
/ROOT/LNMP Reload

Next, we propose solutions to these two conditions:

The solution to the situation one is as follows:

Copy CodeThe code is as follows:
/**
Imagick image Processing Class
Usage:
Introducing Imagick Objects
if (!defined (' Class_imagick ')) {require (INC ' class_imagick.php ');}
$Imagick =new Class_imagick ();
$Imagick->open (' a.gif ');
$Imagick->resize_to (100,100, ' Scale_fill ');
$Imagick->add_text (' 1024i.com ', 10,20);
$Imagick->add_watermark (' 1024i.gif ', 10,50);
$Imagick->save_to (' x.gif ');
Unset ($Imagick);
/**/
Define (' Class_imagick ', TRUE);
Class class_imagick{
Private $image =null;
Private $type =null;
Structure
Public Function __construct () {}
Destruction
Public Function __destruct () {
if ($this->image!==null) {$this->image->destroy ();}
}
Loading images
Public function Open ($path) {
if (!file_exists ($path)) {
$this->image=null;
return;
}
$this->image=new Imagick ($path);
if ($this->image) {
$this->type=strtolower ($this->image->getimageformat ());
}
$this->image->stripimage ();
return $this->image;
}
/**
Image Cutting
/**/
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
Parameters:
$width: New width
$height: New heights
$fit: Fit to size
' Force ': Change the image to $width X $height
' Scale ': scales the picture proportionally within the $width x $height, and the result is not exactly equal to $width x $height
' Scale_fill ': Scales the picture proportionally in the $width X $height, where no pixels are filled $fill_color=array (255,255,255) (red, green, blue, transparency [0 opaque-127 full transparent])
Other: Smart mode, zoom picture and cut $width X $height size from center
Attention:
$fit = "Force", ' scale ', ' Scale_fill ' when the full image is output
$fit = Output The image at the specified position in the image orientation
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 =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 _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 a picture watermark
Parameters:
$path: Watermark Picture (full path included)
$x, $y: watermark coordinates
/**/
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 a text watermark
Parameters:
$text: Watermark Text
$x, $y: watermark coordinates
/**/
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);
}
}
/**
Photo Archive
Parameters:
$path: Archived location and new file name
/**/
Public Function save_to ($path) {
$this->image->stripimage ();
Switch ($this->type) {
Case ' gif ':
$this->image->writeimages ($path, true);
return;
Case ' jpg ':
Case ' JPEG ':
$this->image->setimagecompressionquality ($_env[' imgq ');
$this->image->writeimage ($path);
return;
Case ' PNG ':
$flag = $this->image->getimagealphachannel ();
Compress if the PNG background is opaque
if (imagick::alphachannel_undefined = = $flag or imagick::alphachannel_deactivate = = $flag) {
$this->image->setimagetype (Imagick::imgtype_palette);
$this->image->writeimage ($path);
}else{
$this->image->writeimage ($path);
}unset ($flag);
return;
Default
$this->image->writeimage ($path);
return;
}
}
Direct output image to screen
Public function output ($header =true) {
if ($header) header (' Content-type: '. $this->type);
echo $this->image->getimagesblob ();
}
/**
Set up a shrink chart
When the $fit is true, the proportions are maintained and the $width X $height are produced to reduce the graph
/**/
Public function thumbnail ($width =100, $height =100, $fit =true) {$this->image->thumbnailimage ($width, $height, $ FIT);}
/**
Add a border to an image
$width: Left and right border width
$height: Upper and lower border width
$color: Color
/**/
Public function border ($width, $height, $color = ' rgb (220,220,220) ') {
$color =new Imagickpixel ();
$color->setcolor ($color);
$this->image->borderimage ($color, $width, $height);
}
Get Image width
Public Function Get_width () {$size = $this->image->getimagepage (); return $size [' width '];}
Get Image Height
Public Function Get_height () {$size = $this->image->getimagepage (); return $size [' Height '];}
Set the image type
Public Function Set_type ($type = ' png ') {$this->type= $type; $this->image->setimageformat ($type);}
Get Image Type
Public Function Get_type () {return $this->type;}
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 the color scale
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 effect
Public Function Oil_paint ($radius) {$this->image->oilpaintimage ($radius);}//Oil painting effect
Public Function flop () {$this->image->flopimage ();}//Flip Horizontal
Public Function Flip () {$this->image->flipimage ();}//Flip vertically
}

The solution to the situation II is as follows:

First use the/usr/local/imagemagick/bin/convert-version command to see if the output has been opened to multi-threaded, Features: The value of NULL is a single thread, if Features: The value is that the OpenMP description is multithreaded. Imagick's multithreaded mode has a bug that causes multi-core CPU usage to outlaw up to 100 instantly so be sure to use its single-threaded mode only.

Copy the Code code as follows:
Version:imagemagick 6.7.1-2 2014-05-29 Q16 http://www.imagemagick.org
Copyright:copyright (C) 1999-2011 ImageMagick Studio LLC
Features:

The top is the result that I display when I configure it correctly, and the result below is displayed if not configured correctly

Copy the Code code as follows:
Version:imagemagick 6.7.1-2 2014-05-29 Q16 http://www.imagemagick.org
Copyright:copyright (C) 1999-2011 ImageMagick Studio LLC
Features:openmp

The first result is single-threaded mode, the second result is multithreaded mode, because the Imagick multithreaded mode has a bug, so if you are just beginning to be installed in multithreaded mode Imagick that must be yum remove ImageMagick uninstall it and reinstall it.

After rewriting the class, everything is fine after reloading the imagick, and the efficiency of processing the image is significantly higher than before.


http://www.bkjia.com/PHPjc/779163.html www.bkjia.com true http://www.bkjia.com/PHPjc/779163.html techarticle PHP is usually built using the GD library, because it is built-in does not need to install additional plug-ins on the server, so it is easier to use, but if your program is the main function of processing images, then ...

  • Related Article

    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.