Image processing Artifact ImageMagick and PHP Imagick extensions ____php

Source: Internet
Author: User
Tags install php imagemagick php class

In order to improve the loading speed of the page, as well as reduce traffic, to the image of the site to thin body, research a lot of solutions, and finally determine the use of ImageMagick, understand the software, really unlike, simply too good, the effect is too obvious.

ImageMagick Introduction:

ImageMagick is a powerful, stable, and open source Toolset and development kit that can be used to read, write, and process picture files in more than 89 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. You can also 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 other format, which can be done through the command line , or you can do it programmatically with C + +, Perl, Java, PHP, Python, or Ruby. At the same time, ImageMagick provides a high-quality 2D toolkit that partially supports SVG. Imagemagic's main focus is on performance, reducing bugs, and providing a stable API and ABI. ImageMagick is a software used to create, edit, and synthesize pictures. It can read, convert, and write pictures in a variety of formats. Picture cutting, color replacement, application of various effects, picture rotation, combination, text, line, polygon, ellipse, curve, attached to picture stretching rotation. ImageMagick is free software: full source open, free to use, copy, modify, publish. Support for most operating systems.

ImageMagick's command overview

[Convert | identify | mogrify | composite | montage | compare | display | animate | import | conjure]


Convert: Transform image format and size, blur, crop, dispel stain, jitter, draw near, picture picture, add new picture, generate thumbnail, etc.

Identify: Describes the format and attributes of one or more image files.

Mogrify: In accordance with the required size * * * An image, Blur, cut, jitter and so on. Mogrify rewrite the original image file and write to a different image file.

Composite: Creates a picture from a picture or multiple combinations of pictures.

Montage: Create some separate feature images. In the image containing the elements of any decorative pictures, such as borders, structure, picture names.

Compare: in arithmetic and visual evaluation of different pictures * * * * other transformation pictures.

Display: If you have an X server system, it can display the picture in sequence

Animate: Using X server to display animated pictures

Import: Prints a picture file on X server or any visible window. You can capture a single window, an entire screen or a rectangular part of any screen.

Conjure: Explains the script that executes the MSL (Magick scripting Language) write.
Installation:

sudo apt-get install ImageMagick
Common use cases: influence picture size (occupy space) depends mainly on the picture profile and quality. Quality: The quality of the picture, the higher the quality, occupy the larger space. Reduced 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-quality. Profile: Record Some descriptive information about the picture. such as camera information (aperture, camera model), Photoshop metadata, color tables and other information. It takes up as much space from a few KB to hundreds of KB, possibly even more. ImageMagick can get rid of this information in two ways. +profile "*" or-strip
EXIF information is a series of information collected by a digital camera in the course of filming, which is placed in the head of our well-known JPG file, that is, EXIF information is a set of shooting parameters embedded in the JPEG image file format, including the aperture, shutter, ISO, Date time and other information related to photographic conditions at the time, camera brand model, color coding, recording sound and Global Positioning System (GPS). Simply put, it's like a instamatics date print function, except that the information recorded in the EXIF information is more detailed and complete. However, JPEG image files with EXIF information are slightly larger than normal JPEG files. There is a software such as PS processing of the picture will have "program comments." If it is not a professional photography site, this information is not used, can be removed:

Convert +profile "*"-strip src.jpg src-profile.jpg

Reduce the quality quality of the picture to compress the picture:

Convert-quality src.jpg src-quality85.jpg


The above two commands are combined to work together:

Find/tmp/images-iname "*.jpg"-exec convert-strip +profile "*"-quality 80 {} {} \;


After these two commands are processed, your picture can definitely shrink a lot. PS: According to my experience, converting a PNG picture to a JPS picture will be much smaller and then processed with the above two commands, which is perfect.

PNG can also be compressed by reducing the color number of PNG images. But this method of compressing the image can be clearly seen to be distorted, and jagged.
For real-world PNG pictures (usually referred to as photos), it is generally converted to JPG first and then compressed by the above method.

Convert-format jpg input.png  input.jpg  

upload pictures for users to zoom on their own uploaded images can not be simple to use CSS limit size, because so each time loading the picture will still load the whole large picture. Consumes excess bandwidth and affects page load speed. Scaling should be done according to the actual display. For example, I want the picture in the user's album to be larger than 500x300:

Convert-resize "500x300>" input.jpg  

Here's a imgick extension for installing PHP to work with pictures:

Installation:

1. Install Imgick extension

1 sudo apt-get install Libmagick-dev

2) sudo apt-get install php-pear php5-dev

3) sudo pecl install Imagick
2. Edit the php.ini file so that it loads

Open the php.ini file in which to add extension=imagick.so

/etc/php5/apache2/php.ini
Restart the Apache service to disable it

/etc/init.d/apache2 restart

Attach a simple example to convert the picture format:

$thumb = new Imagick ();
$thumb->readimage (' test.gif ');
$thumb->writeimage (' test.jpg ');
$thumb->clear ();
$thumb->destroy ();

Use too much, I will not enumerate, 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

Use instance: http://hi.baidu.com/7soon/item/d394c2b55bd3bb70254b09ed

Finally, attach my own PHP class that uses Imgick to process pictures:

* * Picture Compression class Imagick * * @version 2014-07-30 * @author andy1219111@163.com * * * class imgick_tool{//imagick Object Real
	
	Example public $obj = null;
		The Public Function __construct () {//Determines whether the extension if (!extension_loaded (' Imagick ') is loaded) {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
		Returns true otherwise than false * */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 for picture * * @param string src_img source Picture path * @return string src_img the picture name otherwise return false */public
		function Strip_profile ($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 Returns 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 picture to be generated The path * @return Boolean to return True otherwise false * */Public Function slimming ($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->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 Thumbnail * * @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 Boolean conversion to a total return of true or false/Public function thump ($src _img, $width = $he
		ight = ') {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);
			Compute 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; }/* * FREE resources */function destory () {if (i)S_object ($this->obj)) {$this->obj->clear ();
		$this->obj->destroy (); }
	}
	
}



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.