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.

Gravity option - Anthony 15/6/2008

The -gravity option is used to place the object in position. The object can be moved from the position set by gravity by using the geometry option as well e.g. -gravity Center -geometry +10+10

  1. <?php  
  2. $cmd "-size 500x300 xc:white -font ../verdana.ttf -pointsize 30 ".
  3. " -draw \"gravity center fill black text 0,0 \"Center\" \" ".
  4. " -draw \"gravity North fill black text 0,0 \"North\" \" ".
  5. " -draw \"gravity Northeast fill black text 0,0 \"Northeast\" \" ".
  6. " -draw \"gravity East fill black text 0,0 \"East\" \" ".
  7. " -draw \"gravity Southeast fill black text 0,0 \"Southeast\" \" ".
  8. " -draw \"gravity South fill black text 0,0 \"South\" \" ".
  9. " -draw \"gravity Southwest fill black text 0,0 \"Southwest\" \" ".
  10. " -draw \"gravity West fill black text 0,0 \"West\" \" ".
  11. " -draw \"gravity Northwest fill black text 0,0 \"Northwest\" \" ".
  12. " -bordercolor black -border 1x1 " ;
  13. exec("convert $cmd output.png"); 
  14. // In this case I think a neater way of writting the code is using more php as below.
  15. // Array of values
  16. $arr = array("North""Northeast""East""Southeast""South""Southwest""West""Northwest""Center");
  17. // Set $command
  18. $command ="";
  19. // Setup loop for the array
  20. foreach ($arr as $value) {
  21. // Create the individual draw commands and add them to the previous value of $command
  22. $command .= " -draw \"gravity $value fill black text 0,0 $value \" " ;  }
  23. // Create the image using php
  24. exec(" convert -size 500x300 xc:white -font ../verdana.ttf -pointsize 30 $command bordercolor black -border 1x1 output.png" );
  25. //exec("convert $cmd output.png"); 
  26. ?> 

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