[GD]Create An Image With Text


First off you will want to make a text file and name it stat.txt

On your server, it needs the CHMOD properties of 777 so that this script can write to it.

Next make a new file and name it image.php this will contain all the scripting necessary to create and show the image.

In this new file type up something like:

PHP Code
  1. <?php
  2. require "config.php"; //get the config file
  3. $message = file_get_contents($filename); //open the file and get its contents
  4. $pic=ImageCreate($width,20); //width defined in the config and height of 20
  5. $col1=ImageColorAllocate($pic,$c1r,$c1g,$c1b); //background color
  6. $col2=ImageColorAllocate($pic,$c2r,$c2g,$c2b); //text color
  7. $col3=ImageColorAllocate($pic,$c3r,$c3g,$c3b); //border color
  8. ImageFilledRectangle($pic, 0, 0, $width, 20, $col3); //border
  9. ImageFilledRectangle($pic, 1, 1, $width-2, 18, $col2); //fill
  10. ImageString($pic, 3, 5, 2, $message, $col1); //text
  11. Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); //expirey date
  12. Header("Content-type: image/png"); //change content type
  13. ImagePNG($pic); //create the picture
  14. ImageDestroy($pic); //destroy it
  15. ?>


Normally, forums and websites will not let you use .php extensions for images therefore you should open up your .htaccess file and in it add:

PHP Code
  1. RewriteEngine On
  2. RewriteRule ^gd_img.png$ image.php


This will fake out the forum or website into believing that it is a true image.

Now that that is done you need to create the file: config.php and in it type something like the following:

PHP Code
  1. <?
  2. // Configuration File
  3. // Modify the values to work for you
  4.  
  5.  
  6. // Name of the file where the message is
  7. // Was originally stat.txt
  8. $filename="stat.txt";
  9.  
  10. // Width of the image
  11. $width=300;
  12.  
  13. // Color of the background
  14. //c1r is red value, c1g is green, c1b is blue
  15. //values from 0 to 255
  16. $c1r=0;
  17. $c1g=0;
  18. $c1b=0;
  19.  
  20. // Color of the text
  21. //c2r is red value, c2g is green, c2b is blue
  22. //values from 0 to 255
  23. $c2r=255;
  24. $c2g=255;
  25. $c2b=255;
  26.  
  27. // Color of the border
  28. //c3r is red value, c3g is green, c3b is blue
  29. //values from 0 to 255
  30. $c3r=0;
  31. $c3g=128;
  32. $c3b=0;
  33.  
  34. // Secret code to access edit.php
  35. $sc="mypasswordgoeshere121";
  36. ?>


Thats that file, this is where the edit file, and the image file will pull information to create and edit the image.

Next create one last file and name it: edit.php
In this file you will be able to edit what the image says.
Now, open it up and type up something like:

PHP Code
  1. <?
  2. require "config.php"; //get config
  3. if($_POST['sc']!=$sc) //if the posted password doesn't equal config return to form
  4. {
  5. echo "<form action='edit.php' method='post'>
  6. New Message:<input type='text' name='new' /><br>
  7. Password:<input type='password' name='sc' /><br>
  8. <input type='submit' value='Enter' />
  9. </form>\n
  10. "; //the form
  11. }else{ //else...
  12. $file = fopen($filename,'w'); //open the file
  13. fwrite($file,$_POST['new']); //write to the file
  14. echo "Modified<br><br><img src='image.php' alt='".$_POST['new']." />"; //success
  15. } //end if
  16. ?>


If you encounter any problems please post them here :)
ShadowMage's Avatar
Author:
Views:
3,750
Rating:
There are currently no comments for this tutorial, login or register to leave one.