PhpCookies About Cookies
logo
Subscribe (full feed)
Enter your email address:
Delivered by FeedBurner
rss
» November 13, 2007 in   posted by koen
creative.jpgHey all. Today we are going to create our own "home" for PhpCookies! Or any other site you may want to build a house for, with PHP!

The house I made, was actually really simple, and didn't include much design, nor colors, but this post isn't to make your dream house, but to tell you about PHP GD. It's a so called library, which enables you to create or modify images, with stunning results. You can open up pictures that you already made, or make a new, completely dynamic image.

So, as you can see, I've added a picture to the post. It's a simple drawing of a house, with on the left side the text "PhpCookies.com
Make Sure You Subscribe"

So, what code caused this image to look like it does? (Note: below the code, I'll explain what it all does, although it's kinda self explanatory at some points).

<?php
header("Content-type: image/png");
$img = imagecreate(500,400);
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
// Create the base of the house
imagefilledrectangle($img, 0, 400, 400, 300, $black);
imagefilledrectangle($img, 200, 300, 400, 200, $black);
// Create the roof
imageline($img, 200,200,300,100,$black);
imageline($img, 400,200,300,100,$black);
// Fill in the roof
imagefill($img, 300, 150, $black);
// Display a name
imagestring($img, 4, 45, 200, "PhpCookies.com", $black);
imagestring($img, 4, 10, 225, "Make Sure You Subscribe", $black);
// Display the image, and remove from the memory
imagepng($img);
imagedestroy($img);
?>


So, the first part with the header function tells the browser to treat this script as an image. Else it would only produce all kind of weird characters. The next part creates an image with an width of 500px, and a height of 400px.

The first color "allocated" using the imagecolorallocate function, is used as the background color for the newly created image. The colors are in the order of R, G, B and can be anywhere between 0 and 255.

Next, we also define the black color, to use for the house and the text. Once we finished the preparation part, we create 2 rectangles, filled with the black color. The 4 numeric values are x-start, y-start, x-end, y-end. Same with the lines. The imagefill functions fills the image with the black color ($black, which we defined earlier). Any color that matches the dot filled in, will be colored the same. So, effectively, we've created a boundary for the filler with the 2 lines we created for our roof.

Then, we display two text strings, display the image as a png, and remove the image from the memory of the server.


Type in some text, to replace PhpCookies.com!




I just thought it'd be nice if you could replace the PhpCookies text I've written, and create yourself an image.

If there are more questions, or specific things you'd like to know, make sure you ask them through the comments section!



45n5 (946) says on November 13, 2007:
wow that's pretty damn cool

adding it to delicious for later ;-)