x10Hosting Forums

Corporate Free Hosting for the Masses.



Register

Reply
 
LinkBack Thread Tools Display Modes
x10 Lieutenant

marshian's Avatar

Join Date: Jan 2008
Posts: 392
Credits: 4,984
marshian is a glorious beacon of light
Location: Belgium

Send a message via MSN to marshian
Quote  
05-23-2008, 05:18 PM
Dynamic images with PHP

Introduction to dynamic images using PHP.
Based on the creation of this example:


Required knowledge:
- PHP at more then average level
- An image manipulation program (eg. GIMP or PSP X2)
- The ability to be able to use this program

Index:
== Introcuction ==
== Makeing it ==
== Tips & Tricks ==
== FAQ ==

== Introduction ==

Ok, in this tutorial I'm going to show you how to create dynamic images using PHP.
First of all, what are dynamic images?
- An example of dynamic images is the image shown above. This image shows your ip, isp, and some other text, but if you see it on different computers, or even on the same computer, but with a different ip, ... there's a difference in the image. You could try downloading the image, and then you'ld notice it doesn't work any more. 'Why doesn't it work any more?' is then the logical question. The answer is easy, you're not looking at an image, you're looking at the result of a PHP script.

'PHP is for webpages.'
- Clearly you've never heard of the header() function... This will be explained later on.

== Makeing it ==

First of all we should determine what we need. In this particular example, we want an image of a crazy (maybe a little stoned) guy and there's supposed to appear text on the image, depending on information from the user.
This tells us the following:
- We need a static background (of the crazy guy)
- We need dynamic text

So first things first, we can't add dynamic text to nothing, so we'll need to make a background. This is the part where you're supposed to be creative, but as I can't wait for you to make an image, I'll just give you my own background:

Note: in this example, we use png.

Ok, now we have a background, so we need text. How are we going to make this text? I've said it 10 times before, we're going to use PHP.

First comes the basic PHP, making some strings based on the user's IP. I suppose you know how to do this, so there's not really much of an explenation here.
(Note: the code of this example has been simplified a little, the origional source is MINE :p )

PHP Code:
<?php
$ip 
$_SERVER["REMOTE_ADDR"];
$ihn gethostbyaddr($ip);
$isp_temp explode("."$ihn);
$isp_temp array_reverse($isp_temp);
$isp "";
$continue true;
$i 0;
foreach(
$isp_temp as $piece) {
    if(
$i 4)
        
$continue false;
    if(!
$continue)
        break;
    if(
strlen($piece) > 3) {
        
$isp $piece.".".$isp;
        
$continue false;
        break;
    }
    if(
$i 0)
        
$isp ".".$isp;
    
$isp $piece.$isp;
    
$i++;
}
$secs rand(60120);
$text = Array(
        
"You're $ip.",
        
"You're also known as $ihn.",
        
"Your isp is $isp.",
        
"LocationScript is currently tracking you.",
        
"Tracking should be completed in approximatly $secs seconds.",
        
"This information has been logged.",
        
"Thank you, come again!"
    
);
?>
As we can all understand, the result of this script is text stored in $text.
key 0 : "You're "+your ip+"."
key 1 : "You're also known as "+your internet host name
key 2 : "Your isp is "+your isp+"."
key 3 : "LocationScript is currently tracking you."
key 4 : "Tracking should be completed in "+random int between 60 and 120+" seconds."
key 5 : "This information has been logged."
key 6 : "Thank you, come again!"

Of course, only 0-2 is actually true, we wouldn't want our poor user to end up being tracked by LocationScript and that information logged, do we?

Now we have text and an image, and it's time to add them together...

The first thing we would want to do is create an image object in PHP (it's not actually an image, it's a resource, but in this case the resource is an image, so we can safely assume it's an image object.)
As we've saved the background image as image/png, we'll need the function imagecreatefrompng().
PHP Code:
$image imagecreatefrompng("background.png"); 
Now for drawing strings on an image, PHP has a function imagestring().
So to draw all strings on the image:
PHP Code:
imagestring($image,,168 ,17 $text[0], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,32 $text[1], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,47 $text[2], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,62 $text[3], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,77 $text[4], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,92 $text[5], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,107 $text[6], imagecolorallocate($image0,0,0)); 
And the result of all this is... an empty page...
(This is the point where you should not give up.)
Of course we get an empty page. We spend a lot of time into creating the image, but what did we do with it? Nothing. We 'might' want to output the result of all our labour to the user, and this is the code to do so:
PHP Code:
return $image
Yeah, I know that's easy, you could have thought of that too, couldn't you? :p

Ok, let's run our example again...
And what do we get this time? IE8 is too smart, and makes an image of it... (I don't know about previous versions...)
But there's always at least one annoying browser if you made a code! This time, an example of them is FF. Any idea what you get if you open that in FireFox?
Exactly, a bunch of random gibberish, that vaguely represents... text?
Let's think about that for a second. Why would FF show text, and not an image? Of course, the standard MIME type of the output of a PHP script is text/html. IE8 is too smart and sees that it's in fact image/png, but FF beleives the headers and shows the file as text/html. We can safely assume all browsers rely on the MIME type, so we should change something about our code...
And once more, PHP has a function that solves our problem: header().
This function HAS to appear BEFORE ANY OUTPUT. If you fail to put it there, you'll get errors...
PHP Code:
header("Content-type: image/png"); 
This time, the output of our script is correct!

This is the full code:
PHP Code:
<?php
$ip 
$_SERVER["REMOTE_ADDR"];
$ihn gethostbyaddr($ip);
$isp_temp explode("."$ihn);
$isp_temp array_reverse($isp_temp);
$isp "";
$continue true;
$i 0;
foreach(
$isp_temp as $piece) {
    if(
$i 4)
        
$continue false;
    if(!
$continue)
        break;
    if(
strlen($piece) > 3) {
        
$isp $piece.".".$isp;
        
$continue false;
        break;
    }
    if(
$i 0)
        
$isp ".".$isp;
    
$isp $piece.$isp;
    
$i++;
}
$secs rand(60120);
$text = Array(
        
"You're $ip.",
        
"You're also known as $ihn.",
        
"Your isp is $isp.",
        
"LocationScript is currently tracking you.",
        
"Tracking should be completed in approximatly $secs seconds.",
        
"This information has been logged.",
        
"Thank you, come again!"
    
);
header("Content-type: image/png");
$image imagecreatefrompng("background.png");
imagestring($image,,168 ,17 $text[0], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,32 $text[1], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,47 $text[2], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,62 $text[3], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,77 $text[4], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,92 $text[5], imagecolorallocate($image0,0,0));
imagestring($image,,168 ,107 $text[6], imagecolorallocate($image0,0,0));
return 
imagepng($image);  
?>
Note: any character too much, and I mean any one character can make your image change into a red X. Don't put a space, tab, enter or any other character before or after the <?php and ?> tags!

== Tips & Tricks ==

If you call your PHP file "index.php", you can put it (and any other file required for the image) in a directory that's named like an image. For example the directory '/ipimg.png/'. You can then link to 'yoursite.com/ipimg.png', which looks better then 'yoursite.com/ipimg.php'. And it's certainly useful if you need more then one file to make your image. (For example 'index.php' and 'background.png': it's cleaner to put it in one directory.)

PHP has a lot more utils to work with images then the ones I've already mentionned. For the full list, and more information, go to http://www.php.net/manual/en/intro.image.php. (Thanks to verbsite)

No more tips & tricks at this point.

== FAQ ==

None at this point.


This is the end of this tutorial about dynamic images with PHP. I hope you learned something from it, but now I'm going to bed.

- Marshian
__________________
If this post was helpful, click on the or donate me credits ^^

Last edited by LHVWB; 05-29-2008 at 05:24 PM. Reason: Added returning of imagepng(); function
Reply With Quote
marshian is offlineReport Post
Account Manager

sohailamir52's Avatar

Join Date: Sep 2007
Posts: 2,796
Credits: 1,301
sohailamir52 has a reputation beyond repute
Location: Dubai, UAE

Quote  
05-24-2008, 01:49 AM
Re: Dynamic images with PHP

Nice tutorial! I'll give this a 9/10 for making everything simple and easy to understand.
__________________

SohailTech MANAGER/X10 MODERATOR
•NEED DESIGNING SERVICES/ADVERTISING? CLICK HERE
•PLEASE DO NOT PM ME OR ANY OF THE STAFF FOR HELP
•CLICK THE TO GIVE ME REP

NEED HELP?
SERVER STATUS - INSERTING ADS - PHP PROBLEMS? - HOW DO CREDITS WORK? - x10 COMMANDMENTS - TERMS OF SERVICE - MARKETPLACE RULES
Reply With Quote
sohailamir52 is offlineReport Post
Lord Of The Keys

LHVWB's Avatar

Join Date: Jan 2008
Posts: 1,280
Credits: 9,231
LHVWB is a jewel in the rough
Location: Australia

Send a message via MSN to LHVWB
Quote  
05-24-2008, 03:59 AM
Re: Dynamic images with PHP

I have worked with dynamic images when I was creating some family tree software but this is a quite good tutorial, nice and easy to understand.

Rep++

For the more advanced programmer you can do a whole lot more with images such as change fonts, draw shapes and etc. Below is the official php manual on the image capabilities of php.

http://www.php.net/manual/en/intro.image.php

Last edited by LHVWB; 05-24-2008 at 03:59 AM.
Reply With Quote
LHVWB is offlineReport Post
x10 Lieutenant

marshian's Avatar

Join Date: Jan 2008
Posts: 392
Credits: 4,984
marshian is a glorious beacon of light
Location: Belgium

Send a message via MSN to marshian
Quote  
05-24-2008, 04:19 AM
Re: Dynamic images with PHP

Quote:
Originally Posted by verbsite View Post
I have worked with dynamic images when I was creating some family tree software but this is a quite good tutorial, nice and easy to understand.

Rep++

For the more advanced programmer you can do a whole lot more with images such as change fonts, draw shapes and etc. Below is the official php manual on the image capabilities of php.

http://www.php.net/manual/en/intro.image.php
Yeah, I forgot to put the link to the PHP's image functions...
Thanks for telling me, I'll add it ^^
__________________
If this post was helpful, click on the or donate me credits ^^
Reply With Quote
marshian is offlineReport Post
x10Hosting Member

Join Date: May 2008
Posts: 2
Credits: 4
knmtftw is on a distinguished road
Quote  
05-24-2008, 05:02 AM
Re: Dynamic images with PHP

This image looks a lot like danasoft's one^^. Nice tutorial btw!
Reply With Quote
knmtftw is offlineReport Post
x10Hosting Member

Join Date: Apr 2008
Posts: 49
Credits: 490
tcmstr134 will become famous soon enough
Quote  
05-26-2008, 10:06 AM
Re: Dynamic images with PHP

Very nice! I will try it out later when I have an image to put as a background.
__________________


Reply With Quote
tcmstr134 is offlineReport Post
x10 Elder

diabolo's Avatar

Join Date: Nov 2007
Posts: 533
Credits: 9,418
diabolo has a spectacular aura about
Quote  
05-26-2008, 12:06 PM
Re: Dynamic images with PHP

you might also want to talk about fonts and use a destroy image after to remove it from memory...

I also made a tut a while ago, you can check it out in my sig
__________________
AnAsian'sCreations - Spinning since 2007
--
If you sorry for me, please add to my rep or donate credits.
-
Check out my Tutorials
[JS] Load Object Last | [php][GD] Creating A Dynamic Signature

Last edited by diabolo; 05-26-2008 at 12:06 PM.
Reply With Quote
diabolo is offlineReport Post
x10 Lieutenant

marshian's Avatar

Join Date: Jan 2008
Posts: 392
Credits: 4,984
marshian is a glorious beacon of light
Location: Belgium

Send a message via MSN to marshian
Quote  
05-26-2008, 12:39 PM
Re: Dynamic images with PHP

Quote:
Originally Posted by diabolo View Post
you might also want to talk about fonts and use a destroy image after to remove it from memory...

I also made a tut a while ago, you can check it out in my sig
I might do that, and while I'm doing that, I could also talk about making different layers, setting filters, etc... but this isn't ment to be a tutorial on all possible ways to manipulate an image with php, it's only an introduction to it, so it must remain simple.

And destroying the image isn't really required, as you'ld have to put that command on the last line of the code, and the script is completed then, so php will free the used memory anyway...
__________________
If this post was helpful, click on the or donate me credits ^^
Reply With Quote
marshian is offlineReport Post
x10Hosting Member

S t e i n's Avatar

Join Date: May 2008
Posts: 6
Credits: 72
S t e i n is on a distinguished road
Location: Philippines

Send a message via MSN to S t e i n Send a message via Yahoo to S t e i n
Quote  
05-27-2008, 02:20 AM
Re: Dynamic images with PHP

Oh my god
thank you sir for the tutorial. it helped me a lot thanks!
Reply With Quote
S t e i n is offlineReport Post
x10 Sophmore

hamsn's Avatar

Join Date: Mar 2008
Posts: 155
Credits: 2,745
hamsn is on a distinguished road
Location: Global

Send a message via AIM to hamsn Send a message via MSN to hamsn Send a message via Yahoo to hamsn
Quote  
05-27-2008, 03:03 AM
Re: Dynamic images with PHP

good tutorial
and also the location tracker
its also good if you post the ip tracker code too
in tutorials
anyways the main programming tutorial was excellent!
Reply With Quote
hamsn is offlineReport Post
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
SUHOSIN - Use of eval is forbidden MaestroFX1 Free Hosting 19 03-07-2008 03:42 AM
Important PHP Information Bryon News and Announcements 0 11-21-2007 03:08 PM
Images in PHP Coding? TheJeffsta Scripts & 3rd Party Apps 4 05-19-2006 08:09 PM
[PHP] PHP and interacting with HTML forms Bryon Tutorials 4 08-02-2005 01:45 PM


All times are GMT -5. The time now is 05:18 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

Mobile Phone | Credit Cards | Home Loan | MPAA | Buy Anything On eBay