11 Replies - 9951 Views - Last Post: 28 May 2011 - 10:21 PM Rate Topic: -----

#1 GorillaCheif   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-April 06

random image from directory

Posted 26 May 2011 - 09:28 PM

I'm more literate in java, but this shouldn't be too hard.

This is supposed to randomly display an image from a selected directory each time the page loads.
But all I get is this
So I have this:

<?php

$path = '/slide';
$file = rand(1, 20);
$file .= '.jpg';
if (file_exists($path . '/' . $file)) {
    echo '<img src="'.$file.'" />';

} 

?>



Then have it called into a DIV tag using AJAX with:
<img src="/php/home.php" alt="rotate images" />



I'm getting the alt text, but no image.
I'm not proficient enough with PHP to see what Im doing wrong here.
Can someone give me some insight?

Is This A Good Question/Topic? 0
  • +

Replies To: random image from directory

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: random image from directory

Posted 26 May 2011 - 10:12 PM

it could be that your condition fails. you should also set the output MIME to text/plain.
Was This Post Helpful? 1
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: random image from directory

Posted 26 May 2011 - 11:50 PM

Look closer at your <img> tag's src attribute:
<?php

// Here you define the root directory
$path = '/slide';

// And get a random file name
$file = rand(1, 20);
$file .= '.jpg';

// Which you then check to see if exists
if (file_exists($path . '/' . $file)) {
    // And if it does, you use it in your <img> tag.
    echo '<img src="'.$file.'" />';

    // Except, notice that you only use $file, but ignore
    // the $path. Perhaps you should try this instead:
    echo '<img src="' . $path . '/' . $file . '" />';
} 

?>



P.S.
You may want to consider using mt_rand() instead of rand(). It's said to be 4x faster, and to generate "better" random numbers. (There is random... and then there is random :P)

This post has been edited by Atli: 26 May 2011 - 11:54 PM

Was This Post Helpful? 4
  • +
  • -

#4 GorillaCheif   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-April 06

Re: random image from directory

Posted 28 May 2011 - 01:51 AM

View PostAtli, on 26 May 2011 - 11:50 PM, said:

Look closer at your <img> tag's src attribute:
<?php

// Here you define the root directory
$path = '/slide';

// And get a random file name
$file = rand(1, 20);
$file .= '.jpg';

// Which you then check to see if exists
if (file_exists($path . '/' . $file)) {
    // And if it does, you use it in your <img> tag.
    echo '<img src="'.$file.'" />';

    // Except, notice that you only use $file, but ignore
    // the $path. Perhaps you should try this instead:
    echo '<img src="' . $path . '/' . $file . '" />';
} 

?>



P.S.
You may want to consider using mt_rand() instead of rand(). It's said to be 4x faster, and to generate "better" random numbers. (There is random... and then there is random :P)



Atli, Thanks, I did notice that, tried it, but didn't get a t-shirt. In fact, got nothing. Where as when I used just the $file, I at least got alt txt.
I can open the image location and get this PHP file, but its blank.

View PostDormilich, on 26 May 2011 - 10:12 PM, said:

it could be that your condition fails. you should also set the output MIME to text/plain.


Dormlich, How do you set the MIME in a PHP doc?
Was This Post Helpful? 0
  • +
  • -

#5 GorillaCheif   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-April 06

Re: random image from directory

Posted 28 May 2011 - 02:34 AM

Aight, I found some turnkey PHP scripts online, but am getting the same results with all of them. (would rather write my own for posterity's sake)
SO, I stsrted over...
<?php
function getRandomFromArray($ar) {
    mt_srand( (double)microtime() * 1000000 );
    $num = array_rand($ar);
    return $ar[$num];
}

function getImagesFromDir($path) {
    $images = array();
    if ( $img_dir = @opendir($path) ) {
        while ( false !== ($img_file = readdir($img_dir)) ) {
            // checks for gif, jpg, png
            if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
                $images[] = $img_file;
            }
        }
        closedir($img_dir);
    }
    return $images;
}

// specify root if images arent in a sub directory, right? or does it matter?
$root = 'http://www.X.com';
$root = $_SERVER['DOCUMENT_ROOT'];

$path = '/LCP/slide';

// Obtaining list of images from directory 
$imgList = getImagesFromDir($root . $path);

$img = getRandomFromArray($imgList);

?> 


//Then call that sucker with:
<img src="<?php echo $path . $img ?>" alt="" />

Anyway, it dawned on me to try thr php code and cal in the same document (duh, I know) and wound up with finally getting somewhere. I didn't get any images, but I got the directory to open...

This post has been edited by GorillaCheif: 28 May 2011 - 02:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: random image from directory

Posted 28 May 2011 - 08:11 AM

Did you open the page source and check the value of src in
<img src="RANDOM_GENERATED_IMAGE_LINK_HERE" alt="" />
?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: random image from directory

Posted 28 May 2011 - 08:43 AM

Try placing images in a subdirectory called images, then run this code:
<?php
$path = "images/";
$images = scandir($path);
$image_number = rand(2,count($images)-1); // start random number at 2 to avoid . and .. directories (0 and 1)
$path .= $images[$image_number];
echo $path;
?>

Was This Post Helpful? 0
  • +
  • -

#8 GorillaCheif   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-April 06

Re: random image from directory

Posted 28 May 2011 - 01:18 PM

View PostCTphpnwb, on 28 May 2011 - 08:43 AM, said:

Try placing images in a subdirectory called images, then run this code:
<?php
$path = "images/";
$images = scandir($path);
$image_number = rand(2,count($images)-1); // start random number at 2 to avoid . and .. directories (0 and 1)
$path .= $images[$image_number];
echo $path;
?>


Warning: scandir(images/) [function.scandir]: failed to open dir: No such file or directory in /X/X/public_html/LCP/php/home.php on line 3

Warning: scandir() [function.scandir]: (errno 2): No such file or directory in /X/X/public_html/LCP/php/home.php on line 3
images/

Still nothing...
Its calling the right file (home.php).

Going back to the second script I tried.. I see a marker that opens the directory, but cannot seem to get the images in that directory to show. I'm missing something somewhat simple here...why isn't it showing those images?
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: random image from directory

Posted 28 May 2011 - 03:21 PM

So you're saying that if you have it set up like below it doesn't work??

Try the attache zip file

Attached image(s)

  • Attached Image

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: random image from directory

Posted 28 May 2011 - 04:45 PM

View PostGorillaCheif, on 28 May 2011 - 10:51 AM, said:

Dormlich, How do you set the MIME in a PHP doc?

header("Content-Type: text/plain");

Was This Post Helpful? 0
  • +
  • -

#11 GorillaCheif   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-April 06

Re: random image from directory

Posted 28 May 2011 - 08:15 PM

CTphpnwb, I get a text of the individual file 'images/screenshot2.png' but no images.


Ok, I just got it! Man, I was making this way more complicated than it is.

Ok, now to figure a way to resize the image...any ideas?
Was This Post Helpful? 0
  • +
  • -

#12 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: random image from directory

Posted 28 May 2011 - 10:21 PM

View PostGorillaCheif, on 29 May 2011 - 03:15 AM, said:

Ok, now to figure a way to resize the image...any ideas?


There are two ways:
  • Have the browser do it, by setting the image size in the HTML or CSS.
    <img src="..." alt="..." style="width: 100px; height: 75px;">
    
    

    This is very simple to do, but you are depending on the browser to do the actual resizing, which doesn't always result in great quality. Also, if you are reducing the size, you are wasting bandwidth.

  • Or yo can have PHP actually resize the image before sending it. It's a little more complex, especially if you plan on using some sort of caching mechanism (which you should, or you're wasting resources like crazy). - Although, this has been created and recreated so many times in the last decade that the internet is littered with example code.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1