Basic Tutorial: PHP GD, Image Function Library
With php gd is the image function library of php. The functions include in this library enable php users to creating image up to manipulating photos. PHP gd can create with file extension of jpg, gif, swf, tiff and jpeg2000.
Installation see http://www.php.net/manual/en/ref.image.php. If Your hosting service has enable gd library, you won’t need any extra works except in coding.
Lets get started, creating images from php
set the canvas:
CODE
$img = imagecreate(250, 80)
imagecreatethis function create and set a blank canvas with the wide of 250 pixels and height of 80 pixels.
setting the basic colors:
CODE
$black = imagecolorallocate($img, 0, 0, 0)
$white = imagecolorallocate($img, 255, 255, 255)
$red = imagecolorallocate($img, 255, 0, 0)
$green = imagecolorallocate($img, 0, 0, 255)
imagecolorallocate $img represent the canvas. the next three value is color value in rgb(0-255), you also set the color in hex (0×00 – 0xff).
drawing a something:
CODE
imagerectangle($img, 10, 10, 240, 70, $white)
imagerectangle this function create a rectangle a width of 230[240(x2) – 10(x1)] and a height of 60[70(y2) – 10(y1)]. $img the canvas, the first two value sets the starting point(x1, y1) . and last two number is the ending(x2, y2). And the last value is the color.
CODE
imagefilledrectangle($img, 20, 20, 60, 60, $red)
imagefilledrectangle this function create a filled rectangle with a color red. All values is the same as the imagerectangle function.
CODE
imagefilledellipse($img, 90, 40, 40, 40, $blue)
imagefilledellipse this one creates an ellipse(circular objects). $img is the canvas, the first two value(cx, cy) set the origin(center) of the ellipse. The third value set the width(horizontal width) and fifth one is the height(vertical width). Last but not the least the color value.
CODE
$corners = array(
0 => 190,
1 => 60,
2 => 210,
3 => 20,
4 => 230,
5 => 60,
);
imagefilledpolygon($img, $corners, 3, $white);
imagefilledellipse and the last drawing function I discuss is a polygon function, in the example there’s only 3 corners. The first value is the canvas, the second one is the corners in array, the value 3 is the number of vertices, and the last one is color.
Showing the graphics:
CODE
header(”Content-type: image/jpeg”)
imagejpeg($img)
header imagejpeg To output the image you must first send the appropriate header, in this example I use jpg extension and the content type is set to “image/jpeg”. And call the imagejpeg function to create the images and shown up to the browser. For other file type such as png(image/png), gif(image/gif), windows bitmap(image/vnd.wap.wbmp), check php manual for details.
And finally, use the imagedestroy function just to clear up the memory used by the imagecreate functions.
Final code should look like this.
CODE
<?php
$img = imagecreate(250,80);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$corners = array(
0 => 190,
1 => 60,
2 => 210,
3 => 20,
4 => 230,
5 => 60,
);
imagerectangle($img, 10, 10, 240, 70, $white);
imagefilledrectangle($img, 20, 20, 60, 60, $red);
imagefilledellipse($img, 90, 40, 40, 40, $blue);
imagefilledellipse($img, 150, 40, 70, 40, $green);
imagefilledpolygon($img, $corners, 3, $white);
header (”Content-type: image/jpeg”);
imagejpeg($img);
imagedestroy($img);
?>
Enjoy.
Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etcdel.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb
PHP: Writing A Generic Login And Register Script
Now there are basically 3 functions that a user management system provides: login, register, and protection. A user management system can do more than this but that is all that this tutorial will be covering. I will try to explain what I am doing as I go along but to fully understand what is happening you should have a basic knowledge of PHP, SQL, and HTML. This tutorial assumes you are using MySQL, adjust accordingly for a different DBMS.
First off lets define the database table where our users will be stored. Using phpMyAdmin run this statement to create our table:
CODE
CREATE TABLE tblUsers (
fldId INT NOT NULL AUTO_INCREMENT,
fldUsername VARCHAR(40) NOT NULL,
fldPassword VARCHAR(40) NOT NULL
);
Now a little explanation as to what this will do. It will create a table in your database called tblUsers with fields fldId, fldUsername, and fldPassword. The last two fields are self explanitory they contain the username and password of the user. The fldId is the user id automatically assigned by the database. For more information on the syntax read the MySQL documentation.
Lets continue by creating the script where our users will register. Open your favorite text editor and enter the following:
CODE
<?php
?>
This tells the webserver that we are starting a php code section. You can have more than one in a script and you can include HTML in your code files as well, more on that later. Lets create a function that will actually do the work of adding the user to the database. Lets call it registerUser, now enter the following in between the php tags:
CODE
function registerUser() {
mysql_connect(’server’, ‘username’, ‘password’, ‘database’);
$username = $_POST[’username’];
$password = md5($_POST[’password’]);
$sql = “INSERT INTO tblUsers (fldUsername, fldPassword) VALUES ($username, $password);”;
mysql_query($sql);
}
We now have a very basic registration function. Now we need to create the form the user will see. So below the ?> lets start our HTML. It should look a bit like this:
CODE
<html>
<head>
<title>Registration</title>
</head>
<body>
<form action=”<?php $_SERVER[’PHP_SELF’].”?register=true” ?>” method=”post”>
Username: <input type=”text” name=”username”>
Password: <input type=”password” name=”password”>
<input type=”submit” value=”Register”>
</form>
</body>
</html>
Now this HTML defines a form with 2 input fields and a button. The thing to look at though is the action attribute of the form tag. Here we have another php code section. This puts the path of the current script as our action with the variable register equal to true. We will deal with that in our code later. For now your code should look like this:
CODE
<?php
function registerUser() {
mysql_connect(’server’, ‘username’, ‘password’, ‘database’);
$username = $_POST[’username’];
$password = md5($_POST[’password’]);
$sql = “INSERT INTO tblUsers (fldUsername, fldPassword) VALUES ($username, $password);”;
mysql_query($sql);
}
?>
<html>
<head>
<title>Registration</title>
</head>
<body>
<form action=”<?php $_SERVER[’PHP_SELF’].”?register=true” ?>” method=”post”>
Username: <input type=”text” name=”username”>
Password: <input type=”password” name=”password”>
<input type=”submit” value=”Register”>
</form>
</body>
</html>
There is one more thing left to do. Handle the variable we passed to the script called register. Lets do that now. Here is the code:
CODE
<?php
if ($_GET[’register’] == ‘true’) {
registerUser();
}
function registerUser() { ….
Here we use an if statement to check and see if it has been set to true if it is we call the function we defined earlier
Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etcdel.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb « Previous Entries Next Entries »















