Linux ImageMagick and PHP Imagick extended installation use

Source: Internet
Author: User
Tags imagemagick zts


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.

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

Zoom in on a user upload image
For users to upload their own 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 output.jpg #如果图片比500x300小就保持原样 to prevent small images from being magnified and distorted.

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

Installation:

1. Download and install ImageMagick

wget http://www.imagemagick.org/download/legacy/ImageMagick-6.6.8-10.tar.gz
TAR-XZVF imagemagick-6.6.8-10.tar.gz
./configure–prefix=/usr/local/imagemagick
Make
Make install
2. Download and install Imagick

Note: Installing this extension does not require installation of ImageMagick

Find the latest version of Imagick from Http://pecl.php.net/package/imagick

wget http://pecl.php.net/get/imagick-3.1.0RC1.tgz
TAR-XZVF Imagick-3.1.0rc1
Phpize
./configure–with-php-config=/usr/local/php/bin/php-config–with-imagick=/usr/local/imagemagick
Make
Make install
Make install execution results show:

Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
Installing header files:/usr/local/php/include/php/
Generate imagick.so to/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/, and then manually configure PHP.ini to take effect


/*
* The image compression class is encapsulated Imagick
*
* @version 2014-07-30
* @author andy1219111@163.com
*/
Class imgick_tool{

Imagick Object Instance
public $obj = null;

Public Function __construct ()
{
Determines whether the extension is loaded
if (!extension_loaded (' Imagick '))
{
return false;
}
$this->obj = new Imagick ();
}
/*
* Png2jpg Transform Picture format
*
* @param string src_img source picture path
* @param string dest_img The path of the picture to be generated
* @return Boolean conversion to a total return true or 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 from the picture
*
* @param string src_img source picture path
* @return string src_img 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 conversion to a total return true or 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 Thin body
*
* @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 true or 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 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 Boolean conversion to a total return true or 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 the height of the thumbnail to get
$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 pictures
if ($this->obj->writeimage ($dest _img))
{
$this->destory ();
return $dest _img;
}
return false;
}
catch (Imagickexception $e)
{
return false;
}
}

/*
* Releasing Resources
*
*/
function Destory ()
{
if (Is_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.