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. | ||||||||
| ||||||||
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"); ?> |