nfotxn: (Default)
[personal profile] nfotxn
Random image manipulation.

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.

<?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]>";} ?>

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?

Date: 2003-01-12 07:59 pm (UTC)
From: [identity profile] folkyboy.livejournal.com
such a small bio for so many lj friends

mark my words

Date: 2003-01-12 08:10 pm (UTC)
From: [identity profile] jongrizzman.livejournal.com
you just started a trend. now EVERYONE is going to do that somewhere in the bio of their journals. just give it a month or more.

Re: mark my words

Date: 2003-01-12 08:18 pm (UTC)
From: [identity profile] nfotxn.livejournal.com
Yeah, but I'm writing a script that sends a random image from the directory in place of the jpg. Hopefully I can make this work.. programming is hard.

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)
From: [identity profile] birddog.livejournal.com
You put the me in meme. Twice.
(deleted comment)

hee-hee

Date: 2003-01-12 08:51 pm (UTC)
From: [identity profile] plaidninja.livejournal.com
Brodie Chree, Wanted Ass Bandit.
Has Seven Warrents out for his arrest.
Do not approach if sighted. Subject is considered dangerous.

Date: 2003-01-12 10:27 pm (UTC)
From: [identity profile] deafeuphoria.livejournal.com
heh, id help you but pretty much all i know how to code is database stuff

Date: 2003-01-13 10:37 am (UTC)
From: [identity profile] smointjoker.livejournal.com
How very Pixies of you.

Frank is hot.

Sj.

Date: 2003-01-14 04:40 am (UTC)
From: [identity profile] notofthisworld.livejournal.com
Even if there was a "image/unknown" MIME type (I'm unsure if there is) it would be bad style to use it. I'd send a MIME type based on the extension.

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 "
[Error: Irreparable invalid markup ('<img [...] $dimensions[3]>') in entry. Owner must fix manually. Raw contents below.]

Even if there was a "image/unknown" MIME type (I'm unsure if there is) it would be bad style to use it. I'd send a MIME type based on the extension.

Like this:
<pre>
&lt;?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]>";}
?&gt;
</pre>

By the way, nice beard.

Profile

nfotxn: (Default)
nfotxn

April 2017

S M T W T F S
      1
23 45678
9101112131415
16171819202122
23242526272829
30      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Mar. 18th, 2026 06:31 am
Powered by Dreamwidth Studios