Results 1 to 5 of 5

Thread: Include subfolders in random image script

  1. #1
    Join Date
    Jul 2006
    Location
    Graham, NC
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Include subfolders in random image script

    How would I go about editing this script so that it would include subfolders in the image list? Currently, it just gets all of the images from the same folder the file is in. I can tweak it to get them from a different folder with no problem. But I am building a website (my first with PHP) for an auto repair business and they are going to be placing their images into different folders depending on what is being done to the automobile. All of these subfolders will be placed in a parent folder called repair_images. My goal is to randomly pick an image(s) from the repair_images folder and it's subfolders.

    Also, if there is a better way of doing this, please let me know and give me an example.

    Any help is greatly appreciated.
    PHP Code:
    <?php
        $folder 
    '.';

        
    $extList = array();
            
    $extList['gif'] = 'image/gif';
            
    $extList['jpg'] = 'image/jpeg';
            
    $extList['jpeg'] = 'image/jpeg';
            
    $extList['png'] = 'image/png';
        
        
    $img null;

        if (
    substr($folder,-1) != '/') {
            
    $folder $folder.'/';
        }

        if (isset(
    $_GET['img'])) {
            
    $imageInfo pathinfo($_GET['img']);
            if (
                isset( 
    $extListstrtolower$imageInfo['extension'] ) ] ) &&
                
    file_exists$folder.$imageInfo['basename'] )
            ) {
                
    $img $folder.$imageInfo['basename'];
            }
        } else {
            
    $fileList = array();
            
    $handle opendir($folder);
            while ( 
    false !== ( $file readdir($handle) ) ) {
                
    $file_info pathinfo($file);
                if (
                    isset( 
    $extListstrtolower$file_info['extension'] ) ] )
                ) {
                    
    $fileList[] = $file;
                }
            }
            
    closedir($handle);

            if (
    count($fileList) > 0) {
                
    $imageNumber time() % count($fileList);
                
    $img $folder.$fileList[$imageNumber];
            }
        }

        if (
    $img!=null) {
            
    $imageInfo pathinfo($img);
            
    $contentType 'Content-type: '.$extList$imageInfo['extension'] ];
            
    header ($contentType);
            
    readfile($img);
        } else {
            if ( 
    function_exists('imagecreate') ) {
                
    header ("Content-type: image/png");
                
    $im = @imagecreate (100100)
                    or die (
    "Cannot initialize new GD image stream");
                
    $background_color imagecolorallocate ($im255255255);
                
    $text_color imagecolorallocate ($im0,0,0);
                
    imagestring ($im255,  "IMAGE ERROR"$text_color);
                
    imagepng ($im);
                
    imagedestroy($im);
            }
        }

    ?>

  2. #2
    Join Date
    Jul 2006
    Location
    Graham, NC
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Bump.

    Any ideas?

  3. #3
    Join Date
    Jul 2006
    Location
    Graham, NC
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK. I have it now cycling through the parent directory and it's subfolders to grab the random image. Now, how can I tweak this so that is searches for a files with a paraticular filename? I want to search for only the files that begin with "t_", regardless of their extension, so I know that I need to use "t_*", but I'm not sure how/where to place that filter.

    PHP Code:
    <?php

        
    // files to search for
        
    $extList = array();
            
    $extList['gif'] = 'image/gif';
            
    $extList['jpg'] = 'image/jpeg';
            
    $extList['jpeg'] = 'image/jpeg';
            
    $extList['png'] = 'image/png';

        
    // set to specific image if needed
        // null will result in a random image
        
    $img null;
        
        
    // path to the directory you want to scan
        
    $directory './';
        if (
    substr($directory,-1) != '/') {
            
    $directory $directory.'/';
        }

        
    // if img is set, show it
        
    if (isset($_GET['img'])) {
            
    $imageInfo pathinfo($_GET['img']);
            if (
                isset( 
    $extListstrtolower$imageInfo['extension'] ) ] ) &&
                
    file_exists$directory.$imageInfo['basename'] )
            ) {
                
    $img $directory.$imageInfo['basename'];
            }
        } 
        
    // if img isnt set, grab a random
        
    else {
            
    //cycle through directory and subfolders
            
    $it = new RecursiveDirectoryIterator("$directory");
            foreach(new 
    RecursiveIteratorIterator($it) as $file)
            {
                
    $file_info pathinfo($file);
                if (
                    isset( 
    $extListstrtolower$file_info['extension'] ) ] )
                ) {
                    
    $items[] = $file;
                }
            }
            
    sort($items);
            
    // get the random image
            
    if (count($items) > 0) {
                
    $imageNumber time() % count($items);
                
    $img $directory.$items[$imageNumber];
            }
        }

        
    // if img isnt null, display it
        
    if ($img!=null) {
            
    $imageInfo pathinfo($img);
            
    $contentType 'Content-type: '.$extList$imageInfo['extension'] ];
            
    header ($contentType);
            
    readfile($img);
        } 
        
    // else, try to create one or give error
        
    else {
            if ( 
    function_exists('imagecreate') ) {
                
    header ("Content-type: image/png");
                
    $im = @imagecreate (100100)
                    or die (
    "Cannot initialize new GD image stream");
                
    $background_color imagecolorallocate ($im255255255);
                
    $text_color imagecolorallocate ($im0,0,0);
                
    imagestring ($im255,  "IMAGE ERROR"$text_color);
                
    imagepng ($im);
                
    imagedestroy($im);
            }
        }
    ?>

  4. #4
    Join Date
    Jul 2006
    Location
    Graham, NC
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK. Just realized a problem. Currently, the script above is stored in a file called rotator.php. In the file where I want the random image to appear, I just set the src property of the img tag equal to the path to the rotator.php file. This works fine if all I want to do is show a random image, but what I really want it to do is show the random thumbnail image and have it set as a link to the fullsize image. So using it as the source of the img tag is not gonna work.

    I tried tweaking the header content-type to text/html and using require_once in the file where I want it to appear, but it is giving me an error message of "Cannot modify header information.....".

    So, what would I need to do to make to code in the post above be able to be used as a require_once file and output something like:
    echo '<a href="' . $img . '"><img src="' . $imgThumb . 'alt="" /></a>';

    Thanks in advance!

  5. #5
    Join Date
    Jul 2006
    Location
    Graham, NC
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Alright. I got the issue handled so that it allws me to use a require statement and have the image display in the page. This give me the ability to pull an image and have it link to another image.

    So, the only thing I have left to figure out is how to make it just pull the thumbnail images which all have "t_" as the first characters of the filename. I tried using a strpos statement, but that searches the whole basename. I just want to search the 1st 2 characters.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •