PHP image scaling, cropping and compression

Source: Internet
Author: User
Tags imagecopy imagemagick

Google pagespeed Insights can score page load speed and give optimization suggestions

Simply put, the picture is optimized using the right size picture (zoom, crop), compress the picture

Only Jpng and PNG two image formats are introduced here

Software Preparation:

ImageMagick

Apt-get Install ImageMagick

Jpegtran

Apt-get Install Libjpeg-turbo-progs

OptiPNG

Apt-get Install OptiPNG

Pngquant

Apt-get Install Pngquant

Php5-gd

Apt-get Install PHP5-GD

0. Format conversion

In general, JPG images are much smaller than PNG images, and using JPG images without special needs (such as transparency) can greatly reduce the size of a picture

Format conversion can use the Convert command

Shell_exec (' Convert a.png a.jpg ');

The picture changed from 700 k to 100 K.

1. Image Zoom

Picture Zoom can use the Convert command

$in = ' a.png '; $out = ' a-convert-resize-100.png ';//$out = ' a.png ';   Replace the input infile//escapeshellarg to handle special characters in the file name such as Space shell_exec (' Convert-resize '. Escapeshellarg ($in). ‘ ‘ . Escapeshellarg ($out));

Picture reduced from 1850*983 to 100*53 (width and height ratio unchanged)

2. Picture clipping

Picture clipping can be achieved using PHP's Imagecopy () or Imagecrop () function

$in = ' a.png '; $out = ' a-copy.png ';//clipping width $width = 100;//clipping height $height = 80;//The beginning of the original x-coordinate $x = 1;//the y-coordinate of the original beginning $y = 2; $inImage = IM Agecreatefrompng ($in);//$inImage = Imagecreatefromjpeg ($in); $outImage = Imagecreatetruecolor ($width, $height); Imagecopy ($outImage, $inImage, 0, 0, $x, $y, $width, $height); Imagepng ($outImage, $out);//imagejpeg ($outImage, $out); Imagedestroy ($inImage); Imagedestroy ($outImage);

3. Image compression

JPG images can use convert lossy compression and Jpegtran lossless compression

Convert–quality can compress a picture by a specified mass, but sometimes it may be more pressing

Jpegtran–progressive can compress pictures into progressive pictures, which is the image that is blurred and clear after loading, instead of showing from top to bottom

$in = ' b.jpg '; $out = ' b-convert-quality-60.jpg '; $out 2 = ' b-jpegtran-progressive.jpg '; $in = Escapeshellarg ($in); $out = Escapeshellarg ($out); $out 2 = Escapeshellarg ($out 2); Shell_exec (' convert-quality '. $in. ‘ ‘ . $out); Shell_exec (' Jpegtran-copy none-optimize-progressive-outfile '. $out 2. ‘ ‘ . $out);

PNG images can use Pngquant lossy compression and OptiPNG lossless compression

Pngquant can compress png32 or png24 pictures into Png8 charts, still retains transparency

Pngquat–quality settings too often will make the picture more and more pressure, so be careful to set, or do not set

$in = ' a.png '; $out = ' a-pngquant.png '; $out 2 = ' a-pngquant-optipnt.png '; $in = Escapeshellarg ($in); $out = Escapeshellarg ($ Out); $out 2 = Escapeshellarg ($out 2); Shell_exec (' Pngquant--speed 1-o '. $out. ‘ ‘ . $in);//replace file//shell_exec (' pngquant--speed 1-f--ext. png '. $in); Shell_exec (' Optipng-strip all-quiet-clobber -o3-i0 '. $out. '-out '. $out 2);

4. First scale or Compress first

It seems that the compression will be smaller before scaling (not necessarily accurate, I only measured a few pictures, and the sharpness of my screen is too blurry ...) )

Test data for A.png (1850*980)

Test data for b.jpg (1920*1080)

Resources

Google optimized picture suggestions Https://developers.google.com/speed/docs/insights/OptimizeImages

Ctrip Blog http://ued.ctrip.com/blog/image-optimization-tools.html

Imagemagic http://www.imagemagick.org/script/command-line-processing.php

Jpegtran http://jpegclub.org/jpegtran/

OptiPNG

Pngquant

PHP image scaling, cropping and compression

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.