Snippets - Masks

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.

Warhol effect - Anthony 9/8/2008

This method should work with most versions; it could be altered to use the -layers option which started around 6.3.5 You can add as many colours to the array as you would like as the code will run them all. You may lose some colours depending on the order you define them.

  1. <?php
  2. // Colours to replace
  3. $mask_array = array('#5c89cc''#bb8600''#2c3516');
  4. // Allowance on colour to replace
  5. $fuzz_array = array('28''20''21');
  6. // Colour to replace with
  7. $replace_array = array('blue''yellow''green');
  8. // Get the size of the image and create a tempory background 
  9. // This can be useful to fill any transparent areas left.
  10. $size getimagesize('input.jpg');
  11. exec("convert -size {$size[0]}x{$size[1]} xc:blue background.miff");
  12. // Find the total number of masks to create
  13. $number count$mask_array );
  14. // Remove one as the array stats at 0
  15. $number $number -;
  16. // Setup the initial value for $cmd
  17. $cmd '';
  18. // Loop through and create the masks
  19. // The masks are going to overwrite the previously created mask on each loop
  20. // The mask will be added to the image on each loop before being overwitten
  21. // I have used the dedicated ImageMagick image formt
  22. for ( $i=0$i<=$number$i++ ) {
  23. $cmd "input.jpg -matte ( +clone -fuzz {$fuzz_array[$i]}% -transparent {$mask_array[$i]} )".
  24. " -compose DstOut -composite -fill {$replace_array[$i]} -fuzz 60% -opaque {$mask_array[$i]} ";
  25. exec("convert $cmd mask.miff");
  26. if ( $i == '0' ) { $cmd " background.miff mask.miff -composite "; }
  27. else { $cmd "output.miff mask.miff -composite "; }
  28. exec("convert $cmd output.miff");
  29. }
  30. // Create the final image
  31. exec("convert output.miff output.png");
  32. // Delete the tempory images
  33. $delete_array = array ( 'background.miff''mask.miff''output.miff' );
  34. foreach ( $delete_array as $value )
  35. {
  36. unlink$value );
  37. }
  38. ?>

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

Glitter text View

Warhol effect View