<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Display random image from directory</title>
	<atom:link href="http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/</link>
	<description></description>
	<pubDate>Thu, 20 Nov 2008 23:41:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Peter</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-18714</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Wed, 23 Mar 2005 00:28:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-18714</guid>
		<description>Love the script and im sure i can use it in many applications,  I was wondering however if it was possibel to filter the display to show only images beginning with the word "thumb_"   much like you have done with the &lt;h3&gt;# if (($file !=".") &#038;&#038; ($file !="..") &#038;&#038; (substr($file, -10) == ".thumb.jpg"))&lt;/h3&gt;

I use a coppermine gallery inside a ezcontents portal and displaying images on different pages with Coppermine is a bit of a task. (coppermine prefixes to identify thumbs)  Your script would ideally suit me if it could filter accordingly.


regards</description>
		<content:encoded><![CDATA[<p>Love the script and im sure i can use it in many applications,  I was wondering however if it was possibel to filter the display to show only images beginning with the word &#034;thumb_&#034;   much like you have done with the<br />
<h3># if (($file !=&#034;.&#034;) &#038;&#038; ($file !=&#034;..&#034;) &#038;&#038; (substr($file, -10) == &#034;.thumb.jpg&#034;))</h3>
<p>I use a coppermine gallery inside a ezcontents portal and displaying images on different pages with Coppermine is a bit of a task. (coppermine prefixes to identify thumbs)  Your script would ideally suit me if it could filter accordingly.</p>
<p>regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leon</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-13748</link>
		<dc:creator>Leon</dc:creator>
		<pubDate>Wed, 29 Dec 2004 04:51:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-13748</guid>
		<description>Any improvements about the sub-directories issue since you wrote this script? I own a gallery and all my images reside in folders (categories) and subfolders (subcategories). I badly need a script that picks a random image after checking all the folders and picks a random image. I know the logic but can't apply it.

- Create a (super)array
- Let the script browse the directories and add the image names to this array.
- Pick randomly from the array.

How?</description>
		<content:encoded><![CDATA[<p>Any improvements about the sub-directories issue since you wrote this script? I own a gallery and all my images reside in folders (categories) and subfolders (subcategories). I badly need a script that picks a random image after checking all the folders and picks a random image. I know the logic but can&#039;t apply it.</p>
<p>- Create a (super)array<br />
- Let the script browse the directories and add the image names to this array.<br />
- Pick randomly from the array.</p>
<p>How?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jim</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6496</link>
		<dc:creator>jim</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6496</guid>
		<description>Your random image code seems to pull the same images a lot of the time.</description>
		<content:encoded><![CDATA[<p>Your random image code seems to pull the same images a lot of the time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lifefeed</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6497</link>
		<dc:creator>lifefeed</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6497</guid>
		<description>The following code has come in handy for me, and is a bit more effecient than the original code.  Adjust it with any of the extra bells and whistles as needed.

&lt;h3&gt;
# Relative to current directory
$imgdir = 'random/image/directory/';
$dir = $_SERVER['DOCUMENT_ROOT'] . '/' . $imgdir;
$images = array();
#
# The user doesn't need to see any errors
$dir_handle = @opendir($dir);
#
# The correct way to iterate over a directory
# (see http://www.php.net/manual/en/function.readdir.php)
while (false !== ($file = readdir($dir_handle)))
&#160;&#160;if (!is_dir($file))
&#160;&#160;&#160;&#160;$images[] = "$file";
#
closedir ($dir_handle);
#
# The following appears to make things "more" random
srand((double)microtime()*1000000);
$r = rand(0,sizeof($images)-1);
#
echo "&#60;img src=&#92;"{$imgdir}{$images[$r]}&#92;"&#62;";
&lt;/h3&gt;
</description>
		<content:encoded><![CDATA[<p>The following code has come in handy for me, and is a bit more effecient than the original code.  Adjust it with any of the extra bells and whistles as needed.</p>
<h3>
# Relative to current directory<br />
$imgdir = &#039;random/image/directory/&#039;;<br />
$dir = $_SERVER['DOCUMENT_ROOT'] . &#039;/&#039; . $imgdir;<br />
$images = array();<br />
#<br />
# The user doesn&#039;t need to see any errors<br />
$dir_handle = @opendir($dir);<br />
#<br />
# The correct way to iterate over a directory<br />
# (see <a href="http://www.php.net/manual/en/function.readdir.php" rel="nofollow">http://www.php.net/manual/en/function.readdir.php</a>)<br />
while (false !== ($file = readdir($dir_handle)))<br />
&nbsp;&nbsp;if (!is_dir($file))<br />
&nbsp;&nbsp;&nbsp;&nbsp;$images[] = &#034;$file&#034;;<br />
#<br />
closedir ($dir_handle);<br />
#<br />
# The following appears to make things &#034;more&#034; random<br />
srand((double)microtime()*1000000);<br />
$r = rand(0,sizeof($images)-1);<br />
#<br />
echo &#034;&lt;img src=&#92;&#034;{$imgdir}{$images[$r]}&#92;&#034;&gt;&#034;;<br />
</h3>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jennifer</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6495</link>
		<dc:creator>Jennifer</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6495</guid>
		<description>Or actually more specifically - &lt;b&gt;as she said above in the post&lt;/b&gt;:

&lt;i&gt;There is at least one caveat that I know about - images must exist in one directory with no sub directories. Theoretically, this shouldn't be a problem, but I've had problems with the script when trying to run it in a directory with subdirectories.&lt;/i&gt;</description>
		<content:encoded><![CDATA[<p>Or actually more specifically - <b>as she said above in the post</b>:</p>
<p><i>There is at least one caveat that I know about - images must exist in one directory with no sub directories. Theoretically, this shouldn&#039;t be a problem, but I&#039;ve had problems with the script when trying to run it in a directory with subdirectories.</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arvind</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6493</link>
		<dc:creator>Arvind</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6493</guid>
		<description>Does this not work if u have sub directories in your images fold e.g. 
images &gt; example
          &gt; example 2

and each sub fold has images in them . This is annoying me as i have different sub folders in my main images folder and this script doesn't work !</description>
		<content:encoded><![CDATA[<p>Does this not work if u have sub directories in your images fold e.g.<br />
images > example<br />
          > example 2</p>
<p>and each sub fold has images in them . This is annoying me as i have different sub folders in my main images folder and this script doesn&#039;t work !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jennifer</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6494</link>
		<dc:creator>Jennifer</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6494</guid>
		<description>Sorry - no, it doesn't support sub directories. I'm sure there IS a way to do that in PHP, but this particular script does not do that...</description>
		<content:encoded><![CDATA[<p>Sorry - no, it doesn&#039;t support sub directories. I&#039;m sure there IS a way to do that in PHP, but this particular script does not do that&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jennifer</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6490</link>
		<dc:creator>Jennifer</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6490</guid>
		<description>Unfortunately server configurations is not my specialty... :-&#92; Good luck!!</description>
		<content:encoded><![CDATA[<p>Unfortunately server configurations is not my specialty&#8230; :-&#92; Good luck!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SiMON</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6491</link>
		<dc:creator>SiMON</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6491</guid>
		<description>Thanks you so much! Ive been looking for something like this for a long time. And now i come across yours which is nice and simple even i made it work!</description>
		<content:encoded><![CDATA[<p>Thanks you so much! Ive been looking for something like this for a long time. And now i come across yours which is nice and simple even i made it work!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob</title>
		<link>http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6492</link>
		<dc:creator>Rob</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/04/22/display-random-image-from-directory/#comment-6492</guid>
		<description>I have adjusted the script using some suggestions from here. I am using it within a block module on the current version of phpwebsite with great success.. Thanks for your efforts.. 

Here are the details:

Save the script as randimg.php..  I have discovered iframes which seem to do the trick.. Add the iframe tag to the textsettings.php file in conf dir of phpwebsite. Add the following to the block content:
&#60;iframe marginwidth="0" marginheight="0" width="180" scrolling="no" frameborder=0 src="randimg.php"&#62;&#60;/iframe&#62;

Rob

&lt;h3&gt;
&#60;?php


# Put the SERVER PATH to image directory. 
# INCLUDE trailing slash
$dir = "/home/user/html/images/photoalbum/1/";


# Put the HTML PATH to image directory.
# DO NOT include trailing slash 
$html_dir = "http://www.someplace.com/images/photoalbum/1";


$dir_handle = opendir($dir);
$image_array = array();


# This opens up the image directory, grabs all files with a ".jpg" extension
# and puts them into an array. You can set whatever extension you'd like here.
# Just switch out the .jpg for .gif, etc. If you have more than one extension, 
# replace this line with something like this:
# if (($file !=".") &#038;&#038; ($file !="..") &#038;&#038; (substr($file, -4) == ".jpg") &#124;&#124; (substr($file, -4) == ".gif"))) {
# This will pull up all .jpg OR .gifs. If you name all the images you want to pull 
# a certain way (example, image-thumb.jpg or image.thumb.jpg) you can do this:
# if (($file !=".") &#038;&#038; ($file !="..") &#038;&#038; (substr($file, -10) == ".thumb.jpg")) {
# basically you want to put the extension in quotes and then count how many characters
# it is and place that after $file, -


while ($file = readdir($dir_handle)) {
if (($file !=".") &#038;&#038; ($file !="..") &#038;&#038; (substr($file, -6) == "tn.jpg")) {
$file_name = "$html_dir/$file";
$image_array[$file_name] = $file_name;
}
}


# sorts the array. For some reason this won't work at all
# unless this is done. Can anyone tell me why?
sort($image_array);


$count = sizeof($image_array);


# trims all the URLs of whitespace and assigns each
# item in the array a number to identify it.
for($i = 0;$i&#60;$count; $i++) {
$image[$i] = rtrim($image_array[$i]);
}


# assign a random value
$r = rand(0,$count-1);


# get the size of a random image
$size = getimagesize($image[$r]);
$width = $size[0];
$height = $size[1];


# This is the HTML output. You can change any of this, but you should
# leave src=&#92;"$image[$r]&#92;" width=&#92;"$width&#92;" height=&#92;"$height&#92;" where it is.
# Remember to escape any quotation marks, etc.
$img_src = "&#60;center&#62;&#60;img src=&#92;"$image[$r]&#92;" width=&#92;"$width&#92;" height=&#92;"$height&#92;" alt=&#92;"This is a Random Image&#92;" /&#62;&#60;/center&#62;";

echo $img_src;

# closes the directory (why keep it open?)
closedir ($dir_handle);


?&#62;
&lt;/h3&gt;</description>
		<content:encoded><![CDATA[<p>I have adjusted the script using some suggestions from here. I am using it within a block module on the current version of phpwebsite with great success.. Thanks for your efforts.. </p>
<p>Here are the details:</p>
<p>Save the script as randimg.php..  I have discovered iframes which seem to do the trick.. Add the iframe tag to the textsettings.php file in conf dir of phpwebsite. Add the following to the block content:<br />
&lt;iframe marginwidth=&#034;0&#034; marginheight=&#034;0&#034; width=&#034;180&#034; scrolling=&#034;no&#034; frameborder=0 src=&#034;randimg.php&#034;&gt;&lt;/iframe&gt;</p>
<p>Rob</p>
<h3>
&lt;?php</p>
<p># Put the SERVER PATH to image directory.<br />
# INCLUDE trailing slash<br />
$dir = &#034;/home/user/html/images/photoalbum/1/&#034;;</p>
<p># Put the HTML PATH to image directory.<br />
# DO NOT include trailing slash<br />
$html_dir = &#034;http://www.someplace.com/images/photoalbum/1&#034;;</p>
<p>$dir_handle = opendir($dir);<br />
$image_array = array();</p>
<p># This opens up the image directory, grabs all files with a &#034;.jpg&#034; extension<br />
# and puts them into an array. You can set whatever extension you&#039;d like here.<br />
# Just switch out the .jpg for .gif, etc. If you have more than one extension,<br />
# replace this line with something like this:<br />
# if (($file !=&#034;.&#034;) &#038;&#038; ($file !=&#034;..&#034;) &#038;&#038; (substr($file, -4) == &#034;.jpg&#034;) || (substr($file, -4) == &#034;.gif&#034;))) {<br />
# This will pull up all .jpg OR .gifs. If you name all the images you want to pull<br />
# a certain way (example, image-thumb.jpg or image.thumb.jpg) you can do this:<br />
# if (($file !=&#034;.&#034;) &#038;&#038; ($file !=&#034;..&#034;) &#038;&#038; (substr($file, -10) == &#034;.thumb.jpg&#034;)) {<br />
# basically you want to put the extension in quotes and then count how many characters<br />
# it is and place that after $file, -</p>
<p>while ($file = readdir($dir_handle)) {<br />
if (($file !=&#034;.&#034;) &#038;&#038; ($file !=&#034;..&#034;) &#038;&#038; (substr($file, -6) == &#034;tn.jpg&#034;)) {<br />
$file_name = &#034;$html_dir/$file&#034;;<br />
$image_array[$file_name] = $file_name;<br />
}<br />
}</p>
<p># sorts the array. For some reason this won&#039;t work at all<br />
# unless this is done. Can anyone tell me why?<br />
sort($image_array);</p>
<p>$count = sizeof($image_array);</p>
<p># trims all the URLs of whitespace and assigns each<br />
# item in the array a number to identify it.<br />
for($i = 0;$i&lt;$count; $i++) {<br />
$image[$i] = rtrim($image_array[$i]);<br />
}</p>
<p># assign a random value<br />
$r = rand(0,$count-1);</p>
<p># get the size of a random image<br />
$size = getimagesize($image[$r]);<br />
$width = $size[0];<br />
$height = $size[1];</p>
<p># This is the HTML output. You can change any of this, but you should<br />
# leave src=&#92;&#034;$image[$r]&#92;&#034; width=&#92;&#034;$width&#92;&#034; height=&#92;&#034;$height&#92;&#034; where it is.<br />
# Remember to escape any quotation marks, etc.<br />
$img_src = &#034;&lt;center&gt;&lt;img src=&#92;&#034;$image[$r]&#92;&#034; width=&#92;&#034;$width&#92;&#034; height=&#92;&#034;$height&#92;&#034; alt=&#92;&#034;This is a Random Image&#92;&#034; /&gt;&lt;/center&gt;&#034;;</p>
<p>echo $img_src;</p>
<p># closes the directory (why keep it open?)<br />
closedir ($dir_handle);</p>
<p>?&gt;<br />
</h3>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.341 seconds -->
