Random image in php
Categories: php
Tags:
If you want to show a random image in your site a good method is through a php script.
A random image example is here ![]()
You prefer the dogs, the cat or the lucky hamster?
(refresh to randomize another image)
Code Analysis
In the source code the function
function LoadJpeg($imgname)
takes in input the image name and returns the content of the image.
In the main program through the lone
header("Content-Type: image/jpeg");
I set the header of the page as JPEG. After that I generate a random number and call the function LoadJpeg with a filename like randomNumer.jpg (eg. 2.jpg if we generate the random number 2).
// number from 1 to 3 $random = (rand()%3) + 1; // random image name $imgname = "$random.jpg"; // load the image $img = LoadJpeg($imgname);
Now i call the function imagejpeg that send the image to the browser, passing to it the content of the random image
// sending the image to the browser imagejpeg($img);
Download Php Random Image. Downloads: 258
Se sei interessato a questo post, potresti anche provare a leggere:
- No related posts
20 Mar 2007 dzamir