Snippets - Modifications

Snippets index page

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.

For more examples check out the examples section of the website

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.

  1. <?php
  2. // Original image
  3. $image '../original_images/sunflower.jpg';
  4. // Amount of images in x
  5. $qty_x '4';
  6. // Amount of images in y
  7. $qty_y '3';
  8. // Get the size of the original image
  9. $size getimagesize($image);
  10. // Size to crop the images to 
  11. //  Code modified as per sugestion of Dave to prevent problems with left over parts of the divided image).
  12. $crop_x round$size[0]/$qty_x 0.5);
  13. $crop_y round$size[1]/$qty_y 0.5 );
  14. // Canvas for combined images - it will be trimmed to the finished image size in the code
  15. $width = ( $size[0] * 1.5 );
  16. $height = ( $size[1] *1.5 );
  17. // Crop the image - saving as a miff format
  18. exec("convert $image -crop {$crop_x}x{$crop_y} image-%d.miff"); 
  19. // Remove the .miff from the filenames and replacing it with a bmp to save overwriting the original
  20. foreach (glob("*.miff") as $filename) {
  21. $file str_replace('.miff'''$filename );
  22.     exec("convert $filename -background black +polaroid $file.bmp"); 
  23. }
  24. // Delete tempory images
  25. foreach (glob("*.miff") as $filename) {
  26. unlink($filename); 
  27. }
  28. // Set $command variable
  29. $command "";
  30. // Loop through generating the image layout
  31. for ($j 0$j $qty_y$j++) { 
  32.   for ($i 0$i $qty_x$i++) {    $n $j>$qty_x*($j)+$i $i
  33.   $offset_x= ( ($crop_x $i ) + rand(225));
  34.   $offset_y=( ($crop_y $j ) + rand(225));
  35.   $command .= " image-$n.bmp -geometry +$offset_x+$offset_y -composite" ;  } 
  36.   }
  37. // Produce the combined image - if saving as a png you could use xc:none for a transparent background
  38. exec"convert -size {$width}x{$height} xc:#262b38 $command -trim combined.jpg"); 
  39. // Delete tempory images
  40. foreach (glob("*.bmp") as $filename) {
  41. unlink($filename); 
  42. }
  43. ?>
  44. <img src="combined.jpg"> 

For more examples and information check out the main Imagemagick section.

Add borders View

Apply rounded corners to an image View

Convert image to black and white View

Dividing an image View

Improve photos colours View

Pencil sketch View

Polaroid effect View

Transparent gradiant View

Venetian blind effect View