Displaying Random Image in Your Page

Nov 4, 2008 | Tags: PHP, Image | del.icio.us del.icio.us | digg Digg

Displaying a random image in your page is cool & fun. It's also useful if you want to display random banners or such. Here it is:

Listing 1: random_img.php

  1. <?php
  2. /**
  3.  * random_img.php :
  4.  * This script selects a random image from a specified
  5.  * directory and outputs the image to browser.
  6.  */
  7.  
  8. /* output selected image to browser */
  9. function out2browser($filename)
  10. {
  11.     list($width, $height, $type) = getimagesize($filename);
  12.    
  13.     switch($type) {
  14.         case IMAGETYPE_PNG:
  15.             $img = imagecreatefrompng($filename);
  16.             header("Content-type: image/png");
  17.             imagepng($img);
  18.             break;
  19.         case IMAGETYPE_GIF:
  20.             $img = imagecreatefromgif($filename);
  21.             header("Content-type: image/gif");
  22.             imagegif($img);
  23.             break;
  24.         case IMAGETYPE_JPEG:
  25.             $img = imagecreatefromjpeg($filename);
  26.             header("Content-type: image/jpeg");
  27.             imagejpeg($img);
  28.             break;
  29.     }
  30. }
  31.  
  32. /* change to your images directory */
  33. $imgpath = "C:/www/img/avatar";
  34. $images  = array();
  35.  
  36. /* list images */
  37. $files = scandir($imgpath);
  38.  
  39. foreach($files as $file) {
  40.     /* select only gif, jpg and png images */
  41.     $ext = substr($file, -4);
  42.     if ($ext == '.gif' || $ext == '.jpg' || $ext == '.png') {
  43.         $images[] = $file;
  44.     }
  45. }
  46.  
  47. /* select random image */
  48. $val = rand(0, count($images) - 1);
  49. $img = $images[$val];
  50.  
  51. out2browser("$imgpath/$img");
  52. ?>

The script above will display a random image from the specified directory. Use the script above in your <img> tag like this:

<img src="random_img.php" border="0" alt="my random image" />

Related Article

2 Comments

Hillbilly Geek on Dec 27, 2008:

On Line 41, if you do this:
$ext = strtolower(substr($file, -4));
then it will handle both upper and lowercase filenames
Otherwise, this is one of the few scripts that gave me no problems, and seems to work well

Nash on Dec 28, 2008:

Thanks for your suggestion!

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000

Tags

Newsletter

Send me your new post on:
OpenCV
PHP
Projects
Networking
Regex
to my email:
No, I won't spam your email.

Recent Posts

  1. PHP Script for Converting Relative to Absolute URL
  2. Streaming OpenCV Videos Over the Network
  3. Password Protected Images with PHP
  4. OpenCV Region of Interest (ROI)
  5. Web Based Surveillance System with OpenCV, PHP and Javascript

Popular Posts

  1. OpenCV Eye Detection
  2. OpenCV Examples Part 1
  3. OpenCV Face Detection
  4. OpenCV Eye Tracking
  5. Display Video from Webcam
  6. Phase Correlation in OpenCV
  7. Simple File Server and Downloader Script
  8. Building Dynamic SQL String from Associative Array
  9. Template Matching with OpenCV
  10. Fetching a Web Page From Your PHP Code

About the Author

avatar Cool PHP programmer writing cool PHP scripts. Feel free to contact
Tel. +62 31 8662872
+62 856 338 6017
ICQ 489571630
Skype dede_bl4ckheart
Yahoo dede_bl4ckheart
Google nashruddin.amin