Snippets - Resize and crop
NOTE: Most of the IM snippets have been tested on IM version 6.3.5 There may be some differences if you are using the code on other versions.
Dividing an image - Anthony 17/6/2008
Make an image look as though it is made up from individual poleroid photos. The image rotations and spacings are random with certain limits so the final image will be slightly different everytime. I have modified the code slightly to use image formats that would not normaly be in the folder miff and bmp. If you have any bmp files in the same folder as this code they will be deleted. This will also improve the quality slightly as I was using jpg format for one set of tempory images before.
<?php// Original image$image = '../original_images/sunflower.jpg';// Amount of images in x$qty_x = '4';// Amount of images in y$qty_y = '3';// Get the size of the original image$size = getimagesize($image);// Size to crop the images to// Code modified as per sugestion of Dave to prevent problems with left over parts of the divided image).$crop_x = round( $size[0]/$qty_x + 0.5);$crop_y = round( $size[1]/$qty_y + 0.5 );// Canvas for combined images - it will be trimmed to the finished image size in the code$width = ( $size[0] * 1.5 );$height = ( $size[1] *1.5 );// Crop the image - saving as a miff formatexec("convert $image -crop {$crop_x}x{$crop_y} image-%d.miff");// Remove the .miff from the filenames and replacing it with a bmp to save overwriting the originalforeach (glob("*.miff") as $filename) {$file = str_replace('.miff', '', $filename );exec("convert $filename -background black +polaroid $file.bmp");}// Delete tempory imagesforeach (glob("*.miff") as $filename) {unlink($filename);}// Set $command variable$command = "";// Loop through generating the image layoutfor ($j = 0; $j < $qty_y; $j++) {for ($i = 0; $i < $qty_x; $i++) { $n = $j>0 ? $qty_x*($j)+$i : $i;$offset_x= ( ($crop_x * $i ) + rand(2, 25));$offset_y=( ($crop_y * $j ) + rand(2, 25));$command .= " image-$n.bmp -geometry +$offset_x+$offset_y -composite" ; }}// Produce the combined image - if saving as a png you could use xc:none for a transparent backgroundexec( "convert -size {$width}x{$height} xc:#262b38 $command -trim combined.jpg");// Delete tempory imagesforeach (glob("*.bmp") as $filename) {unlink($filename);}?><img src="combined.jpg">

For more examples and information check out the main Imagemagick section.
Clone ViewCrop an image View
Crop an image into pieces View
Dividing an image View
Keep minimum image size View
New image size in variable View
Resize if over certain size View
Resize keep aspect ratio. View
Resize no aspect ratio. View
Speed up jpg resize View