OLD | NEW |
(Empty) | |
| 1 <?php |
| 2 # The script name is "random-cached-image" because this is expected to return |
| 3 # different images on every (re)load to test caching, which is similar to |
| 4 # other random-cached* scripts. |
| 5 # However, this actually returns an image from a small number of predefined |
| 6 # images in sequence, |
| 7 # because it is hard to generate random (PNG/JPEG/etc.) images from scratch. |
| 8 |
| 9 require_once '../../resources/portabilityLayer.php'; |
| 10 |
| 11 if (!sys_get_temp_dir()) { |
| 12 echo "FAIL: No temp dir was returned.\n"; |
| 13 exit(); |
| 14 } |
| 15 |
| 16 $id = $_GET['id']; |
| 17 if (filter_var($id, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^
[a-z0-9\-]+$/"))) === false) { |
| 18 echo "FAIL: invalid id.\n"; |
| 19 exit(); |
| 20 } |
| 21 |
| 22 $countFilename = sys_get_temp_dir() . "/random-cached-image." . $id . ".tmp"; |
| 23 $count = 0; |
| 24 if (file_exists($countFilename)) { |
| 25 $count = file_get_contents($countFilename); |
| 26 } |
| 27 $count += 1; |
| 28 file_put_contents($countFilename, $count); |
| 29 |
| 30 # Images with different dimensions. |
| 31 $imageFilenames = array( |
| 32 '../../resources/square20.png', |
| 33 '../../resources/square100.png', |
| 34 '../../resources/square200.png' |
| 35 ); |
| 36 |
| 37 header("Content-type: image/png"); |
| 38 header("Cache-control: max-age=60000"); |
| 39 header("ETag: 98765"); |
| 40 |
| 41 readfile($imageFilenames[$count % count($imageFilenames)]); |
| 42 ?> |
OLD | NEW |