Skip to content

ImageMagick

Image editing functionality became incredibly important when PHP 5 brought quality Object Oriented programming support to PHP. There was a real need for fast, dynamic handling of images. And that meant the online community required dependable server-side software capable of handling the task. This is where ImageMagick has proven so beneficial.

What is ImageMagick?

ImageMagick is open-source software. It’s designed to empower users with the means to both create and edit bitmap images via a quick, simple process. It’s available for free, as source code or a ready to run binary distribution.

ImageMagick enables you to utilize various image files, including:

  • PDF
  • JPEG
  • GIF
  • TIFF
  • PNG
  • PostScript
  • SVG
  • PhotoCD

With ImageMagick, you can mirror, flip, scale, rotate, transform images, tweak colors, and take advantage of multiple special effects.

How can you use ImageMagick?

Generally, you can execute ImageMagick’s functions via a command in the command line interface. Alternatively, you may choose to use it through a program composed in the language of the application currently used by ImageMagick.

For instance, applications that have been written in Perl may create and edit images through PerlMagick. The corresponding PHP interface, MagickWand, is typically utilized for producing thumbnail images for PHP-based online stores or galleries.

Interfaces are available in various other languages, including Magick++ for C++, MagickWand for C, PerlMagick for Perl, and more.

How to change the size of an image while maintaining its original aspect ratio:

<?php

header('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;

?>

ImageMagick is designed to handle several files at once. This can be a huge advantage if you’re working on your own web app with a heavy dependence on graphics and pictures.

How to make thumbnails for all images within a specific 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();

?>

How to utilize ImageMagick via the command line

You can use ImageMagick with Unix-style command line commands, though this may appear to be a daunting, complex process for some users. However, it’s quite straightforward once you know how to do it.

Adjust a file extension via the command line

$magick> convert image.jpg image.png

We changed the “image.jpg” file into an “image.png” file in the example above. The command line makes it easier for you to manipulate more than one file easier too.

How to convert every image within a folder to .gif

$magick> convert *.jpg images.gif

In the example above, we converted all of the .jpg images in the specified folder to .gif. However, you may utilize the command line for complex tasks as well.

For example, if you select all the folder’s images that you want to edit in just one file, you can read the file, and convert every image referenced within it, using the command line interface.

How to convert all files in a myimages.txt file to .gif

 $magick> convert @mysuperimages.txt mysupermovie.gif

The command line tool is extremely powerful, and provides you with almost infinite possibilities to explore.

ImageMagick options

ImageMagick is available in various web hosting plans online, with command line interfaces for Python, PHP, JavaScript, and other languages.