March 6, 2021
Hot Topics:

5 Cool PHP Image Manipulation Tricks

  • By Sachin Khosla
  • Send Email »
  • More Articles »

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

Originally published on https://www.developer.com.

Page 1 of 2

This article was originally published on May 2, 2012

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date