Tiny updates
Jan. 12th, 2003 10:46 pmRandom image manipulation.
<?php
//read folder
$folder=opendir(".");
while ($file = readdir($folder))
$names[count($names)] = $file;
closedir($folder);
//sort file names in array
sort($names);
//remove any non-images from array
$tempvar=0;
for ($i=0;$names[$i];$i++){
$ext=strtolower(substr($names[$i],-4));
if
($ext==".jpg"||$ext==".gif"||$ext=="jpeg"||$ext==".png"){$names1[$tempvar]=$names[$i];$tempvar++;}
}
//random
srand ((double) microtime() * 10000000);
$rand_keys = array_rand ($names1, 2);
//random image from array
$img=$names1[$rand_keys[0]];
//image dimensions
$dimensions = GetImageSize($img);
//Write headers
header("Content-type: image/jpeg");
readfile($img);
//Comment out the header writing and uncomment
//the two below lines to have the script write an HTML tag instead
//if (isset($pic)){header ("Location: $img");}
//else {echo "<img src=\"$img\" $dimensions[3]>";}
?>

Based on Random Image Display Script... only cooler. It simple writes the contents of a random image to the HTTP response. I was writing it my self and decided to check Freshmeat... low and behold it had been done before! Only need to change two lines.
One question I have is this: should I just pre-write the header based on the file extension or can I use some fancy HTTP header that tells the browser that it may be a PNG, GIF or JPEG?
no subject
Date: 2003-01-12 07:59 pm (UTC)mark my words
Date: 2003-01-12 08:10 pm (UTC)Re: mark my words
Date: 2003-01-12 08:18 pm (UTC)It'd be nice to start 2003 off by starting a Meme. It makes me feel all important.
Re: mark my words
Date: 2003-01-13 08:12 am (UTC)hee-hee
Date: 2003-01-12 08:51 pm (UTC)Has Seven Warrents out for his arrest.
Do not approach if sighted. Subject is considered dangerous.
no subject
Date: 2003-01-12 10:27 pm (UTC)no subject
Date: 2003-01-13 10:37 am (UTC)Frank is hot.
Sj.
no subject
Date: 2003-01-14 04:40 am (UTC)Like this:
<?php //read folder $folder=opendir("."); while ($file = readdir($folder)) $names[count($names)] = $file; closedir($folder); //sort file names in array sort($names); //remove any non-images from array $tempvar=0; for ($i=0;$names[$i];$i++){ $ext=strtolower(substr($names[$i],-4)); if ($ext==".jpg"||$ext==".gif"||$ext=="jpeg"||$ext==".png"){$names1[$tempvar]=$names[$i];$tempvar++;} } //random srand ((double) microtime() * 10000000); $rand_keys = array_rand ($names1, 2); //random image from array $img=$names1[$rand_keys[0]]; //image dimensions $dimensions = GetImageSize($img); //Write headers $ext=strtolower(substr($img,-4)); if($ext == ".jpg" || $ext == "jpeg") {$mimetype = "image/jpeg";} if($ext == ".png") {$mimetype = "image/png";} if($ext == ".gif) {$mimetype = "image/gif";} header("Content-type: " . $mimetype); readfile($img); //Comment out the header writing and uncomment //the two below lines to have the script write an HTML tag instead //if (isset($pic)){header ("Location: $img");} //else {echo "Like this:
<pre>
<?php
//read folder
$folder=opendir(".");
while ($file = readdir($folder))
$names[count($names)] = $file;
closedir($folder);
//sort file names in array
sort($names);
//remove any non-images from array
$tempvar=0;
for ($i=0;$names[$i];$i++){
$ext=strtolower(substr($names[$i],-4));
if
($ext==".jpg"||$ext==".gif"||$ext=="jpeg"<wbr>||$ext==".png"){$names1[$tempvar]=$names[<wbr>$i];$tempvar++;}
}
//random
srand ((double) microtime() * 10000000);
$rand_keys = array_rand ($names1, 2);
//random image from array
$img=$names1[$rand_keys[0]];
//image dimensions
$dimensions = GetImageSize($img);
//Write headers
$ext=strtolower(substr($img,-4));
if($ext == ".jpg" || $ext == "jpeg") {$mimetype = "image/jpeg";}
if($ext == ".png") {$mimetype = "image/png";}
if($ext == ".gif) {$mimetype = "image/gif";}
header("Content-type: " . $mimetype);
readfile($img);
//Comment out the header writing and uncomment
//the two below lines to have the script write an HTML tag instead
//if (isset($pic)){header ("Location: $img");}
//else {echo "<img src=\"$img\" $dimensions[3]>";}
?>
</pre>
By the way, nice beard.