Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating Dynamic images with PHP
#1
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
Reply
#2
Thank you for posting this tutorial.
Reply
#3
Thanks....
I'm in love with GD.... Yeye
Reply
#4
Awesome tutorial. I feel like adding awards to the site just for you.
Superman I am here to rescue you.
This is Support Forums not Support PMs.  Do not PM me for support unless it's private and site related.
Reply
#5
(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
Reply
#6
Thanks for this tut very helpful!
Reply
#7
Thank you too!
I'm glad I could help.
Reply
#8
I always wondered how they do that ip creepy stuff >_>
Reply
#9
(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.
Reply
#10
Whoa, thanks for sharing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 936 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Dynamic Text Generator TalishHF 5 2,113 01-16-2012, 02:24 PM
Last Post: Gaijin
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,660 10-10-2011, 01:00 PM
Last Post: Greyersting
  Creating a simple PHP/MySQL login script ndee 16 10,404 05-14-2011, 02:18 AM
Last Post: モrainee
  Creating new pages in SMF Project Evolution 3 5,666 01-10-2011, 06:28 AM
Last Post: w00pz

Forum Jump:


Users browsing this thread: 2 Guest(s)