+ Reply to Thread
Results 1 to 2 of 2

Thread: Generating a "What is your IP" Image

  1. #1
    KentonBomb's Avatar
    KentonBomb is offline x10Hosting Member KentonBomb is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    42

    Talking Generating a "What is your IP" Image

    I originally posted this in my blog, but here is a tutorial for all you who want it
    Note: This is very long

    Since I made my sig tell people their IP Address, some people have asked me "How does it do it?"

    Well, This is to clear that up, and show you how to make your own!

    This tutorial is based off of a tutorial by Dethredic, but this generates the image by itself.

    First of all, this is what the end result will be:



    This is generated by the web-based scripting language known as PHP. You must have PHP installed on your server for this tutorial to work.

    If you are not experienced with PHP, This tutorial will be confusing. So I would suggest you find some beginner tutorials first.

    First, open your PHP code with

    Code:
    <?Php
    Now to add the first line.

    Code:
    <?php
    $img = imagecreate(220, 20);
    Simple. Loads the initial "image" into memory. The variable $img will be used alot later on. The image is 220x20 Pixels

    The next part initializes the different colors needed to print onto the image. We will be using White, and Green.
    Code:
    <?
    $img = imagecreate(220, 20);
    $white = imagecolorallocate($img, 255, 255, 255);
    $green = imagecolorallocate($img, 0, 150, 0);
    This might look a little complicated, but let me break it down.

    $white declares the variable for you. The ImageColorAllocate function simply turns an RGB value into a variable ($white, $green) That can be referenced later.

    The 3 parameters after the $img variable declares how much of each color (Red, Green, Blue, RGB) you want for that color. For a darker green, I want 0 Red, 150 Green, and 0 Blue. The maximum for that is 255. Don't ask me why. I have no idea If you wanted a purple color, you could say you wanted 150 Red, 150 Blue, and 0 Green. The combonations are endless.

    Now, we move on.

    Code:
    <?php   
    $img = imagecreate(220, 20);
    $white = imagecolorallocate($img, 255, 255, 255);
    $green = imagecolorallocate($img, 0, 150, 0);
    imagefill($img, 0, 0, $green);
    You can see we now added an ImageFill. Do i need to explain that?

    "But what are the 0, 0 for?"
    That is the X and Y coordinates to where you want to start filling the image. If you chose 10, 10, you would have a 10 Pixel line at the edge and the top of the image not filled. We want the whole image filled, so we picked 0, 0, or the upper left hand corner of the image.

    Now we must obtain the IP address. That is by using this little enviorment variable called REMOTE_ADDR.

    Code:
    <?php   
    $img = imagecreate(220, 20);
    $white = imagecolorallocate($img, 255, 255, 255);
    $green = imagecolorallocate($img, 0, 150, 0);
    imagefill($img, 0, 0, $green);
    $ip = $_SERVER["REMOTE_ADDR"];
    You use the $_SERVER variable to declare enviormental variables. Nifty, Huh? Well, this then stores the IP Address of the client into the $ip variable. We will use this in the next step: Getting the next onto the image.

    Code:
    <?php   
    $img = imagecreate(220, 20);
    $white = imagecolorallocate($img, 255, 255, 255);
    $green = imagecolorallocate($img, 0, 150, 0);
    imagefill($img, 0, 0, $green);
    $ip = $_SERVER["REMOTE_ADDR"];
    imagestring($img, 4, 2, 2,  "$ip is your ip :P", $white);
    We used a function called "ImageString". It... Writes a string in the image. We used the string "$ip is your ip" with a smiley on the end. The "4" declares the font size. The 2 "2"'s are the positioning of the text. The $white simply declares we are using the color "White" as previously declared.

    Now you have the completed image in memory. We want it to display on a page though. So we must change the header of the PHP file. We do this by adding this line:

    Code:
    <?php   
    $img = imagecreate(220, 20);
    $white = imagecolorallocate($img, 255, 255, 255);
    $green = imagecolorallocate($img, 0, 150, 0);
    imagefill($img, 0, 0, $green);
    $ip = $_SERVER["REMOTE_ADDR"];
    imagestring($img, 4, 2, 2,  "$ip is your ip :P", $white);
    header("Content-type: image/png");
    This makes your web browser look at the file like a PNG image. It still doesn't show up though, so we must bring it out of memory, and onto our page.

    Code:
    <?php   
    $img = imagecreate(220, 20);
    $white = imagecolorallocate($img, 255, 255, 255);
    $green = imagecolorallocate($img, 0, 150, 0);
    imagefill($img, 0, 0, $green);
    $ip = $_SERVER["REMOTE_ADDR"];
    imagestring($img, 4, 2, 2,  "$ip is your ip :P", $white);
    header("Content-type: image/png");
    imagepng($img);
    And that displays the image proudly. We just close the PHP file with this final, completed code:

    Code:
    <?php   
    $img = imagecreate(220, 20);
    $white = imagecolorallocate($img, 255, 255, 255);
    $green = imagecolorallocate($img, 0, 150, 0);
    imagefill($img, 0, 0, $green);
    $ip = $_SERVER["REMOTE_ADDR"];
    imagestring($img, 4, 2, 2,  "$ip is your ip :P", $white);
    header("Content-type: image/png");
    imagepng($img);
     ?>
    Now, upload that to your webserver as [Anything].php. Now you can use it with an HTML IMG tag, or a BBCode IMG tag.

    Note: Some forums don't allow dynamic images with PHP extensions. To get around this, make the img tag point to:

    http://example.com/blah.php/whatever.png

    It doesn't matter what the name after the PHP url is. It will treat it just the same

    I hope you enjoyed this tutorial, and you learned something while you were at it

  2. #2
    Jesse's Avatar
    Jesse is offline Lord Of The Keys Jesse is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    Manila, PH
    Posts
    1,357

    Re: Generating a "What is your IP" Image

    Wow, thanks for this tutorial.
    x10HOSTING
    Member Since October 2007.

    JESSEMAN.ME
    | iMusicz.net

+ Reply to Thread

Similar Threads

  1. Image gallery help
    By mikel2k3 in forum Scripts & 3rd Party Apps
    Replies: 7
    Last Post: 09-04-2007, 11:06 AM
  2. Looking for a free pdf to image converter
    By Christopher in forum Off Topic
    Replies: 8
    Last Post: 01-11-2007, 03:50 PM
  3. Homepage image 200 points!!
    By __z|x__ in forum The Marketplace
    Replies: 3
    Last Post: 06-13-2006, 06:27 PM
  4. errors while attaching
    By mattspec in forum Feedback and Suggestions
    Replies: 0
    Last Post: 12-19-2005, 01:50 PM
  5. November Desktop
    By n4tec in forum Off Topic
    Replies: 12
    Last Post: 11-08-2005, 07:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers