Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Image Generator in PHP
Author Message
ZiNgA BuRgA
Smart Alternative

Posts: 17,024.1882
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.1887
Offline
Post: #1
Random Image Generator in PHP
Decided to put this up for anyone interested.

NOTE, requires:
  • A hosting service with PHP support
  • Images must be uploaded to the same folder as the PHP script

Copy and paste this to make a new .php file:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

// Force no caching
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: image");








//****************** CHANGE THIS PART **************************
//Add lines to add images

$pic = array(
 "1.jpg",
 "2.jpg",
 "3.jpg"
);


//**************************************************************













$rpic = $pic[mt_rand(0, count($pic)-1)];

header("Content-Length: ".filesize($rpic));
header("Content-Type: image");

$fp = fopen($rpic, 'rb');
fpassthru($fp);
fclose($fp);

?>

Upload this file to your server, and direct people to use this .php file as the image.



I chose to use this method as it works better than a header redirect (which typically gets cached).  Also hides the actual files from the user :P

Further reference:
http://endlessparadigm.com/forum/showthread.php?tid=930

(This post was last modified: 01/04/2008 06:09 AM by ZiNgA BuRgA.)
26/03/2007 03:48 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply 


Messages In This Thread
Random Image Generator in PHP - ZiNgA BuRgA - 26/03/2007 03:48 AM
RE: Random Image Generator in PHP - S7* - 03/04/2007, 04:52 AM
RE: Random Image Generator in PHP - Ge64 - 03/04/2007, 06:16 AM
RE: Random Image Generator in PHP - RIKKU - 11/04/2009, 09:27 PM
RE: Random Image Generator in PHP - S7* - 12/04/2009, 08:53 AM
Thread Revived!!! - Necro-Bot - 11/04/2009, 11:36 AM

Forum Jump:


User(s) browsing this thread: 1 Guest(s)

 Quick Theme: