I must sometimes modify images and PDF documents in PHP. Prepare them to display after user upload, prepare for database, fix dimensions or many other things. Of course we can use native PHP functions, but it’s very slow. Better option is to use external libs like convert (ImageMagick) and Ghostscript. Today I have some useful convert commands – it’s “list to remember” for me, but maybe also you will find someting nice? This post is short, because I paste only commands and very short description.
Table of Contents
Fix image orientation
Phone images can have wrong image orientation – an example is photo form iPhone: good on computer, but 90 degree rotated after upload and display in user browser. You will don’ see it in image browsers but it can be very frustating. But.. how detect rotation? It’s simple, you can just remove orientation flag:
convert INPUT_FILE -auto-orient OUTPUT_FILE
Remove transarency
We don’t want to use alpha channel when we create thumbnails, so it’s nice idea to remove transparency:
convert INPUT_FILE -alpha remove OUTPUT_FILE
Force output file format
Convert always tries detect output format based on name, but sometimes it’s wrong or we just don’t want to use extensions. We can still forcr output format and save file with custom name:
convert INPUT_FILE some-other-commands jpg:OUTPU_FILE convert INPUT_FILE some-other-commands png:OUTPU_FILE convert INPUT_FILE some-other-commands pdf:OUTPU_FILE
Create thumbnails
I mentioned about thumbnails. It’s also good example. We can easily create thumbnails with max-size using convert:
// Max 100px width convert INPUT_FILE -thumbnail 100x OUTPUT_FILE // Max 100px height convert INPUT_FILE -thumbnail x100 OUTPUT_FILE // Max 100x100px convert INPUT_FILE -thumbnail 100x100 OUTPUT_FILE
Rotation (and conditional rotation)
We can rotate images using degrees, but also use conditional rotate – only when height is higher than width or vice versa:
// Rotate 90 degrees convert INPUT_FILE -rotate "+90" OUTPUT_FILE // Rotate only if width value is higher than height convert INPUT_FILE -rotate "+90>" OUTPUT_FILE // Rotate only if height value is higher than width convert INPUT_FILE -rotate "+90<" OUTPUT_FILE
Image resizing
Sometimes we want to resize image (with or without keep aspect ratio):
// Scaling with preserve ratio convert INPUT_FILE -resize 70% OUTPUT_FILE // Resize to max sizes convert INPUT_FILE -resize 640x480 OUTPUT_FILE // Conditional resizing (slash is only to escape string) convert INPUT_FILE -resize 640x480\> OUTPUT_FILE
Set gravity
Hard to exmplain, but it’s very important option – with gravity option we can set postion of other elements on image (text, other images, etc):
// Put image in center and make background white convert INPUT_FILE -gravity center -background white OUTPUT_FILE
Expand workspace:
We can also just expand image “workspace”. It’s good option with gravity. There is an example. Let’s say we have 200x250px image. The command will make 640×480 px file with this small image in center.
convert INPUT_FILE -gravity center -extent 640x480 OUTPUT_FILE
Summarize: use many options
Last example: we want convert image to PDF file, we also want to make it as as big as possible on A4 page – we must rotate that image if needed and also use white background. Hard? Not!
convert INPUT_FILE -rotate "+90>" -resize 575x823^\> -gravity center -background white -extent 595x842 pdf:OUTPUT_FILE