PHP extension Magickwand
PHP extension Magickwand ImageMagick
Brief introduction
Magickwand is an extension of PHP, through which the interaction with the ImageMagick is established and the image is processed. It is an excellent alternative to the default GD image library. In terms of security and ease of use, using Magickwand in PHP is much safer and quicker than using command-line ImageMagick. In addition, Imagick can also be used as an alternative to ImageMagick in PHP.
There are two forms of Magickwand:
1, just establish the interaction with ImageMagick, this situation must first install ImageMagick, the advantage is the extension file small (generally hundreds of k), you can quickly upgrade the ImageMagick version, but the Windows temporary directory requires special permissions.
2, the extension itself contains ImageMagick, the advantage is that there is no need for additional installation ImageMagick, temporary directory does not require special permissions, but the extension file is larger (generally more than 4M).
Magickwand default is not in the PHP installation package, need to go to PHP website download, and open this extension in php.ini.
code example
Convert flower.jpg-quality 80% flower_quality.jpg $resource = Newmagickwand (); Magickreadimage ($resource, ' small_flower.jpg '); Magicksetformat ($resource, ' JPG '); Magicksetimagecompression ($resource, mw_jpegcompression); Magicksetimagecompressionquality ($resource, 80.0); Header (' content-type:image/gif '); Magickechoimageblob ($resource);
Write text on the picture:
Convert Flower.jpg-font courier-fill white-pointsize 20-annotate +50+50 flower flower_annotate1.jpg $resource = Newmagickwand (); $dwand = Newdrawingwand (); $pwand = Newpixelwand (); Pixelsetcolor ($pwand, "white"); Drawsetfont ($dwand, "/usr/share/fonts/default/truetype/cour.ttf");//Specify Font drawsetfontsize ($dwand); Drawsetfillcolor ($dwand, $pwand); Drawsetgravity ($dwand, mw_southgravity); Magickreadimage ($resource, ' small_flower.jpg '); if (Magickannotateimage ($resource, $dwand, 0, 0, 0, "Flower")) { header (' content-type:image/gif '); Magickechoimageblob ($resource); } else{ Echo magickgetexceptionstring ($resource);
Cutting:
Convert Flower.jpg-crop 128x128+50+50 flower_crop.jpg $resource = Newmagickwand (); Magickreadimage ($resource, ' small_flower.jpg '); if (Magickcropimage ($resource, N/A, +)) { header (' content-type:image/gif '); Magickechoimageblob ($resource); } else{ Echo magickgetexceptionstring ($resource); }
Rotate Picture:
Convert flower.jpg-rotate flower_rotate45.jpg $resource = Newmagickwand (); Magickreadimage ($resource, ' small_flower.jpg '); Magickrotateimage ($resource, NULL, $); Header (' content-type:image/gif '); Magickechoimageblob ($resource);
Resizing:
Convert flower_original.jpg-resize 640x480 flower.jpg $resource = Newmagickwand (); Magickreadimage ($resource, ' small_flower.jpg '); Magickresizeimage ($resource, Mw_quadraticfilter, 1.0); Header (' content-type:image/gif '); Magickechoimageblob ($resource);
Sharpening:
Convert flower.jpg-unsharp 1.5x1.0+1.5+0.02 flower_unsharp.jpg $resource = Newmagickwand (); Magickreadimage ($resource, ' small_flower.jpg '); Magickunsharpmaskimage ($resource, 1.5, 1.0, 1.5, 0.02); Header (' content-type:image/gif '); Magickechoimageblob ($resource);
RELATED LINKS
Magickwand for PHP (Windows+patch)
Magickwand for PHP Manual
Magickwand for Php/windows (Php_magickwand_q16_st.dll)
Also attached:
What are ImageMagick and Magickwand? How do I add a load?
ImageMagick is a program for image processing.
Can run under Windows and Linux
He has a very powerful function.
Can carry out various image processing such as size, format, watermark, label, etc.
For example, perform a convert--help under Linux
You can view part of the ImageMagick feature description
In a PHP program, you can also call this ImageMagick program
However, the form of the call is implemented through the PHP module.
The name of this module is called Magickwand.
Many programmers use Magickwand instead of PHP's built-in GD image function library
The ImageMagick feature is very powerful, and the installed RPM is included in the Linux release CD
But the version of the ImageMagick that comes with the system is usually very old and cannot coexist with magickwand.
So you can install a new one on your own
And compiling Magicwand is also more complicated.
The below will tell you more about
Environment preparation
------------------------
Compiling PHP Magickwand module environment must itself have been loaded with PHP
(because you will need to use PHP inside the Phpize program)
Download the latest version of ImageMagick (version greater than 6.2)
This is because the Redhat comes with a 5.x version and cannot be combined with Magickwand
So you can only compile a 6.2.x version of the original
And, not only is the development environment required for this package, on the actual running server
It is also possible to compile a imagemagick-6.2.x version manually.
Download the source package from the official station of ImageMagick (the first item in Google search is the official station)
Post-decompression configuration installation
./configure
ldflags= "-l/usr/lib" cppflags= "-i/usr/include"
--prefix=/usr/local/imagemagick
--enable-shared
--enable-lzw
Make
Make install
The 6.2 version of the ImageMagick installation is complete
Download
------------------------
Download the source package from the official station of Magickwand (the first item in Google search is the official station)
Download PHP source code from Php.net
The PHP source code decompression, the Magickwand also unzip, put in the PHP source under the ext/directory
such as/root/php-4.4.2/ext/magickwand
Integration Module
------------------------
Why integrate module? This is because PHP's built-in image processing function library is GD, not Magickwand
In the PHP configuration script, it is impossible to see
For example, to the source code root directory of PHP, such as:
cd/root/php-4.4.2
Execution./configure--help | grep Magickwand
You can find information about magickwand not being searched at all.
So direct compilation is not supported by the Magickwand module
Therefore, you need to manually add Magickwand source code to the source tree of PHP.
and regenerate the Configure script, and then compile
Go to catalogue Cd/root/php-4.4.2/ext/magickwand
Executive Phpize
Phpize This command is included in the PHP package itself, if this machine does not have PHP, this can not execute this command
(That's why you put the program on a server that already has PHP installed)
Then go back to the source root path of PHP
cd/root/php-4.4.2
Delete old Configure files
Rm./configure
Generate a new Configure file
./buildconf--force
This time in the PHP source directory, a new configure script was born
Use this script to configure PHP to open the Magickwand module
Execution./configure--help | grep Magickwand can see:
--with-magickwand=dir Include Magickwand Support
This means that the PHP configuration can now be compiled to enable the module.
Configuration compilation
------------------------
Now configure PHP, using parameters:
./configure
--enable-shared
--with-magickwand=shared,/usr/local/imagemagick
--enable-module=shared,magickwand
The Enable-module method is used here to obtain the so module file, which is easy to add to other servers
and then compile
Make
Wait a few minutes and you're done.
Get module
------------------------
Then you can go to the source directory of PHP to find the so file just compiled
Find/root/php-4.4.2-name ' magickwand.so '
Copy this file and put it on a different server.
CP./module/magickwand.so/root
Installation on a production environment
------------------------
On the actual running server, you must first follow the method described above
Compile a 6.2.x version of ImageMagick and install it on the server
Then copy the magickwand.so into the PHP module directory.
For example, the AS4 version of the php:/usr/local/php4/lib/php
And then join Extenstion=magickwand.so in php.ini.
such as the version of the corresponding AS4 php.ini in/usr/local/php4/etc/php.ini
Save to exit without restarting any services.