Php uses the imagick module to scale, crop, and compress images.

Source: Internet
Author: User
This article describes how to use the imagick module in php to scale, crop, and compress images. For more information, see

This article describes how to use the imagick module in php to scale, crop, and compress images. For more information, see

PHP uses the Imagick module to scale, crop, and compress images, including GIF images.

Scale and crop

The Code is as follows:


/**
* Crop images
* Cropping rules:
* 1. The height is null or zero and auto-adaptive Based on width scaling height
* 2. The width is null or zero and auto-adaptive Based on the zooming width of the height
* 3. Adjust the width, height to a ratio of not empty or zero to scale or crop the Image Based on the aspect ratio. The image is cropped from the center of the header by default.
* @ Param number $ width
* @ Param number $ height
*/
Public function resize ($ width = 0, $ height = 0 ){
If ($ width = 0 & $ height = 0 ){
Return;
}

$ Color = ''; // 'rgba (255,255,255, 1 )';
$ Size = $ this-> image-> getImagePage ();
// Original width and height
$ Src_width = $ size ['width'];
$ Src_height = $ size ['height'];

// Adaptive scaling by width
If ($ width! = 0 & $ height = 0 ){
If ($ src_width> $ width ){
$ Height = intval ($ width * $ src_height/$ src_width );

If ($ this-> type = 'gif '){
$ This-> _ resizeGif ($ width, $ height );
} Else {
$ This-> image-> thumbnailImage ($ width, $ height, true );
}
}
Return;
}
// Auto-scaling by height and width
If ($ width = 0 & $ height! = 0 ){
If ($ src_height> $ height ){
$ Width = intval ($ src_width * $ height/$ src_height );

If ($ this-> type = 'gif '){
$ This-> _ resizeGif ($ width, $ height );
} Else {
$ This-> image-> thumbnailImage ($ width, $ height, true );
}
}
Return;
}

// The scaled size
$ Crop_w = $ width;
$ Crop_h = $ height;

// The cropped position after scaling
$ Crop_x = 0;
$ Crop_y = 0;

If ($ src_width/$ src_height) <($ width/$ height )){
// The width proportion is smaller than the target width proportion, and the width proportion is scaled up from the header at the target height.
$ Crop_h = intval ($ src_height * $ width/$ src_width );
// Crop from the top without calculating $ crop_y
} Else {
// Scale-up of the aspect ratio greater than the target width and height ratio and crop the Image Based on the center of the target width
$ Crop_w = intval ($ src_width * $ height/$ src_height );
$ Crop_x = intval ($ crop_w-$ width)/2 );
}

If ($ this-> type = 'gif '){
$ This-> _ resizeGif ($ crop_w, $ crop_h, true, $ width, $ height, $ crop_x, $ crop_y );
} Else {
$ This-> image-> thumbnailImage ($ crop_w, $ crop_h, true );
$ This-> image-> cropImage ($ width, $ height, $ crop_x, $ crop_y );
}
}

How to process GIF images

The Code is as follows:


/**
* To process a GIF image, you must process each GIF image.
* @ Param unknown $ t_w zoom width
* @ Param unknown $ t_h zoom height
* @ Param string $ whether isCrop is cropped
* @ Param number $ c_w crop width
* @ Param number $ c_h crop height
* @ Param number $ c_x crop coordinate x
* @ Param number $ c_y crop coordinate y
*/
Private function _ resizeGif ($ t_w, $ t_h, $ isCrop = false, $ c_w = 0, $ c_h = 0, $ c_x = 0, $ c_y = 0 ){
$ Dest = new Imagick ();
$ Color_transparent = new ImagickPixel ("transparent"); // transparent color
Foreach ($ this-> image as $ img ){
$ Page = $ img-> getImagePage ();
$ Tmp = new Imagick ();
$ Tmp-> newImage ($ page ['width'], $ page ['height'], $ color_transparent, 'gif ');
$ Tmp-> compositeImage ($ img, Imagick: COMPOSITE_OVER, $ page ['X'], $ page ['y']);

$ Tmp-> thumbnailImage ($ t_w, $ t_h, true );
If ($ isCrop ){
$ Tmp-> cropImage ($ c_w, $ c_h, $ c_x, $ c_y );
}

$ Dest-> addImage ($ tmp );
$ Dest-> setImagePage ($ tmp-> getImageWidth (), $ tmp-> getImageHeight (), 0, 0 );
$ Dest-> setImageDelay ($ img-> getImageDelay ());
$ Dest-> setImageDispose ($ img-> getImageDispose ());

}
$ This-> image-> destroy ();
$ This-> image = $ dest;
}

Compression during storage

The Code is as follows:


// Save to the specified path
Public function save_to ($ path ){
// Compress the image quality
$ This-> image-> setImageFormat ('jpeg ');
$ This-> image-> setImageCompression (Imagick: COMPRESSION_JPEG );
$ A = $ this-> image-> getImageCompressionQuality () * 0.60;
If ($ a = 0 ){
$ A = 60;
}
$ This-> image-> setImageCompressionQuality ($ );
$ This-> image-> stripImage ();

If ($ this-> type = 'gif '){
$ This-> image-> writeImages ($ path, true );
} Else {
$ This-> image-> writeImage ($ path );
}
}

ImagickService. php

The Code is as follows:

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.