Do you have a question? Post it now! No Registration Necessary. Now with pictures!
October 5, 2011, 10:54 pm
rate this thread
Re: simple image in PHP
This can be simple or complicated depending on what you want to do.
There are many image functions that let you do almost anything. You
can draw the image from scratch if you want.
Here is a simple example using a pre made jpeg pic and adding text
over it. Then saving it as jpg:
// HERE WE CREATE A "CANVAS" USING THE PREMADE IMG AS A BACKGROUND
// NOW CREATE A PALLET OF WHITE AND BLACK
// PLACE SOME TEXT ON THE CANVAS, OVER THE BG IMG
// TTF stands for True Type Font - use must use a .TTF font with this
// Explanation of the ImageTTFText:
// ImageTTFText( prm1, prm2, prm3, prm4, prm5, prm6, prm7, prm8);
// prm1 = img resource handle (the handle we created above with
// prm2 = size of text (in pixels)
// prm3 = angle of text (0 would be left to right)
// prm4 = left start position of text (pixels)
// prm5 = top position of text (pixels)
// prm6 = text color (from our pallet)
// prm7 = name of font file to use (this must have been uploaded to
the server... be sure to use correct spelling/caps - ex. "Times.TTF"
// prm8 = the text to draw onto the canvas.
ImageTTFText($im, 22, 0, 75, 25, $blk, "Times.ttf", "Some text");
// Here I a, drawing to my canvas the words "Some Text". It will be 22
pixels tall (cap letters). 0 degrees angle. start at 75 pixels from
canvas' left edge. 25 pixels down from canvas' top edge. The text will
// Here we will output
// HEADER will designate the output as a jpeg
// Next img is "generated"
// note that you don't have to actually SAVE an img to use it.
// if we simply link the HTML img src= to the PHP code that creates an
img. The code will act as an img though it is created "on the fly".
// By providing a file name we can save the img to a file.
// The last parameter is the quality of the image (compression). 100 =
best, 0=worst (small file)
ImageJpeg($im, "myJpeg.jpg", 100);
// Here the resource handle is released back so memory is reclaimed.
There is also png and other img type functions.
You can download a help file for PHP.
I have no doubt that if I have missed something, wrong about
something, made a typo, or presented something wrong - someone will
point it out :)