Php_imagick allows you to cut, rotate, sharpen, subtract, or add special effects to an image.

Source: Internet
Author: User
Tags imagemagick
This article describes how to cut, rotate, sharpen, subtract, or add special effects to php_imagick. You can call the PHP extension of ImageMagick function to make PHP have the same functions as ImageMagick.

This article describes how to cut, rotate, sharpen, subtract, or add special effects to php_imagick. You can call the PHP extension of ImageMagick function to make PHP have the same functions as ImageMagick.

This article describes how to cut, rotate, sharpen, subtract, or add special effects to an image using php_imagick. Share it with you for your reference. The specific analysis is as follows:

A PHP extension that allows PHP to call the ImageMagick function. With this extension, PHP can have the same functions as ImageMagick.

ImageMagick is a powerful, stable, and free toolkit that can be used to read, write, and process image files in over 185 basic formats, including popular TIFF, JPEG, GIF, PNG, PDF, and PhotoCD. By using ImageMagick, You can dynamically generate images based on the needs of web applications. You can also resize, rotate, sharpen, subtract, or add special effects to one or more images, save the operation results in the same format or other formats.

Php_imagick is a PHP extension package for image processing. It can be used to resize, rotate, sharpen, subtract, or add special effects to images.

1. Install Imagick extension in windows:

1. Download and install ImageMagick

2. Download php_imagick.dll

If you are using thread-safe php, download

Windows-builds/php53/imagick-2.3.0-dev/vc9_zts/php_imagick.dll

3. Set

Add in php. ini

Extension = php_imagick.dll, restart web server

Ii. Install Imagick extension in linux:

1. Install ImageMagick in yum

Yum install ImageMagick-devel

2. test whether the installation is successful.

Convert-version

3. Install imagick Extension

01. wget 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

Iii. Cases

1. Border Processing

The Code is 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 take a look at a simple example.

Php_imagick program example

1. Create a thumbnail and display it

The Code is 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 under the Directory and save it

The Code is as follows:

<? Php
$ Images = new Imagick (glob ('images/*. JPG '));
Foreach ($ images as $ image ){
// Providing 0 forces thumbnailImage to maintain aspect ratio
$ Image-> thumbnailImage (, 0 );
}
$ Images-> writeImages ();
?>


3. scale down GIF animated images

The Code is 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 started,

Example:

Crop/generate thumbnails/Add watermarks to automatically detect and process GIF images

Call method:

The Code is as follows:

Include 'imagick. class. php ';
$ Image = new lib_image_imagick ();
$ Image-> open('a.gif ');
$ Image-> resize_to (100,100, 'scale _ Fill ');
$ Image-> add_text ('1024i. com', 10, 20 );
$ Image-> add_watermark('1024i.gif ', 10, 50 );
$ Image-> save_to('x.gif ');
Imagick. class. php

<? Php
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 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 the image size
$ Fit: Adaptive size
'Force': forces the image to be deformed to $ width X $ height.
'Scale': scale the image in the security box $ width X $ height proportionally. After the output is scaled, the image size is not completely equal to $ width X $ height.
'Scale _ Fill': scales the image in the security box $ width X $ height proportionally. The color of the area without pixels is filled in the security box, when this parameter is used, you can set the background fill color $ bg_color = array (255,255,255) (red, green, blue, transparency) Transparency (0 Opacity-127 completely transparent ))
Others: Smart mode scales the image and loads the middle part of the image $ width X $ height pixel size
$ Fit = 'force', 'Scale', and 'scale _ fill ': output the complete image.
$ Fit = when the image orientation value is specified, some images at the specified position are output.
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 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;
}
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'];
}
// Set the image type, which is consistent with the source type by default.
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;
Else
Return false;
}

Public function thumbnail ($ width = 100, $ height = 100, $ fit = true) {$ this-> image-> thumbnailImage ($ width, $ height, $ fit );} // generating a thumbnail $ fit will maintain a ratio when it is true and generate a thumbnail within the security box $ width X $ height
/*
Add a border
$ Width: Border width
$ Height: Border Width
$ Color: RGB color 'rgb (, 0, 0) 'or hexadecimal color' # ff0000' or 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 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 the color level
Public function modulate ($ brightness, $ saturation, $ hue) {$ this-> image-> modulateImage ($ brightness, $ saturation, $ hue );} // adjust the brightness, saturation, and tone
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 horizontally
Public function flip () {$ this-> image-> flipImage ();} // vertical flip
}

I hope this article will help you with PHP programming.

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.