Skip to content

Instantly share code, notes, and snippets.

@FiXato
Created March 8, 2010 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FiXato/325268 to your computer and use it in GitHub Desktop.
Save FiXato/325268 to your computer and use it in GitHub Desktop.
Random image script I wrote years ago. GISTed because someone asked for it.
<?php
/* Random Image Generator
* Base code & Inspiration by : Maciek "Tularis" Sokolewicz
* ReWritten and enhanced by : Filip H.F. "FiXato" Slagter
* Contact : fixato@gmail.com
* Uses privately adapted version of Directory Listing Script found at : http://php.net/readdir (created by php@silisoftware.com on 31-Jan-2003 04:16)
*/
if(!function_exists('image_type_to_mime_type')) { //check if function exists...
//Alternate version of Image_Type_To_Mime_Type()-function, if that function is absent
//By Filip H.F. "FiXato" Slagter
function image_type_to_mime_type($imageType) {
$return = FALSE;
switch($imageType) {
case 1:
$return = 'image/gif';
break;
case 2:
$return = 'image/jpeg';
break;
case 3:
$return = 'image/png';
break;
case 4:
$return = 'application/x-shockwave-flash';
break;
case 5:
$return = 'image/psd';
break;
case 6:
$return = 'image/bmp';
break;
case 7:
$return = 'image/tiff';
break;
case 8:
$return = 'image/TIFF';
break;
case 9:
$return = 'image/jpeg';
break;
case 10:
$return = 'application/octet-stream';
break;
case 11:
$return = 'application/octet-stream';
break;
case 12:
$return = 'application/octet-stream';
break;
case 13:
$return = 'application/x-shockwave-flash';
break;
case 14:
$return = 'image/iff';
break;
case 15:
$return = 'application/octet-stream';
break;
}
return $return;
}
}
function setImageSize($filename, $width='', $height='') {
//Function to resize Images, while maintaining a reasonable aspect-ratio
//Created by Filip H.F. "FiXato" Slagter
if(file_exists($filename)) {
$imageInfo = getImageSize($filename);
$imageSizes['width'] = $imageInfo[0];
$imageSizes['height'] = $imageInfo[1];
if($width !='' && $height !='') {
$imageSizes = resize($width, $height, $imageSizes);
}
if(isset($imageInfo['mime'])) {
$mime = $imageInfo['mime'];
}
else {
$mime = FALSE;
}
if(isset($imageInfo['channels'])) {
$channels = $imageInfo['channels'];
}
else {
$channels = 'undefined';
}
if(isset($imageInfo['bits'])) {
$bits = $imageInfo['bits'];
}
else {
$bits = 'undefined';
}
$fileSize = filesize($filename);
$exifOutput = '';
/* Commented out because of invalid exif-function
if(function_exists('exif_read_data')) {
$exif = @exif_read_data ('$filename',0,true);
foreach($exif as $key=>$section) {
foreach($section as $name=>$val) {
$exifOutput .= "$key.$name: $val<br />\n";
}
}
}
*/
$ext = $imageInfo[2];
if($ext != '' && function_exists('image_type_to_mime_type')) {
$altContentType = image_type_to_mime_type($ext);
}
else {
$altContentType = 'application/octet-stream';
}
$returnArray = array('filename' => $filename,
'origWidth' => $imageInfo[0],
'origHeight' => $imageInfo[1],
'height' => ceil($imageSizes['height']),
'width' => ceil($imageSizes['width']),
'mime' => $mime,
'ext' => $ext,
'altContentType' => $altContentType,
'altMime' => $altContentType,
'channels' => $channels,
'bits' => $bits,
'exif' => $exifOutput,
'fileSize' => $fileSize);
return $returnArray;
}
else {
return FALSE;
}
}
function random($dirArray = array('.') ) {
//init
for($i = 0; $i < count($dirArray); $i++) {
$dirsToScan[] = realpath($dirArray[$i]);
}
$array = array();
$num = 0;
$temp = array();
mt_srand((double)microtime()*1000000); //init random generator
$file = '';
$dirKey = '';
$startingDir = '';
$allowedExts = array('jpg', 'jpe', 'jpeg', 'gif', 'png', 'bmp');
$dirsScanned = array();
while (count($dirsToScan) > 0) {
foreach ($dirsToScan as $dirKey => $startingDir) {
if ($dir = opendir($startingDir)) {
while (($file = readdir($dir)) !== false) {
$ext = strtolower(substr(strrchr($file,"."),1));
if (($file != '.') && ($file != '..') && in_array($ext,$allowedExts) && $file != '') { //ignore current Dir, upper Dir, ignore empty filesnames and check for FileExtenion
$realPathName = realpath($startingDir.'/'.$file);
if (is_dir($realPathName)) {
if (!in_array($realPathName, $dirsScanned) && !in_array($realPathName, $dirsToScan)) {
$dirsToScan[] = $realPathName;
}
} elseif (is_file($realPathName)) {
$filesInDir[] = $realPathName;
$filesInDir2[] = $file;
}
}
}
closedir($dir);
}
$dirsScanned[] = $startingDir;
unset($dirsToScan[$dirKey]);
}
}
$count = count($filesInDir);
$num = mt_rand(0, ($count-1));
if($count > 0){
//$size = getimagesize($filesInDir[$num]);
$imgInfo = setImageSize($filesInDir[$num]);
$stream = fopen($filesInDir[$num], "rb");
if($imgInfo && $stream){
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
header('Content-Disposition: inline; filename="'.$filesInDir2[$num].'"');
header('Content-Description: Random '.$imgInfo['channels'].' Channels, '.$imgInfo['bits'].'bits '.$imgInfo['ext'].'-Image File');
header('Content-Length: '.$imgInfo['fileSize']);
header('Content-Type: '.$imgInfo['altMime']);
fpassthru($stream);
header ('Connection: close');
exit();
}
}else{
echo 'no images';
}
}
random(array('.'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment