Snippets - With php

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.

Row of images - Anthony 15/7/2008

This code is based on a shell script by Anthony Thyssen and creates a row of images and a associted "colour map". The two images can be used by the php function ImageColorAt( ) to create a type of Imagemap. The images are resized; have their file name added and modified to look like a poleroid photo. This needs a later version of Imagemagick as it uses the -layers operator.

  1. <?php
  2. // Code modified from a shell script by Anthony Thyssen
  3. // Default location for the first image
  4. // This can be any number as I correctly handle the formating
  5. // of images which may generate given a negative offset.
  6. $center "0"
  7. // Distance to the next images center. This can be negative!
  8. $offset "100";  
  9.     
  10. // Final image background canvas color    
  11. $background "LightSteelBlue";  
  12. // Setup image names for laying out the images
  13. $layer_image "image.miff";
  14. $layer_map "layer_map.miff";
  15. // Create an array of all the png images in the folder
  16. $image_array glob"*.png" );
  17. // Find how many images there are in the array and remove 1 as we start at 0 not 1    
  18. $count count$image_array );
  19. $count $count -1;
  20. // Create an array of colours to use for the map
  21. $colours = array( 'Red''Blue''brown''Yellow''Green' );
  22. // Loop through the array of images    
  23. for ( $i=0$i<=$count$i++ )    
  24.      {
  25. // Remove the .png extension - this will be replaced by a miff extension
  26. // Need to do as we need to rename some images to prevent overwritting
  27.         $file str_replace'.png'''$image_array[$i] );
  28.      
  29. // Create tempory image name formats    
  30.         $temp_map "0.miff";
  31.         $temp_image "1.miff";
  32.         
  33. // Modify the image - thumbnail, polaroid image to a temp file.
  34. // I used super-sampled polariod to make the result better
  35. // This could have been done in a previous step to this loop
  36.         $cmd " -size 500x500 $image_array[$i] -matte -thumbnail 240x240".
  37.         " -set caption %t -bordercolor Lavender -background none".
  38.         " -pointsize 12 -density 96x96 +polaroid -resize 50% -write $temp_image".
  39.         " -channel A -threshold 50% +channel".
  40.         " -fill {$colours[$i]} -colorize 100%  $temp_map";
  41. // Carry out the command        
  42.         exec("convert $cmd");
  43.     
  44. // Now get image size, half it, and determine its virtual canvas
  45. // location so as to place it's center at the current position.
  46. // Could be done using getimagesize(  )
  47.         $xpos exec("convert $temp_image -format \"%[fx: $center - w/2 ]\" info:");
  48.         $ypos exec("convert $temp_image -format \"%[fx: - h/2 ]\" info:");
  49. // $xpos may be positive, in which case it needs to have a '+' sign
  50. // $ypos is always negative ( but check its formating anyway )
  51.         if ( $xpos ) {    $xpos "+".$xpos;    }
  52.         if ( $ypos ) {    $ypos "+".$ypos;    }
  53.         $pos "$xpos$ypos";
  54.     
  55. // Create new tempory images  with the position offset -  overwritting the original
  56.         exec("convert -page $pos $temp_image $temp_image");
  57.         exec("convert -page $pos $temp_map $temp_map");
  58. // Increment position for next image  
  59.         $center $center $offset;
  60. // Build up the images into the final order        
  61.         exec("convert -background $background $layer_image $temp_image -layers merge $layer_image");
  62.         exec("convert -background black $layer_map $temp_map -layers merge $layer_map");        
  63.     }
  64.     
  65. // Convert the images from a miff format to a browser readable one    
  66. exec("convert $layer_image +repage output.jpg");
  67. exec("convert $layer_map +repage output_map.gif");
  68.     
  69. // Remove the tempory images
  70.     foreach ( glob"*.miff" ) as $filename ) {
  71.         unlink$filename ); 
  72.         }
  73. ?>
  74. <img src="output.jpg">
  75. <br>
  76. <img src="output_map.gif">

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

Batch file View

Check size before resizing View

Dividing an image View

Fonts Installed view View

Get image data into a variable 1 View

Get image data into a variable 2 View

Gravity option View

New image size in variable View

Row of images View

Setup Information View

Use an array View

Warhol effect View