Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Random image from sub folders
Old 01-22-2009, 10:51 AM Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
hi all
Despite my lack of ability to download images I am pressing on with my image gallery!
I would like to show a random thumbnail image on my home page. I have trawled the web and there are several easy to use scripts that perform this. However, they only allow me to use images from a single folder but I want to be able to select images from subfolders.
My main images folder contains several folders all of which contain thumbnail folders, it is from these thumbnail folders that I want to select the random pics!

Any help would be great
Burnsie is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-22-2009, 11:04 AM Re: Random image from sub folders
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
PHP Code:
<?php
$images 
= array();
$types = array('.jpg','.gif','.bmp','.jpeg','.png');    // Image types to fetch

function dirSearch($dirpath){
    global 
$images;
    
$dh scandir($dirpath);            // Read the contents into an array
    
shift($dh);shift($dh);                // Remove the '.' and '..'
    
foreach($dh as $item){
        if(
is_dir("$dirpath/$item")){        // Another dir so check that as well
            
dirSearch("$dirpath/$item");
        }else{                    
// Check 'filetype' by extension only
            
if(in_array(strtolower(strrchr($item,".")),$types)){
                
$images[] = "$dirpath/$item";
            }
        }
    }
}

dirSearch('./images');                    // Directory to start search from
?>
This should fetch all images from all subdirectories to './images' and every image from './images' as well.
I'm not sure this is exactly what you asked for though.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>

Last edited by Insensus; 01-22-2009 at 11:05 AM..
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 11:33 AM Re: Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
wow
i can't argue with your response time.
So I save the text as rotate.php or something then is this file placed in my main images folder?
All the subfolders contain multiple images and a folder which contains the thumbnails, which correspond to the larger images. It is from these 'thumbnail' folders that I want the random images to come from but NOT from the medium sized pics.
Will your magic script do this?
cheers
Burnsie is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 11:48 AM Re: Random image from sub folders
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
It doesn't... yet... :P

PHP Code:
<?php
$images 
= array();
$types = array('.jpg','.gif','.bmp','.jpeg','.png');    // Image types to fetch
$thumbdir 'thumbs';                 // The name of the thumbnail directories

function dirSearch($dirpath){
    global 
$images;
    
$dh scandir($dirpath);            // Read the contents into an array
    
shift($dh);shift($dh);                // Remove the '.' and '..'
    
foreach($dh as $item){
        if(
is_dir("$dirpath/$item")){        // Another dir so check that as well
            
dirSearch("$dirpath/$item");
        }elseif(
strrchr($dirpath,"/")=="/$thumbdir"){
        
// First check if we're in a thumbnail dir and if so check the filetype
            
if(in_array(strtolower(strrchr($item,".")),$types)){
                
$images[] = "$dirpath/$item";
            }
        }
    }
}

dirSearch('./images');                    // Directory to start search from
?>
If this works, you'll end up with an array filled with the locations of each thumbnail.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>

Last edited by Insensus; 01-22-2009 at 11:49 AM..
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 06:27 PM Re: Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
hi
Thank's again for the speedy response!
However, I haven't got it working (yet). Do I place the php file in the 'images' folder?
The name of the thumbnail folders is 'thumbs' and I have pointed to the correct path for the directory to start the search from.
Am I missing anything?
cheers
Burnsie is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 06:39 PM Re: Random image from sub folders
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
You should put the PHP script in the folder containing the 'images' folder, or change the dirSearch('./images'); to dirSearch('.');.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 08:38 PM Re: Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
I have tried and tried but still it doesn't work!
Nevertheless I am not defeated and will try some more.
Burnsie is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 08:43 PM Re: Random image from sub folders
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I will try the script as well.
Maybe I made a mistake somewhere I'm not aware of yet.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 08:58 PM Re: Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
cheers, you are a star.
I have folders in folders, which makes things a bit more complicated than they might be so I may not have stumbled upon the right paths yet! This is perhaps more likely to be the problem than your script!
Burnsie is offline
Reply With Quote
View Public Profile
 
Old 01-22-2009, 09:03 PM Re: Random image from sub folders
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
My bad.
I did make some mistakes.
PHP Code:
<?php
$images 
= array();
$types = array('.jpg','.gif','.bmp','.jpeg','.png');    // Image types to fetch
$thumbdir 'thumbs';                 // The name of the thumbnail directories

function dirSearch($dirpath){
    global 
$images$thumbdir$types;
    
$dh scandir($dirpath);            // Read the contents into an array
    
array_shift($dh);array_shift($dh);                // Remove the '.' and '..'
    
foreach($dh as $item){
        if(
is_dir("$dirpath/$item")){        // Another dir so check that as well
            
dirSearch("$dirpath/$item");
        }elseif(
strrchr($dirpath,"/")=="/$thumbdir"){
        
// First check if we're in a thumbnail dir and if so check the filetype
            
if(in_array(strtolower(strrchr($item,".")),$types)){
                
$images[] = "$dirpath/$item";
            }
        }
    }
}

dirSearch('./images');                    // Directory to start search from
?>
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-24-2009, 05:06 PM Re: Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
hi
still no luck I'm afraid. I am sure I have the structure right but still i can't see any images. However, if you refresh the page the little cross icon where the picture should be 'blinks' as though it is looking for a random image to display but non appears. Perhaps my path is still not right, I will keep trying
cheers
Burnsie is offline
Reply With Quote
View Public Profile
 
Old 01-24-2009, 05:43 PM Re: Random image from sub folders
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Then my guess is you either expect my script to do something else than it does, or you are now making the mistakes. :P

Because when I use it in an images dir with thumb dirs and such, it does return an array of images.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-26-2009, 11:44 AM Re: Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
Hi
still no joy. I have tried everything I can think of but to no avail so I guess I must be missing something along the line!
I have called your script 'rotate.php'.
I have a folder called 'pictures', in this folder resides my image folder, which is called 'animals'.
I have placed 'rotate.php' in the pictures folder.
I have replaced 'dirSearch('./images');' with dirSearch('./animals'); [and also tried 'dirSearch('.');'].
I have pointed my image source to read '<img src="pictures/rotate.php" />'.

am I missing something? I have tried placing rotate.php in just about every folder on my site in the hope that I may hit apon the right path if I have not understood things.
As I say if you refresh the page the image icon 'blinks' in a way that suggests that the script is trying to work but I don't see any images.
Burnsie is offline
Reply With Quote
View Public Profile
 
Old 01-26-2009, 02:10 PM Re: Random image from sub folders
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
PHP Code:
<?php
$images 
= array();
$types = array('.jpg','.gif','.bmp','.jpeg','.png');    // Image types to fetch
$thumbdir 'thumbs';                 // The name of the thumbnail directories

function dirSearch($dirpath){
    global 
$images$thumbdir$types;
    
$dh scandir($dirpath);            // Read the contents into an array
    
array_shift($dh);array_shift($dh);                // Remove the '.' and '..'
    
foreach($dh as $item){
        if(
is_dir("$dirpath/$item")){        // Another dir so check that as well
            
dirSearch("$dirpath/$item");
        }elseif(
strrchr($dirpath,"/")=="/$thumbdir"){
        
// First check if we're in a thumbnail dir and if so check the filetype
            
if(in_array(strtolower(strrchr($item,".")),$types)){
                
$images[] = "$dirpath/$item";
            }
        }
    }
}

dirSearch('./images');                    // Directory to start search from
$img $images[rand(0,count($images)-1)];      // Choose random image
$file file_get_contents($img);                     // Read image file into string

$type strtolower(strrchr($img,"."));             // Output header depending on type
switch($type){
    case 
'.jpg':
    case 
'.jpeg':
        
header('Content-type: image/jpeg');
        break;
    case 
'.gif':
        
header('Content-type: image/gif');
        break;
    case 
'.bmp':
        
header('Content-type: image/bmp');
        break;
    case 
'.png':
        
header('Content-type: image/png');
        break;
}
echo 
$file;    // Output actual image
?>
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>

Last edited by Insensus; 01-26-2009 at 02:15 PM..
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-26-2009, 05:55 PM Re: Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
Success!!!
You beautiful human being. May flights of angels carry you to your rest. It now does exactly what I wanted...displays a random image from multiple folders. Hoorah

One slight problem...my page displays the number of folders and pictures in my image files. Unfortunately it now sees 'rotate.php' as an image so it states '1 pictures(s)'. Is there a way to 'disguise' rotate.php so it doesn't show as a picture?

Other than that, Brilliant.
Burnsie is offline
Reply With Quote
View Public Profile
 
Old 01-26-2009, 06:06 PM Re: Random image from sub folders
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Kind words, kind words.

If you can find the script that determines how many pictures there are, maybe I could do something.
Or if it's some system with configuration options, maybe you can select the image types to count?
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-27-2009, 12:47 PM Re: Random image from sub folders
Burnsie's Avatar
Skilled Talker

Posts: 81
Trades: 0
Hi
I shall look into this!
Meanwhile!! Is it possible to adapt your script so the folders contained in my 'animals' folder each have a random cover picture taken from within their own folder? At the moment they have a directory cover picture '<% $directory[cover_url] %>' but it would (I think) be more effective with random pics!
Cheers
Burnsie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Random image from sub folders
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.63460 seconds with 12 queries