Support Forums

Full Version: Creating Dynamic images with PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Welcome and enjoy!

Intro:
Since I don't have enough Posts to update my signature, I've decided to show you how to create dynamic signature with PHP

Demo Wrote:[Image: image.php]

The day number is updated on every load.
Try refreshing the page and look at the time in the right bottom corner

Requirements:
  • Basic understanding of PHP and it's Syntax.
  • GD Library enabled Server
  • And time to read the more...

Functions:
  1. imagecreate()
  2. imagecolorallocate()
  3. imagestring()
  4. header()
  5. imagepng()

Let us start writing our PHP file.
I've named mine image.php, you can name yours different.

We start first with defining the width and height of the image.
PHP Code:
$width 450;
$height 70
I've set my image width to 450 pixel and height to 70 pixel.

Now we use the function imagecreate() to create our image.
PHP Code:
$image imagecreate($width$height); 
imagecreate() accepts 2 parameters
1st = width of the image, 2nd = height of the image.

Next we will define Background color for the image.
PHP Code:
$background imagecolorallocate($image878787); 
imagecolorallocate() accept 4 parameters
1st = image resource that was create with the imagecreate() function.
2nd/3rd/4th are the RGB color value paramter

Now that we have the basic of our image let us add some text to it.
PHP Code:
$text "I hope this will not get new Hackforums";
$text_color imagecolorallocate($image176176176);
imagestring($image10'10''10'$text$text_color); 
$text_color is defined by the imagecolorallocate() function.
The function imagestring() accept 6 parameters.
1st = image resource that was create with imagecreate()
2nd = the font size
3rd = Left - distance from the left side of the image wich I've set to 10 pixel
4th = Top - distance from the top side of the image wich is also 10 pixel
5th = Text to add
6th = text color

And the last step is to define our content as image.
PHP Code:
header ("Content-type: image/png");
imagepng($image); 
With header() we define Content-type as PNG image.
imagepng() is then used to output given resource.
imagepng() accept 4 parameters
1st = image resource that was created with imagecreate()
2nd = This one is optional and is used to set the path and file name of the image - value must be string
3rd = Is also optional and it is used to define image compression quality from 0 = none - to 9 = Best
4th = filters - allows reducing the image size

Read More:
GD Docs
imagedestroy()
imagecreatetruecolor()
imagegif()
imageellipse()



Thank you for reading!
Ninja
Thank you for posting this tutorial.
Thanks....
I'm in love with GD.... Yeye
Awesome tutorial. I feel like adding awards to the site just for you.
(10-07-2009, 07:06 PM)Omniscient Wrote: [ -> ]Awesome tutorial. I feel like adding awards to the site just for you.

Thank you very much Omni. Roflmao

And if please include the Ninja award..
Ninja
Thanks for this tut very helpful!
Thank you too!
I'm glad I could help.
I always wondered how they do that ip creepy stuff >_>
(10-08-2009, 01:02 PM)Moudi Wrote: [ -> ]I always wondered how they do that ip creepy stuff >_>

Yes this is the way just add $_SERVER['SERVER_ADDR'] to the text you want to have on the image.
Whoa, thanks for sharing.
Pages: 1 2