http://www.developer.com/lang/php/imagemagick-php-image-manipulation.html

Back to article

5 Cool PHP Image Manipulation Tricks


May 2, 2012

ImageMagick is not very well known by end users, but PHP programmers across the world use this powerful utility widely to perform image operations, be they batch operations or individual tasks.

In this article I share my five coolest ImageMagick tricks, which you can use to perform more PHP image manipulation tasks in less time and increase your overall productivity.

1. Watermark Your Images in PHP

Watermarking your images -- especially when using them on a blog or even in a business presentation -- is a good idea. it allows you to maintain the copyright on the image(s). With ImageMagick, watermarking your images can be quite easy and quick. Notice how I do that in the following example.

convert kid2.png -font Arial -pointsize 70 -draw "gravity south fill black text 0,12 'Copyright' fill white text 1,11 'Copyright' " out.jpg

Here is the ImageMagick watermarking output:

ImageMagick PHP image manipulation

The above command is the simplest way to create a watermark with a little emboss effect. If you do not want the emboss effect and want to have a simpler command, then use the following :

convert kid2.png -font Arial -pointsize 70 -draw "gravity south fill black text 0,12 'Copyright' " out.jpg

Here is the output:

ImageMagick PHP image manipulation

Another way to create the watermark is by using the image and then controlling the transparency. Here are the steps to use that technique:

  1. Create a transparent image for the needed watermark text:
    convert -background transparent -fill black -pointsize 72 label:Copyright label2.png
  2. Create the final image with 35% copyright text:

    composite -dissolve 35 -gravity south label2.png kid2.png out.png

2. Batch Resize Images in PHP

Resizing images is a very common task, but it can be cumbersome when one needs to resize images in large numbers. ImageMagick provides a slick way to resize a large number of images using a single command: mogrify.

The mogrify command lets you perform batch image manipulations such as resize, blur, flip, draw and so on using the command line. The mogrify manual says that the original images will be overwritten until a different output format is specified. However, I used the -path parameter in the following command, which creates the new resized images in the specified folder. The name of the output folder is resized and is already present in the same folder. Make sure the resized folder has write permissions.

mogrify -resize 50% -format jpg -path resized *.jpg

The above command resizes all JPEG images in the folder to 50% of their original size and copies the resized images into the resized folder. You can also specify the size of the output images, using the following command. If you specify ! after the size, then the resulting images will ignore the aspect ratio.

mogrify -resize 400*300! -format jpg -path resized *.jpg

3. Create Rounded Buttons in PHP

Creating rounded buttons on the fly is pretty straight forward with ImageMagick. To create a quick rounded button, use the following command:

convert -size 150x50 xc:none -fill 'rgb(67, 135, 245)' -stroke 'rgb(0, 79, 206)' -strokewidth 2 -draw "roundrectangle 0,0 120,40 20,25" button.gif

ImageMagick PHP image manipulation

You can change the color of the button by changing the rgb values. To put the text over this rounded background, use the following convert command.

convert button.gif -pointsize 22 -draw "fill white text 30,28 'button'" button2.gif

ImageMagick PHP image manipulation

4. Brighten / Sharpen / Crop an Image in PHP

Need an alternative to Photoshop or another heavy software product for fine tuning your pictures? ImageMagick offers many of the same features in a lighter-weight utility. In this section, you are going to see how to brighten, sharpen and crop images in ImageMagick.

Here is the original image for the example.

ImageMagick PHP image manipulation

The following command increases brightness by 120%.

convert kid.png -modulate 125% bright.jpg

ImageMagick PHP image manipulation

The following command increases sharpness.

convert k2.png -unsharp 1.5×1.0+1.5+0.02 sharp.png

ImageMagick PHP image manipulation

The following command crops an image.

convert k2.png -crop 150x150+30+10 crop.png

ImageMagick PHP image manipulation

5. Frame an Image in PHP

I am sure you always wondered how Google's Picasa tool creates a cool collage of your images very quickly. Well, you can create the same effect using ImageMagick. See how I converted the original sample image to a cool bordered image.

ImageMagick PHP image manipulation

The command to create this frame is also simple:

convert kid.png -bordercolor white -border 6 -bordercolor grey60 -border 1 -background none -rotate 6 -background black \( +clone -shadow 60x4+4+4 \) +swap -background none -flatten frame.png

Explore More ImageMagick Tricks

The article provided just a few examples of ImageMagick. You can explore more ImageMagick features on ImageMagicks's Usage page.

Sitemap | Contact Us