Thread: Create php image that shows own IP

Results 1 to 10 of 10
  1. #1 Create php image that shows own IP 
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    This is php which belongs to web development so don't say it belongs to graphics.

    This tutorial will do a image that displays own ip. Same as my signature has but without the background image.
    I will explain everything about the code in this tutorial but if you only want the code its end of the tutorial.


    To start, create a new file, and then add the php code tags

    Code:
    <?php
             
    ?>

    Then, we need to make the webpage tell the browser that this page is an image, so to do this, we would do:

    Code:
    header("Content-Type: image/png");
    So lets start and make a new image:

    Code:
    $im = @imagecreate(400, 20)
    or die("An error occured, try to refresh");
    If an error occurs, it will come up with "An error occurred, try to refresh"
    You can change the 400 to the width of the image and 20 to the height.

    Next we will add in the background:

    Code:
    $background_color = imagecolorallocate($im, 0, 0, 0);
    The 3 last numbers represent: Red, Green, Blue
    in this case, its black.

    And now we need to receive the IP of the user viewing the page:

    Code:
    $ip=$_SERVER['REMOTE_ADDR'];
    Now we will set the color for the ip and for the author:

    Code:
    $text_color = imagecolorallocate($im, 0, 0, 255);
    $author = imagecolorallocate($im, 255, 0, 0);

    Now lets write the strings/information we have got:

    Code:
    imagestring($im, 1, 5, 5,  "Your IP is: " . $ip, $text_color);
    the $im is the image, the 1 represents the font, and after that, the 2 numbers are for the X and Y coordinates.

    Last we will save the image to PNG and close the image:

    Code:
    imagepng($im);
    imagedestroy($im);

    Your final code should look now something like this:

    Code:
    <?php
    header("Content-Type: image/png");
    $im = @imagecreate(400, 20)
    or die("An error occured, try to refresh");
    $background_color = imagecolorallocate($im, 0, 0, 0);
    $ip=$_SERVER['REMOTE_ADDR'];
    $text_color = imagecolorallocate($im, 0, 0, 255);
    $author = imagecolorallocate($im, 255, 0, 0);
    imagestring($im, 1, 5, 5,  "Your IP is: " . $ip, $text_color);
    imagepng($im);
    imagedestroy($im);
    ?>
    Hope you'll like it, enjoy.
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    For future reference, you should probably get in the habit of using imagecreatetruecolor() rather than imagecreate().
    Reply With Quote  
     

  4. Thankful users:


  5. #3  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by Anthony` View Post
    For future reference, you should probably get in the habit of using imagecreatetruecolor() rather than imagecreate().
    Why is that? I prefer imagecreate().
    Reply With Quote  
     

  6. Thankful user:


  7. #4  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by VirtX View Post
    Why is that? I prefer imagecreate().
    They do the same thing, but imagecreatetruecolor basically gives you more colors and shades for creating images. It isn't important for a simple script like this, but it is useful to know the difference.

    Color depth - Wikipedia, the free encyclopedia
    Reply With Quote  
     

  8. Thankful users:


  9. #5  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by Anthony` View Post
    They do the same thing, but imagecreatetruecolor basically gives you more colors and shades for creating images. It isn't important for a simple script like this, but it is useful to know the difference.

    Color depth - Wikipedia, the free encyclopedia
    Alright, thank you.
    Reply With Quote  
     

  10. #6  
    Donator


    Join Date
    Jul 2010
    Posts
    1,827
    Thanks given
    18
    Thanks received
    76
    Rep Power
    97
    How do i add a background image because i dont want that black background, or can i make it transparent...?
    Since 2010
    Reply With Quote  
     

  11. Thankful user:


  12. #7  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Code:
    $background_color = imagecolorallocate($im, 0, 0, 0);
    Change 0,0,0 to color you wish. You can get colors for this from Paint or googleing it.
    Reply With Quote  
     

  13. Thankful user:


  14. #8  
    Donator


    Join Date
    Jul 2010
    Posts
    1,827
    Thanks given
    18
    Thanks received
    76
    Rep Power
    97
    Quote Originally Posted by VirtX View Post
    Code:
    $background_color = imagecolorallocate($im, 0, 0, 0);
    Change 0,0,0 to color you wish. You can get colors for this from Paint or googleing it.
    How do i make a image like your signature? could u pm me the code? will rep
    Since 2010
    Reply With Quote  
     

  15. #9  
    Donator

    ProFiles's Avatar
    Join Date
    May 2011
    Posts
    1,673
    Thanks given
    4
    Thanks received
    240
    Rep Power
    118
    Quote Originally Posted by Rocky View Post
    How do i make a image like your signature? could u pm me the code? will rep
    I didn't make it, but you can try some other code to add a background image url to it.
    Reply With Quote  
     

  16. #10  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    29
    Posts
    763
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by Rocky View Post
    How do i make a image like your signature? could u pm me the code? will rep
    Get a background image and use PHP to write text on top of it.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Image gallery needed [PHP]
    By David in forum Website Development
    Replies: 10
    Last Post: 11-29-2010, 01:06 PM
  2. PHP image uploader
    By Deadhead in forum Website Development
    Replies: 6
    Last Post: 08-16-2010, 06:33 PM
  3. Replies: 0
    Last Post: 07-14-2010, 11:53 PM
  4. Make an image in PHP [simple]
    By thatwasonce in forum Website Development
    Replies: 3
    Last Post: 07-11-2009, 07:23 PM
Tags for this Thread

View Tag Cloud

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •