|
Filling Shapes This is not as hard as it may sound. In fact, it’s
merely a change of function names! You simply add the word “filled” into the
name of the function and it gets done! The arguments are the same as the outline
functions, so that’s not a problem. Here’s a couple of examples:
· imagepolygon() => imagefilledpolygon()
· imagerectangle() => imagefilledrectangle()
· imageellipse() => imagefilledellipse()
Etc... And that’s it! That’s all you need to do to fill a shape! Instead of
drawing an outline with [color], it will fill the shape outline with [color]!
Here’s a couple of examples from previous sections, using filled functions this
time:
Writing Text Onto An Image Writing text is just as simple as using the
shape functions - it uses an image identifier, and a colour identifier. It also
uses an x and y coordinate to define where to start writing.
imagestring() accepts these arguments: image identifier, font, [x], [y],
string, colour
The font argument basically represents the font size, you can change the font
using some other GD functions like imageloadfont() which I wont go over
in this tutorial.
So let’s give it a go. Let’s make a canvas, and output some text onto it!
<?php
$im = imagecreate(800, 400);
$words = "imagestring ex. by scrowler";
$bg = imagecolorallocate($im , 0,0,0);
$txt = imagecolorallocate($im , 255,255,255);
imagestring($im, 2, 200, 300, $words , $txt);
# outputting
?>
|
That’s it! You can extend yourself by giving the user a form
to input data, you can load up an image first, then put inputted text on it.
You can even apply wordwrap() formatting to define an edge to where
text can go!
Now… After all that time, let’s finally get on to outputting the image!
Outputting The Image Like the other functions, outputting images is a
real doddle! The first step is to decide which major format you wish to
output to: .jpg, .gif, .bmp or .png.
Now set the header content to that image type (you can use a form, as $_FILES[‘file’][‘type’]
will return the appropriate format for this, but let’s do it manually):
· JPG/JPEG: header(“Content-type: image/jpeg”);
· GIF: header(“Content-type: image/gif”);
· BMP: header(“Content-type: image/wbmp”);
· PNG: header(“Content-type: image/png”);
Right, after we’ve done all that, there’s only one more important step: actually
outputting it. We’ve now told the browser that this is an image, so let’s output
the content. To do this, we use image[type](). This function accepts
these arguments: image identifier, filename, quality.
Substitute the [type] in the above function to .jpeg, .gif, .png or .wbmp to
output to different formats. The image identifier argument is the variable name
containing all the image data you’ve processed through the script. The filename
specifies where you want to save the image, but we aren’t saving it, so just put
"" in there. The last argument sets the quality - this is a simple
integer from 1 to 100 specifying the output quality of the final image.
And you’re done! Have fun, here’s a couple of examples:
<?php
# jpeg
header(“Content-type: image/jpeg”);
imagejpeg($im, “”, 100);
# png
header(“Contept-type: image/png”);
imagepng($im, “”, 100);
# etc…
?>
|
If you have any problems, comments or suggestions for this
tutorial, don’t hesitate to contact me via the
BioRUST Creative Forums. You can
click my name below to visit my profile, where you can email or send a
private message. I will be happy to help you. I hope I have inspired you to
create a new kind of art. Goodbye and good luck!
PS. You can download the example files by clicking
here. ;)
- Tutorial written by Scrowler
|