ImageMagick can be used to create and edit images dunamically and display the results online or locally at our desired URLs. It offers lots of features, such as crop, resize, rotate, flip, blur, sharpen, inserting texts, and lots more, using its syntax properly.
And we can do all this stuffs using PHP. In order to use ImageMagick with PHP we can use PHP's function for executing external programs, which is exec and system. Other such functions also exist in PHP but the two mentioned above have the highest
priority.
The usage format is:
echo exec(string command [, array &output ]);
?>
In its simplest form, ImageMagick can be used as:
$firstImg = "test.bmp";
$finalResult = "test2.png";
exec("path_tp_image_magick\imagemagick\convert {$firstImg} {$finalResult}");
?>
Basically, the above function converts test.bmp to test2.png, i.e changes the file format.
Some of the basic stuffs that can be done using ImageMagick
Stuff 1: How to get width and height of image using ImageMagick in php
$dimensionsArr = array();
$inputImage = "full_path_to_input_image.jpg";
//get image width and height
exec("full_path_to_image_magick/identify -format \"%w\n\%h\" {$inputImage}", $dimensionsArr);
?>
In above case width and height of image are stored in $dimensionsArr array.
Stuff 2: How to Watermark an Image using ImageMagick in PHP
$inputImage = "full_path_to_input_image.jpg";
$waterMarkText = "Watermark Me";
$outputImage = "full_path_to_output_image.jpg";
exec("full_path_to_image_magick/composite -watermark 30% -gravity northeast {$inputImage} {$waterMarkText} {$outputImage}");
?>
Stuff 3: Crop an image using ImageMagick in PHP
$firstImg = "first.jpg";
$output = "cropped.jpg";
exec("E:\wamp\www\im\imagemagick\convert {$firstImg} -crop 120x80+160+80 {$output}");
?>
In above code what we did is we cropped an image "first.jpg" to set width of 120px and height of 80px and the cropping is started from 160x80 coordinate. Or a better way of cropping images can be done as:
$firstImg = "first.jpg";
$output = "cropped.jpg";
exec("E:\wamp\www\im\imagemagick\convert {$firstImg} -shave 120x80 {$output}");
?>
What above code does is, it cuts the borders with the values we defined in width X height parameters.
Stuff 4: Rotate Image using ImageMagick in PHP
$firstImg = "first.jpg";
$rotated = "rotated.bmp";
exec("E:\wamp\www\im\imagemagick\convert {$firstImg} -rotate 90 {$output}");
?>
Above code we used value -rotate 90 to rotate the image at 90 degrees, and saved the output in bmp
Stuff 5: Resize an image using ImageMagick in PHP
exec("E:\wamp\www\im\imagemagick\convert first.jpg -resize 10% -sample 500% first_new.png");
?>
The above code resizes first.jpg to 10% of its actual size with sampling at 500% and saves to "first_new.png"
Stuff 6: Extract frames from animated image using ImageMagick in phpOne of the exciting feature that ImageMagick has is, we can extract the frames from an animated image file.
For that we can write a simple one-line command as:
$animatedImage ="animated.gif";
exec("E:\wamp\www\im\imagemagick\convert {$animatedImage} +adjoin animated_Image%02d.gif");
?>
The above code extracts frames from the animated image "animated.gif" and saves it as "animated_Image1.gif", "animated_Image2.gif", etc for each frame.
These are just some of the features that ImageMagick offers, there's lot more to check out. Above code snippets can act as a good start prior to delving into the ImageMagick commands in detail.