When PHP 5 finally brought extensive Object Oriented programming support to PHP, the need for image editing grew even higher. Images needed to be handled dynamically and as fast as possible, and in order to do that, the Internet community needed reliable server-side software to handle it. And this is where ImageMagick comes into play.
ImageMagick
ImageMagick is a free open source software suite, which allows users to create and edit bitmap images quickly and easily. It is being delivered as a ready-to-run binary distribution or as a source code.
With ImageМagick you can work with different image files, among them GIF, JPEG, PDF, PhotoCD, PNG, PostScript, SVG, and TIFF. Thanks to this great product you can quickly and easily flip, mirror, rotate, scale, shear and transform images, adjust image colors and apply various special effects.
How to use ImageMagick
Most often ImageMagick's functions are executed through a command in the command line interface or via a program written in the same language as that of the very application ImageMagick is using at the moment. For example, applications written in Perl can automatically and dynamically create and edit images via PerlMagick. The corresponding interface for PHP is called MagickWand and is often used for the creation of thumbnail images for PHP-based online galleries or e-shops. There are interfaces for several other languages, among them: MagickWand for C, Magick++ for C++, JMagick for Java, MagickNet for .NET, PerlMagick for Perl, etc.
Change the image size while preserving the original aspect ratio:
<?phpheader('Content-type: image/jpeg');
$image = new Imagick('my-large-image.jpg');
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
?>
ImageMagcik can also handle multiple files at ones, which can be very useful if you are creating your own web application, relying on the heavy usage of pictires and graphics in it.
Create thumbnails for all images in a given folder:
<?php$images = new Imagick(glob('images/*.JPG'));
foreach($images as $image) {
// Providing 0 forces thumbnailImage to maintain aspect ratio
$image->thumbnailImage(1024,0);
}
$images->writeImages();
?>
Use ImageMagick with the command line
ImageMagick can also be used with Unix styled command line commands. While for most of the users, this can seem needlessly complicated, it's actually fairly easy.
Change a file's extension using the command line
$magick> convert image.jpg image.pngIn the above example, we converted the "image.jpg" file into an “image.png” file. The command line also makes manipulating multiple files simpler.
Convert all the images in a folder to .gif
$magick> convert *.jpg images.gifWith the above example, we converted all .jpg images in the selected folder to .gif.
But the command line can be used for complicated tasks, too. For example, if we have selected all the images, which we want to edit in a single file, we can use the command line interface to read the file and convert all the images referenced in there.
Convert all the files in a myimages.txt file to .gif
$magick> convert @myimages.txt mymovie.gifThe command line is a very powerful tool, which can give you almost endless possibilities.
ImageMagick with NTC Hosting
NTC Hosting offers ImageMagick with all web hosting plans. Our hosting packages contains integrated modules for PHP and Perl. For all other languages: HTML, Python, JavaScript, etc., a CLI (command line interface) version is being offered.