• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.

Load Image Randomly

Here is what I have - but not sure where to insert in view.php.
Code:
<?php
$imageFolder = 'images';

$images = array();
$dirh = opendir($imageFolder);
while( $file = readdir($dirh) ) {
    if( $file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

$imageFileName = $images[rand(0, count($images)-1)];

echo '<img src="' . $imageFolder . '/' . $imageFileName . '" alt="" />';
 
Open content/themes/Peafowl/view.php and find this:

Code:
<?php include_theme_header(); ?>

Paste this after that code:
Code:
<?php
$images = array();
$dirh = opendir(__CHV_PATH_IMAGES__);
while($file = readdir($dirh) ) {
    if($file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

$new_image_url = absolute_to_relative(__CHV_PATH_IMAGES__).$images[rand(0, count($images)-1)];
?>

Then find this:
Code:
<a href="<?php show_img_url(); ?>" target="_blank"><img src="<?php show_img_url(); ?>" alt="<?php show_img_filename(); ?>" id="full_image" /></a>

Replace that with this:
Code:
<a href="<?php echo $new_image_url; ?>" target="_blank"><img src="<?php show_img_url(); ?>" alt="<?php show_img_filename(); ?>" id="full_image" /></a>

;)
 
Rodolfo said:
Open content/themes/Peafowl/view.php and find this:

Code:
<?php include_theme_header(); ?>

Paste this after that code:
Code:
<?php
$images = array();
$dirh = opendir(__CHV_PATH_IMAGES__);
while($file = readdir($dirh) ) {
    if($file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

$new_image_url = absolute_to_relative(__CHV_PATH_IMAGES__).$images[rand(0, count($images)-1)];
?>

Then find this:
Code:
<a href="<?php show_img_url(); ?>" target="_blank"><img src="<?php show_img_url(); ?>" alt="<?php show_img_filename(); ?>" id="full_image" /></a>

Replace that with this:
Code:
<a href="<?php echo $new_image_url; ?>" target="_blank"><img src="<?php show_img_url(); ?>" alt="<?php show_img_filename(); ?>" id="full_image" /></a>

;)

I did everything exactly as shown above. But it is not working. When on the viewer page and click on image, the link goes to a different raw image in a new window - From /?v=jhgyyu4.jpg to images/ewebh.jpg (raw image) .

I want it to always stay in the same window and viewer page and load new image as long as you keep clicking on the image. And the new image should be at /?v=xxxxx.jpg (viewer)- not in images/xxxx.jpg (raw image).
 
Ah..

Replace this:
PHP:
$new_image_url = absolute_to_relative(__CHV_PATH_IMAGES__).$images[rand(0, count($images)-1)];

With this:
PHP:
$new_image_url = __CHV_BASE_URL__.'?v='.$images[rand(0, count($images)-1)];
 
It's opening the new image in a new browser window. I want it to open in the same window.

Also when it has no more new images to show it goes to /?v=thumbs with a Not found (404) error, saying the server can't find the requested page.

Also when you keep clicking sometimes it opens the same image that was shown in the previous one.
 
xipr said:
It's opening the new image in a new browser window. I want it to open in the same window.

Also when it has no more new images to show it goes to /?v=thumbs with a Not found (404) error, saying the server can't find the requested page.

Also when you keep clicking sometimes it opens the same image that was shown in the previous one.


Replace

Code:
<a href="<?php echo $new_image_url; ?>" target="_blank"><img src="<?php show_img_url(); ?>" alt="<?php show_img_filename(); ?>" id="full_image" /></a>

with

Code:
<a href="<?php echo $new_image_url; ?>"><img src="<?php show_img_url(); ?>" alt="<?php show_img_filename(); ?>" id="full_image" /></a>
 
One more problem - when it goes through the entire images folder, it is also going through these two files: /?v=thumbs and ?v=.htaccess - which leads to a 404 error. I want it to ignore the the thumbs folder and the .htaccess file.
 
xipr said:
One more problem - when it goes through the entire images folder, it is also going through these two files: /?v=thumbs and ?v=.htaccess - which leads to a 404 error. I want it to ignore the the thumbs folder and the .htaccess file.


Simply change this :

PHP:
$images = array();
$dirh = opendir(__CHV_PATH_IMAGES__);
while($file = readdir($dirh) ) {
    if($file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

with this :

PHP:
$images = array();
$dirh = opendir(__CHV_PATH_IMAGES__);
while($file = readdir($dirh) ) {
    if(is_file($file) && $file != '.htaccess' ) {
        $images[] = $file;
    }
}
 
Danny.Domb said:
xipr said:
One more problem - when it goes through the entire images folder, it is also going through these two files: /?v=thumbs and ?v=.htaccess - which leads to a 404 error. I want it to ignore the the thumbs folder and the .htaccess file.


Simply change this :

PHP:
$images = array();
$dirh = opendir(__CHV_PATH_IMAGES__);
while($file = readdir($dirh) ) {
    if($file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

with this :

PHP:
$images = array();
$dirh = opendir(__CHV_PATH_IMAGES__);
while($file = readdir($dirh) ) {
    if(is_file($file) && $file != '.htaccess' ) {
        $images[] = $file;
    }
}

Unforunately it is not working. When clicking on the image it now directly goes to /?v= with a 404 error message.
 
Now it's working! The only issue now is - I have over 200 images in the folder - but the script only repeats the first 20 images randomly and always goes back to images that have been seen already. I want it to go through the entire images in the folder.
 
Well, if you server is running on windows it has been proved that windows algorith to radomizing is very poorly coded.

A suggestion could be to list all images into an array and save it in a session, shuffle this array and loop trougth the array, that would allow to never view the same images anymore.

I don't intend to code it.
 
Danny.Domb said:
Well, if you server is running on windows it has been proved that windows algorith to radomizing is very poorly coded.

A suggestion could be to list all images into an array and save it in a session, shuffle this array and loop trougth the array, that would allow to never view the same images anymore.

I don't intend to code it.

I am running into the same problem again, that the first 30 images are being repeated. Will anyone here be able to code it? I want to list all images into an array and save it in a session, shuffle this array and loop trough the array. Any help is appreciated!
 
Back
Top