I will in this tutorial show you how to create images with PHP, this uses inbuilt functions and doesn't need anything else then just PHP to work. Let's begin.
Creating the image
When creating the image you can do it in two ways, one way is to use mageCreate and just set the size of the image to create an empty image with the size you selected, the other way is to use ImageCreateFromPng(you can replace "Png" with "gif" or "jpeg" for other formats) to use an existing image with the format PNG as base(means your image will look like that one):
Empty Image:
Code:$myImage=ImageCreate(400,60);
With Image as base:
Code:$myImage=ImageCreateFromPng("http://www.website.com/Image_Base.png");
So in both ways we're creating an Image and stores it in a variable, this variable will now contain our image.
Adding colors
Then we need to create some colors, to create them we'll use ImageColorAllocate and our image variable together with 3 values creating a RGB color value. RGB is when you have a red(R) value from 0 to 255, a green(G) value from 0 to 255 and a blue(B) value from 0 to 255 to create one of 16,581,375 different colors. We'll then store them in variables to later be able to use them. If you created an empty image the first color you create will be added as the background color.
Code:$white=ImageColorAllocate($myImage, 255, 255, 255);
$black=ImageColorAllocate($myImage, 0, 0, 0);
$red=ImageColorAllocate($myImage, 255, 0, 0);
$green=ImageColorAllocate($myImage, 0, 255, 0);
$blue=ImageColorAllocate($myImage, 0, 0, 255);
Drawing forms
You can also draw geometrical forms on your images, there's some different you can choose from:
Code:ImageEllipse() ImageArc() ImagePolygon() ImageRectangle() ImageLine()
To use them we need to set the image variable to use, the form's position, the form's location and which color to use(a variable with a color we've already created). To create a rectangle we're first setting the first parameter to $myImage since that is our image, then the start x index and then the start y index, then the form's width and height and lastly the color, like $blue(since we've created it).
Code:ImageRectangle($myImage, 50, 20, 200, 15, $blue);
ImageRectangle($myImage, 300, 10, 300, 35, $green);
Now only the edge will be painted, to fill them with color to we have to add "Filled", like this:
Code:ImageFilledRectangle($myImage, 50, 20, 200, 15, $blue);
ImageFilledRectangle($myImage, 300, 10, 300, 35, $green);
Writing Text
We can also paint text on the image by using imagettftext. We will then be able to write a custom text in which color we want with what Font we want and we can also select size, position and angle of the text, you use it like this:
An example could look like this:Code:imagettftext(Image variable, Text size, Text angle, X position, Y position, Color, Font, Text);
Code:imagettftext($myImage, 12, 0, 5, 20, $black, "Fonts/Oblivious font.ttf", "This is a text");
You maybe noticed that you need to select your own font, if you don't already have any fonts you can link to you can easily find free fonts for download by just searching the net for "Free fonts"
Outputting the finished image
When you're image is completed you have to output it to the page, remember that this is the only thing you can have in the page.
So we first tell the page that it "is" an image( a png image to be exact) and then we outputs the image at the page, this things is done like this:
Note that you can replace "png" to "gif" or "jpeg" if you want another image format. And now we don't need the image in our variable anymore so then we'll do some cleanup, like so:Code:header("Content-type:image/png");
ImagePng($myImage);
Code:ImageDestroy($myImage);
Since this page is only showing the image you will just use the HTML IMG tag to show it on your site(or you can use it as the background to other elements instead). This could be done like this:
HTML Code:<img src="http://www.website.com/myPHPpage.php" alt="myImage" />
And that was everything for this tutorial, if you have any question just post them below![]()
Not bad, also see:
PHP:Tutorial - Getting to know GD
Grayscale Images with GD
+rep
This just screams to be used for creating a captcha. +rep
great +rep
Many thanks of your post.
This is superb.
The mind boggles at what a creative person could do with this...
+rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks