In order to improve the loading speed of the page, as well as reduce traffic, to the site of the picture to thin body, research a lot of solutions, and finally determined the use of ImageMagick, understand the software, really unsanitary environment, simply too good, the effect is too obvious.
ImageMagick Introduction:
ImageMagick is a powerful, stable, open source toolset and development package that can be used to read, write, and process image files in more than 89 basic formats, including popular TIFF, JPEG, GIF, PNG, PDF, and PHOTOCD formats. With ImageMagick, you can dynamically generate images based on the needs of your Web application, and you can resize, rotate, sharpen, subtract, or add effects to one (or a group of) images, and save the results of the operation in the same format or in another format, which you can do with the command line , or you can do it in C + +, Perl, Java, PHP, Python, or Ruby programming. ImageMagick also provides a high-quality 2D toolkit that partially supports SVG. Imagemagic's main focus is on performance, reducing bugs and providing stable APIs and ABI. ImageMagick is a software used to create, edit, and synthesize pictures. It can read, convert, and write images in many formats. Picture cutting, color substitution, application of various effects, picture rotation, composition, text, line, polygon, ellipse, curve, attach to the picture stretching rotation. ImageMagick is free software: all open source, free to use, copy, modify, release. Most operating systems are supported.
ImageMagick commands at a glance
[Convert | identify | mogrify | composite | montage | compare | display | animate | import | conjure]
Convert: Transform image format and size, blur, crop, banish stains, jitter, proximity, picture pictures, add new images, generate thumbnails, etc.
Identify: Describes the format and characteristics of one or more image files.
Mogrify: According to the specified size * * * An image, blur, crop, jitter and so on. Mogrify overwrites the original image file and then writes to a different image file.
Composite: Creates a picture based on a picture or multiple picture combinations.
Montage: Create some separate feature images. Any decorative images that contain feature images, such as borders, structures, picture names, and so on.
Compare: Evaluate different pictures in arithmetic and visually * * * other makeover pictures.
Display: If you have an X server system, it can show the picture in a sequential order
Animate: Display animated pictures with X server
Import: Prints the picture file on X server or any visible window. You can capture a single window, the entire screen or a rectangular portion of any screen.
Conjure: Explains the script executed by the MSL (Magick Scripting Language).
Installation:
sudo apt-get install ImageMagick
Common use cases:The size of the image (space occupied) depends mainly on the profile and quality of the image.
- Quality: The quality of the picture, the higher the quality, the greater the space occupied. The proper reduction of quality can greatly reduce the size of the picture. Generally speaking, from the quality of 100 to 85, basically the naked eye is difficult to distinguish between the difference, but the size of a large reduction. ImageMagick is set by using -quality .
- Profile: Record Some descriptive information about the picture. Information such as camera information (aperture, camera model), Photoshop metadata, color table, etc. It can take up from a few kilobytes to hundreds of KB, or even larger. There are two ways imagemagick can get rid of this information. +profile "*" or -strip
EXIF Information is a series of information captured by the digital camera in the course of shooting, which is placed in the head of a well-known JPG file, that is, the EXIF information is a set of shooting parameters embedded in the JPEG image file format, mainly including the aperture, shutter, ISO, Date time and other information related to the prevailing photographic conditions, camera brand model, color coding, recorded sound during shooting, and global Positioning System (GPS). Simply put, it is like the shoot camera date printing function, but the information recorded by EXIF is more detailed and complete. However, JPEG image files with EXIF information are slightly larger than normal JPEG files. There is a software like PS processed pictures will have "program comments." If it is not a professional photography site, this information is useless, you can remove:
Convert +profile "*"-strip src.jpg src-profile.jpg
Reduce the quality quality of the image to compress the image:
Convert-quality src.jpg src-quality85.jpg
The above two commands are combined to use:
Find/tmp/images-iname "*.jpg"-exec convert-strip +profile "*"-quality 80 {} {} \;
By the processing of these two commands, your picture can definitely be reduced a lot.PS: According to my experience, the conversion of PNG images to JPS Pictures will be reduced a lot, and then with the above two commands to deal with, it is perfect.
PNG can also be compressed by reducing the number of PNG image color. However, the compressed image of this method can be clearly distorted and jagged.
For real-world PNG images (often referred to as photographs), they are generally converted to JPG and compressed by the above method.
Convert-format jpg input.png input.jpg
Zooming a user's upload imagefor users to upload their own images can not be easily used to limit the size of CSS, because so each time you load the picture will still load the whole large picture. Consumes extra bandwidth and affects page loading speed. You should scale according to the actual display. For example, I want the image size of the user album cannot exceed 500x300:
Convert-resize "500x300>" input.jpg
The following is the installation of PHP Imgick extension, to process the picture:
Installation:
1. Installing the Imgick Extension
1) sudo apt-get install libmagick-dev2) sudo apt-get install php-pear php5-dev3) sudo pecl install Imagick
2. Edit the php.ini file so that it loads
Opens the php.ini file, where you added extension=imagick.so
/etc/php5/apache2/php.ini
Restart the Apache service to invalidate it
/etc/init.d/apache2 restart
Attach a simple example to convert the image format:
$thumb = new Imagick (); $thumb->readimage (' test.gif '); $thumb->writeimage (' test.jpg '); $thumb->clear (); $ Thumb->destroy ();
Use too much, I will not list, provide information for everyone to learn:
Imgick Class api:http://php.net/manual/en/class.imagick.php
Image compression Experience: http://blog.lizhigang.net/archives/228
ImageMagick official website: http://www.imagemagick.org/script/index.php
http://elf8848.iteye.com/blog/382528
Usage Example: http://hi.baidu.com/7soon/item/d394c2b55bd3bb70254b09ed
Finally, I'm enclosing my own class of PHP processing pictures with Imgick:
/* * Picture Compression class re-encapsulates Imagick * * @version 2014-07-30 * @author [email protected] */class Imgick_tool{//imagick object instance Public $obj = Null;public function __construct () {//Determine if the extension is loaded if (!extension_loaded (' Imagick ')) {return false;} $this->obj = new Imagick ();} /* * png2jpg Convert Picture Format * * @param string src_img source Picture path * @param string dest_img The path of the picture to be generated * @return Boolean converted to a total return true otherwise FAL Se */public function png2jpg ($src _img, $dest _img) {if (!is_object ($this->obj)) {return false;} try{$this->obj->readimage ($src _img), if ($this->obj->writeimage ($dest _img)) {$this->destory (); return $dest _img;} return false;} catch (Imagickexception $e) {return false;}} /* * Remove profile information from image * * @param string src_img source Picture path * @return string src_img picture name otherwise returns false */public function Strip_profi Le ($src _img, $dest _img = ") {if (!is_object ($this->obj)) {return false;} try{$dest _img = Empty ($dest _img)? $src _img: $dest _img; $this->obj->readimage ($src _img); $this->obj-> Stripimage (); if ($this->obj->writeimaGE ($dest _img)) {$this->destory (); return $src _img;} return false;} catch (Imagickexception $e) {return false;}} /* * Set JPG image quality * * @param string src_img source Picture path * @param string dest_img The path of the picture to be generated * @return Boolean converted to a total of true otherwise false * /public function set_quality ($src _img, $quality =, $dest _img = ") {if (!is_object ($this->obj)) {return false;} try{$dest _img = Empty ($dest _img)? $src _img: $dest _img; $this->obj->readimage ($src _img); $this->obj-> Setimagecompression (imagick::compression_jpeg); $this->obj->setimagecompressionquality ($quality); if ($ This->obj->writeimage ($dest _img)) {$this->destory (); return $dest _img;} return false;} catch (Imagickexception $e) {return false;}} /* Picture Slimming * * @param string src_img source picture path * @param int quality Set Picture compression quality * @param string dest_img path to the picture to be generated * @return bool Ean converted to a total return true otherwise false */public function slimming ($src _img, $quality = all, $dest _img = ",) {if (!is_object ($this->obj)) {return false;} try{$dest _img = Empty ($dest _img)? $src _img:$dest _img, $this->obj->readimage ($src _img), $this->obj->setimageformat (' jpeg '); $this->obj-> Setimagecompression (imagick::compression_jpeg);//reduce the quality of the picture to the original 60% $quality = $this->obj-> Getimagecompressionquality () * $quality/100; $this->obj->setimagecompressionquality ($quality); $this Obj->stripimage (); if ($this->obj->writeimage ($dest _img)) {$this->destory (); return $dest _img;} return false;} catch (Imagickexception $e) {return false;}} /* * Generate thumbnails * * @param string src_img source picture path * @param int quality Set Picture compression quality * @param string dest_img The path of the picture to be generated * @return Boo Lean converted to a total return true otherwise false */public function thump ($src _img, $width = +, $height = ") {if (!is_object ($this->obj)) { return false;} try{$file _info = pathinfo ($src _img);//generate thumbnail name $file_name = substr ($file _info[' basename '],0,strrpos ($file _info[') BaseName '], '. '); $dest _img = $file _info[' dirname '). '/' . $file _name. ' _thump. ' $file _info[' extension '); $this->obj->readimage ($src _img);//calculate to get the height of the thumbnail $img_width = $this->obj->>getimagewidth (); $img _height = $this->obj->>getimageheight (); $dest _height = $img _ Height * ($width/$img _width); $this->obj->resizeimage ($width, $dest _height, Imagick::filter_catrom, 1, false); /Generate Picture if ($this->obj->writeimage ($dest _img)) {$this->destory (); return $dest _img;} return false;} catch (Imagickexception $e) {return false;}} /* Release resources * */function Destory () {if (Is_object ($this->obj)) {$this->obj->clear (); $this->obj->destroy () ;}}}