| Forum |
|
|||||||
| Tutorials This section offers tutorials and How to's on just about anything related to computers and IT. Note: All tutorials are courtesy the posters and not verified by Digit |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Right Off the Assembly Line
Join Date: Jul 2005
Location: http:404 not found
Posts: 33
|
The rand function selects a random number between the 2 numbers you give it. So rand(1,25) will output a random number between 1 and 25 everytime the script is run. This function is usefull for making any dynamic random content and can be used for many different things. We are going to use it to select a random image. Ok, so lets go through the code and see what it does. Code: $rand = rand(1, 25); The first line of our code selects a random number between 1 and 25, so we have the image we are using straight away. Code: $image = "$rand.png"; The second line tells the server what image we are going to be using. Note the use of $rand followed by .png This creates the name of the image. 1.png up to 25.png Code: $im = imagecreatefrompng($image); The next line tells the server to create an image using the randomly selected png image. Code: header("Content-Type: image/png"); The next line is imprtant. It instructs the persons browser to expect binary data. If this isnt specified the browser will output text (like you see when you edit a picture in notepad). With this line the browser knows it is supposed to create an image from the information it recieves. Code: Imagepng($im,'',100); ImageDestroy ($im); The final two lines create the image and then throw it away. Now, we should have a script that looks like this Code: <?php $rand = rand(1, 25); $image = "$rand.png"; $im = imagecreatefrompng($image); header("Content-Type: image/png"); Imagepng($im,'',100); ImageDestroy ($im); ?> If you put that in a folder with 25 images called 1.png 2.png 3.png all the way up to 25. Each time the image is loaded it will out put a different image. |
|
|
| Advertisements. Register and be a member of the community to get rid of them. | |
|
Advertisement
|
|
|
|
#5 (permalink) |
|
Right Off the Assembly Line
Join Date: Jan 2009
Posts: 1
|
why create random avatar when you can create your own avatar?
http://www.ceveni.com/2009/01/create...tar-using.html |
|
|
|
|
#6 (permalink) |
|
AFK
Join Date: Oct 2006
Location: Bombay
Posts: 1,596
|
WTF!!!
What a historic bump Reported.......
__________________
Check out http://thefinal3rd.com, an Indian footie blog. Follow me on http://twitter.com/thewisecrab Fishdumplings!! Duniya goal hai? |
|
|
|
|
#7 (permalink) | |
|
Sami Hyypiä, LFC legend
Join Date: Jun 2007
Location: Us desh mein jaha Mera Neta Chor Hai
Posts: 1,940
|
Quote:
Raising from the dead is a gift indeed, use it well.
__________________
Read before asking / messaging any moderator for any query: FAQ + answers for new members Read all the sticky threads before asking any type of query. Most basic questions are answered in those. Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect. "Luck has a peculiar habit of favoring those who don't depend on it." |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|