PHP: ImageMagick perfectly replaces the GB class library for image processing

Source: Internet
Author: User
Tags imagemagick

During php development, everyone is used to using the GB class library to process image information. However, many functions of the GB Class Library are also a headache. You need to query functions one by one and read the official manual, next we will introduce ImageMagick, a powerful image processing tool, which will simplify image processing under liunx.

Let's take a look at the introduction of Baidu Encyclopedia:
ImageMagick is a powerful, stable, and free tool set and development kit that can be used to read, write, and process image files in over 89 basic formats, popular formats include 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. Operations on images can be performed through the command line or through C/C ++, Perl, Java, PHP, Python or Ruby programming. At the same time, ImageMagick provides a high-quality 2D toolkit that partially supports SVG. Currently, imagemagic focuses on performance, bug reduction, and stable API and Abi.

Function
1. convert an image from one format to another, including converting it to an icon directly.
2. Resize, rotate, sharpen (Sharpen), reduce colors, image effects
3. The compositing image of the thumbnail (a montage of image thumbnails)
4. Transparent images for Web backgrounds
5. convert a group of images into GIF animations and convert them directly.

Make several images into one composite image
 
Write or draw a graph on an image, with text shadow and border rendering.
8. Add a border or frame to the image
9. Obtain some image features
10, almost including the General Plug-in functions that gimp can do. It even includes rendering of various curve parameters. But the command is complex enough. ImageMagick can be compiled on almost any non-proprietary operating system, whether it is a 32-bit or 64-bit CPU, including Linux, windows '95/'98/ME/NT 4.0/2000/XP, Macintosh (MACOs 9/10), VMS and OS/2.

Installation:
ImageMagick provides multiple liunx, Mac, and win versions.
Specific installation and download can check the official website: http://www.imagemagick.org/script/index.php

The following describes the commonly used functions.:

1. Scaling user-uploaded images. CSS cannot be used to limit the size of images uploaded by users, because the entire image will be loaded each time the image is loaded. Excessive bandwidth is used and the page loading speed is affected. Scale according to the actual display needs. For example, if you want the image size in your album to be greater than 500x300, you can use ImageMagick to process the IM code.

Convert-resize "500x300>" input.jpg output.jpg # Keep the image as it is if it is smaller than X to prevent the image from being enlarged or distorted.

2. generally, different proportions of thumbnails are generated for photo albums. thumbnails and previews are provided. These thumbnails cannot be simply limited by CSS. Different Proportions of previews must be generated for each image.

3. removing excess information EXIF information is a series of information collected by a digital camera during the shooting process, which is placed in the header of a well-known JPG file, that is to say, EXIF information is a set of shooting parameters embedded in the JPEG image file format, including the aperture, shutter, ISO, date and time of photography and other information related to the photography conditions at that time, camera brand model, color encoding, recording sound and GPS information. Simply put, it is like the date printing function of a dummies camera, except that EXIF information records more detailed and complete information. However, JPEG image files with EXIF information are slightly larger than common JPEG files. In addition, images processed by software such as PS will contain "program
Comments ". If it is not a professional photography website, the information is useless and can be removed:
Im code

convert -strip input.jpg output.jpg 

4. Adjust the compression ratio most of the time, our website does not need so clear images, adjust the jpg image compression ratio to reduce the image size, the naked eye will not tell the quality of the picture after compression. Generally, 75% is the best proportion.
Im code

convert -quality 75% input.jpg output.jpg 

The preceding steps can be completed at one time:
Im code

convert -resize "500x300" -strip -quality 75% input.jpg output.jpg 

The above are all processing methods for JPG format. The following describes the processing of BMP, GIF, PNG, and other formats. Convert BMP to JPG directly. Then follow the above method. While GIF and PNG seem to be somewhat special. Some features of GIF (animation effect, transparency, etc.) are not available in JPG. You can select to convert or not to convert them according to the actual situation. If you convert them to JPG, you only need to take the first frame as follows:
Im code

convert -format jpg input.gif input.jpg 

PNG can also be compressed by reducing the color Quantity of PNG images. However, the image compressed in this way can be obviously distorted and displayed in a jagged state. For a PNG Image in the real world (usually a photo), it is usually converted to JPG first, and then compressed through the above method. Note that when converting a transparent or translucent PNG image to a jpg image, the transparent part turns black... We recommend that you do not convert your profile picture to JPG .. Ugly ~~~ My avatar is very poisoned .. With regard to the image extension, it is found that most websites prefer to convert user-uploaded images (profile pictures, photo albums, etc.) into a specific format (usually JPG ). The potential drawback of doing so is that it performs implicit Format Conversion Based on the extension when processing with software such as ImageMagick.
I personally think it is more flexible to process images without the extension.

Note: It is easy to rewrite the above command line with mini_magick into rails. In essence, mini_magick calls the system command line ~~

Example: The following is an Image Upload class:
Achieve unified scaling of image size

/*** @ Filesource upload. func. for PHP * to upload an image, ImageMagick * // *** upload a file ** $ size: the size (in the format of 100x100, in the middle of which is lowercase X) * returned value: * 0 file type Error */function upfile ($ size, $ subdir) {set_time_limit (0); $ filetype = array ("jpg", "GIF", "BMP ", "Jpeg", "PNG"); $ uppath = dirname (_ file __))). directory_separator. 'rooms '. directory_separator. 'img '. directory_separator. $ subdir. directory_separator; // If (file_exists ($ uppath) unlink ($ uppath); Mkdir ($ uppath); If (! Is_dir ($ uppath) mkdir ($ uppath); $ A = strtolower (pathinfo ($ _ FILES ['uploadfile'] ['name'], pathinfo_extension )); // determine the file type if (! In_array ($ A, $ filetype) {// $ text = implode (",", $ filetype); return 2; // echo "You can only upload the following types of files: ", $ text," <br> ";}else {// generate the object name $ filename = explode (". ", $ _ FILES ['uploadfile'] ['name']); do {$ filename [0] = $ size; // name the image by size $ name = implode (". ", $ filename); $ uploadfile = $ uppath. $ name;} while (file_exists ($ uploadfile); try {If (copy ($ _ FILES ['uploadfile'] ['tmp _ name'], $ uploadfile )) {// usr/bin/convertexec ("/usr/ Local/ImageMagick/bin/convert-resize '{$ size}>! '{$ Uploadfile} "); Return"/kkyoo/rooms/icoimg/{$ subdir }/". $ name ;}} catch (exception $ e) {echo $ e-> getmessage () ;}} return 0 ;}// end func upfile

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.