BACKGROUNDS

This is the first in a series of tutorials I will be writing on using PHP and Image Magick from the command line. In order to use these tutorials your server must have PHP and Image Magick installed. You can check with your server's administrator if you are not sure. These lessons will cover the basics. For a more indept study and examples you can read the tutorials at Image Magick on using it from the command line.
An image needs a canvas, so this first lesson will cover how to make backgrounds using IM and how to upload that background from IM right into your directory from a script inside your directory. You don't have to know PHP to do this but I will be using PHP codes throughout with an explantion of their function.
To open and close a PHP file you will need these two codes: <? to open the script and ?> to close the script. Inbetween those codes you will need variables, one for each piece of information that you will pass to IM about your image (height, width, color, etc.), in order to complete your request. A variable can be any name you wish to name it preceded by a dollar sign (which is a PHP variable) with distinct image magick commands for each variable.
Basically what you need to know is the size of your background, the color, and whether it should be a solid back or a gradient or a pattern, and what you would like to name that background, as well as whether it should be a gif, jpg, or png. So this is what we use for the variables. Here is a sample of a complete script request.
<?
$SIZE="-size 100x100";
$XC="xc:green";
$OUT="Examples/greenXC.gif";
exec ("/usr/bin/convert $SIZE $XC $OUT");
?>
There are a few rules required by the scripting for this to work. The commands following the equal sign in each variable must be between quotes and must end with a semicolon. Looking at each line individually this is our request.
$SIZE="-size 100x100":
I named my first variable $SIZE and -size is the IM specific command followed by the actual widthxheight dimension.
$XC="xc:green";
I named the variable for an XC background. Tells IM to make an XC:green. Note there are no spaces after the colon.
$OUT="Examples/greenXC.gif";
$OUT is the variable for the name and type of background I want. It will be sent to a subdirectory (in the directory the script is in) called Examples and it will be an image named greenXC.gif, and it is a gif image.
exec ("/usr/bin/convert $SIZE, $XC, $OUT");
The last line is what calls IM (exec) and sends the info. /usr/bin is the path to IM. You may need to verify the path to IM on your server with the administrator, but I know that this will work at Arbor and Z-Box. Convert is an IM specific command which is followed by our variables. What the script does is read the variable and performs the functions. Note the quotes and the parentheses and the semicolon are all necessary for it to work.
I could have gotten the same results without using any variables simply by using the following script

<?
exec ("/usr/bin/convert -size 100x100 xc:green greenXC.gif");
?>

However using the variables has some advantages. One major advantage is that I can take those variable and create a form that will submit a request to IM without my ever having to go in to the script and make changes to it. It also has some advantages if you want to change a variable. You can work on that variable without having to worry about deleting or changing any other variables. If it doesn't work you know where to look for the error.
Now armed with this script we can get an xc in any color that IM offers including a transparency. We can change the xc to a gradient or a plasma or a pattern. Below are some sample scripts along with the output.
<?
$SIZE="-size 100x100";
$XC="xc:green";
$OUT="Examples/greenXC.gif";
exec ("/usr/bin/convert $SIZE $XC $OUT");
?>
<?
$SIZE="-size 100x100";
$XC="xc:none";
$OUT="Examples/transpXC.gif";
exec ("/usr/bin/convert $SIZE $XC $OUT");
?>
<?
$SIZE="-size 100x100";
$GD="gradient:blue";
$OUT="Examples/blueGD.gif";
exec ("/usr/bin/convert $SIZE $GD $OUT");
?>
<?
$SIZE="-size 100x100";
$GD="gradient:blue-orange";
$OUT="Examples/blueorangeGD.gif";
exec ("/usr/bin/convert $SIZE $GD $OUT");
?>
<?
$SIZE="-size 100x100";
$GD="gradient:firebrick-none";
$OUT="Examples/firenoneGD.gif";
exec ("/usr/bin/convert $SIZE $GD $OUT");
?>
A plasma is a random image so even if you used the same variables every time the image will not be exactly the same. A color that uses all three channels will produce greater results than a color that only uses one channel.
<?
$SIZE="-size 100x100";
$PL="plasma:fractal";
$OUT="Examples/plasma.gif";
exec ("/usr/bin/convert $SIZE $PL $OUT");
?>
<?
$SIZE="-size 100x100";
$PL2="plasma:yellow";
$OUT="Examples/plasma2.gif";
exec ("/usr/bin/convert $SIZE $PL $OUT");
?>
<?
$SIZE="-size 100x100";
$PL="plasma:goldenrod-peachpuff";
$OUT="Examples/plasma3.gif";
exec ("/usr/bin/convert $SIZE $PL $OUT");
?>
<?
$SIZE="-size 100x100";
$PT="pattern:checkerboard";
$OUT="Examples/checker.gif";
exec ("/usr/bin/convert $SIZE $PT $OUT");
?>
<?
$SIZE="-size 100x100";
$PT="pattern:hexagons";
$OUT="Examples/hexagons.gif";
exec ("/usr/bin/convert $SIZE $PT $OUT");
?>


Next month's lesson will be on Annotate (Labels or Images).

HERE IS A ZIP OF A SAMPLE SCRIPT AS WELL AS A FORM FOR CREATING BACKGROUNDS. IT WILL PLACE THE BACKGROUND IN TO THE DIRECTORY WHERE THE SCRIPT IS. EACH IMAGE HAS ITS OWN URL. YOU CAN SEND THE IMAGE TO A SUBDIRECTORY BY CHANGING THIS LINE ON THE SCRIPT:

exec ("/usr/bin/convert $GSIZE $GSTYLE $GPATTERN $OUT");

TO INCLUDE THE DIRECTORY NAME AS BELOW:

exec ("/usr/bin/convert $GSIZE $GSTYLE $GPATTERN Temp/$OUT");

IM BACKGROUNDS

This is a text version of the Form if you prefer it to the zipTEXT

© 2008 LingoLinda.com