Php_imagick to achieve picture cut, rotate, sharpen, subtractive or add special effects

Source: Internet
Author: User
Tags foreach 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

The code is as follows Copy Code
by www.111cn.net
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

The code is as follows Copy Code
<?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

The code is as follows Copy Code

<?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

The code is as follows Copy Code

<?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 1

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

Call Mode:

The code is as follows Copy Code

Include ' imagick.class.php ';

$image = new Lib_image_imagick ();

$image->open (' a.gif ');
$image->resize_to ("Scale_fill");
$image->add_text (' 1024i.com ', 10, 20);
$image->add_watermark (' 1024i.gif ', 10, 50);
$image->save_to (' x.gif ');

imagick.class.php

The code is as follows Copy Code

<?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

}

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.