Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Non repeating random Image from a folder

    The code below random shows image from a folder and it works just the way it is but then when i try to show more than 1 image, it repeats itself.

    PHP Code:
    <?php

    $folder 
    'images/';

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

    $img null;

    if (isset(
    $_GET['img'])) {
        
    $imageInfo pathinfo($_GET['img']);

        if (isset(
    $extList[strtolower($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);
        }
    }

    ?>
    The code to show the image:
    PHP Code:
    <img src="rotate.php"> and <img src="rotate.php"
    I'm trying to show about 2/3 images on the same place but all the images are the same. Is there anyway to prevent it from happening and show different images?

    thank you.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    How do you know which 3 images to show?
    Can you describe how your images are stored, and how you pick 3 of them ... even though you might have 50 of them?

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    the code works and it shows random image from a specified folder but when i try to show more than 1 image by using
    PHP Code:
    <img src="rotate.php"
    multiple times, it still shows only one image multiple times.

    i don't care what image it is as long as they are not repeated. thank you.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    So, it's based on the user ... ?

    Somewhere, you have to keep track of which image(s) were already displayed.
    Using SESSION or COOKIE ..

    Do they need to see a unique image each time they visit, or with each page refresh?
    And do they need to be displayed in order?
    And what happens when the user comes back a different day, does the list start over again?

    Or maybe the 3 images rotate automatically (using javascripting)?

    It's really hard to answer your original post ... not being sure what the specifications are.

  • #5
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Thank You for your interest.

    i really don't care as long as the images are differently displayed. Currently all the images are the same.

    for example:
    PHP Code:
    <img src="rotate.php">  <img src="rotate.php">  <img src="rotate.php"
    shows image1.jpg three times.

    I want
    PHP Code:
    <img src="rotate.php">  <img src="rotate.php">  <img src="rotate.php"
    to show image1.jpg image2.jpg image3.jpg. It can be in any order as long as the images are different. It can be on refresh or unique as long as its different. I hope you got my point.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Are they all JPG type images?

    And would it be OK to use scripting like this?

    <?php $count=3; include("rotate.php");?>

    So you would include the PHP script and tell it how many to print?

    Would that be OK, or does it have to be an <img src ... > tag?

  • #7
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    it has to be in <img src .."> tag.

    Thank You.

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    doh!

    I'm replying to this in two different threads?

    I wonder if you're having a caching issue (which is why they are the same).
    When you do a page refresh, do they change then?

  • #9
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    yeah. i thought if one of them could be sorted out, i'll use it since both the codes are for the same purpose.

    when i refresh, they change. but all the images remain the same which is image1.jpg image1.jpg

    i want it to be as image1.jpg image2.jpg image 3.jpg (they can be in any order as long as they are different)

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    So it's a cache issue then.
    Refreshing changes it, so you know it's working.

    Try this ...

    Duplicate the script 3 times ...
    Call them "rotate1.php", "rotate2.php", "rotate3.php"
    The same script, but different names ... so it might force it to not cache.

    Now put the three of them in your page and see what happens.

  • #11
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    no its not a cache issue and of course, i posted a working code! i just needed help modifying it.

    thanx for your time. never mind.

  • #12
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Is the reason kind of obvious? All three images are going to be the same, because of the GET. If the GET isn't set it sets it according to the else which in this case you might be doing but it still sets all three images the same in the img tag because the page is calling the script at the same exact time.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^


  •  

    Posting Permissions

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