Rubblewebs

THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.

I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.

IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.

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

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


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:


exec("convert ../folder_back/output.png folder_below/input.jpg ");


You can use variables for the image paths:


$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:


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:


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:


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


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


$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.


$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" );