Basic command line code format using php ( IM command line processing ).

( Another site with some information of php use: IM examples )

<?php
exec
("convert input.jpg output.png");
?>

exec - Execute an external program ( ImageMagick is not built into php ).

convert - The program within ImageMagick to use. In some cases you need to give the path to the convert program depending on how ImageMagick is setup on the server e.g. /usr/local/bin/convert.

input.jpg - Read the image to be modified into the memory. This must be done first except when using Composite.

output.png - The image name and format to save as.

This piece of code will read input.jpg from the folder the code is in and save as output.png in the same folder. All it is doing is changing the image format from a jpg to a png.


You can use an image from a different folder and save to a different folder:

<?php
exec
("convert ../folder_back/output.png folder_below/input.jpg ");
?>

You can use variables for the image paths:

<?php
$input 
"../folder_back/output.png";
$output "folder_below/input.jpg";
exec("convert $input $output");
?>

Using fonts.

When using fonts you can use the fonts that were built into ImageMagick when it was installed:

<?php
exec
("convert input.jpg -font Arial -pointsize 80 \
-draw \"gravity center fill black text 0,0 'Rubblewebs' \" output.png"
);
?>

Upload a font yourself and use the path to that font:

<?php
exec
("convert input.jpg -font ../fonts/jd.ttf -pointsize 80 \
-draw \"gravity center fill black text 0,0 'Rubblewebs' \" output.png"
);
?>

Or leave -font out altogether and go with the ImageMagick default font:

<?php
exec
("convert input.jpg -pointsize 80 -draw \"gravity center fill black \
text 0,0 'Rubblewebs' \" output.png"
);
?>

-pointsize is used to set the font size.
-draw is being used to write the text
gravity center places the text in the middle of the image
fill black use black text
text tells the code that we are writing some text ( draw can be used for a lot of other items )
The 0,0 can be used to offset the text from the location set by gravity
The text to write is inside the ''

NOTE that gravity and fill if they are used outside of the -draw " " command need to be written -gravity and -fill.
Also the " in this case needs escaping so becomes \"


Notes about formatting the code

  • If writing the code in the format exec(" code "); any other " need escaping \"
  • If you use ( ) with the code it will need escaping with \ so it becomes \( \)
  • When using ( ) you need to use a white space Works: ( code ) Fails: (code)
  • The operators need a - you can not leave a space Works: -gravity Fails: - gravity
  • When using colours specialy RGB you need to be careful of white spaces Works: rgb\(0,255,255\) or 'rgb(0,255,255)' or 'rgb( 0, 255, 255 )' Fails: rgb\(0, 255, 255\)
  • To write the code over to the next line ( not with word wrap ) you need to use \ at the end of the line BUT that will need escaping and so will become \\

Another way to write the code

Writing the code this way means you can write the code over different lines and it saves using the \\ which makes the code easyer to read

<?php
 $color
="red";
  
$image="rose.jpg";
  
$size="70x46";
  
$string="A Rose by any Name";

  
exec("convert -background none -fill $color -gravity center" .
            
" -font Candice -size $size caption:'$string'" .
            
" \( $image -negate -flip \) +swap -composite" .
            
" output.jpg" );
?>

This is the latest method of writting the code I use; it means you can echo the command to confirm it contains what you expect. Particularly useful if the command contains variables.

<?php
 $color
="red";
  
$image="rose.jpg";
  
$size="70x46";
  
$string="A Rose by any Name";

    
$smd "-background none -fill $color -gravity center" .
            
" -font Candice -size $size caption:'$string'" .
            
" \( $image -negate -flip \) +swap -composite";

// Display the contents of the command and when the publishing the code comment this out.
        
echo "<br> $cmd <br>";
        
  
exec("convert $cmd output.jpg" );
?>