• 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.

Random Images

stuart

Chevereto Noob
I've just added my own home brew random image module to the bottom of my new adult image hosting site :)
Feel free to critique it and I've provided the code below if anyone has any suggestions to make it faster / better coded.

http://www.imagetease.com/

PHP:
<?php
//path to directory to scan. i have included a wildcard for a subdirectory
$directory = "images/thumbs/";

//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");

$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }

//shuffle array
shuffle($imgs);

$imgs = array_slice($imgs, 0, 12);

//display images
foreach ($imgs as $img) {
$imgname = str_replace("images/thumbs/", "", $img);
echo "<a href=\"http://www.imagetease.com/?v=$imgname\"><img src='$img' class=\"randomphotos\"/></a>";
}
?>
 
Hmm... okay their is a lot of things to be improve in your code, but here are some suggestions...

PHP:
<?php
// There is no point of setting the directory path, since chevereto have already a constant for it....

// you are not force to add an "". before a variable or a constant
// instead of $directory lets use the chevereto constant
// Many thinks glob is limited to a single file type or directory, but it isn't so with GLOB_BRACE we can select as many files we want.
$images = glob('{'.__CHV_PATH_THUMBS__.'*.jpg,'.__CHV_PATH_THUMBS__.'*.png,'.__CHV_PATH_THUMBS__.'*.gif}', GLOB_BRACE);

// Images is already an array, so why should we make an array with an array?

//shuffle array
shuffle($images);

// instead of slicing the array, lets just make a shorter loop 
for ($i = 0; $i < 12; $i++)
{
    // we are going to use the ' quote because we are outputing html
    // basename return the file name, so no need to use the str replace
    // Still no points in using a absolute path, a relative one will do the job.
    // I don't like simply using ?v= as a relative path, so lets use an absolute path...
    print('<a href="'.absolute_to_url(__CHV_ROOT_DIR__).'?v='.basename($images[$i]).'"><img src='.absolute_to_url($images[$i]).' class="randomphotos" /></a>');
}
?>

I didn't test it, tell me if you have any errors...
 
Thanks Danny thats just the feedback I was after :) Some great points in there and I learnt a thing or two I didn't know about PHP like the (.basename) option!
 
Intrestingly that code you sent is not working and is not generating any errors what so ever within the error_log... It's almost like its returning zero images... Any ideas?
 
stuart said:
Intrestingly that code you sent is not working and is not generating any errors what so ever within the error_log... It's almost like its returning zero images... Any ideas?

Doing a simple test "echo $images[1];" returns nothing, It looks like the $images array is empty...
 
Did you active the error reporting in chevereto? If you didn't their is not error that will be logged.


Anyway here is the fix:

first of all, I did a wrong copy and paste so instead of using the constant __CHV_PATH_THUMBS__ I was using _CHV_PATH_THUMBS__ (missing a _)

The second problem is really strange, anway, chevereto do not allow the index.php?v=XXX ... very strange... anyway simply remove the index.php

the third problem would be that glob return an absolute path, and we want an url, so I used absolute_to_url() function of chevereto to replace the absolute path to an url...

I fixed my code above.
 
Danny.Domb said:
Did you active the error reporting in chevereto? If you didn't their is not error that will be logged.


Anyway here is the fix:

first of all, I did a wrong copy and paste so instead of using the constant __CHV_PATH_THUMBS__ I was using _CHV_PATH_THUMBS__ (missing a _)

The second problem is really strange, anway, chevereto do not allow the index.php?v=XXX ... very strange... anyway simply remove the index.php

the third problem would be that glob return an absolute path, and we want an url, so I used absolute_to_url() function of chevereto to replace the absolute path to an url...

I fixed my code above.

Cheers mate thats perfect! I was using the raw apache logs which to the best of my knowledge will always log the error no matter what Chev says... I think Chev just suppresses on-screen errors?

Anyhow thanks v v much this is much faster than my code, managed to save a few MS loading time :p
 
Apache will log php syntax errors, but not if you are using a function with wrong parameters, or like in my case, a constant that do not exist. These are php error, and therefore will be log in the php log. Chevereto do not simply suppresses on-screen errors, it stop the logging of them completely, so if their is no syntax error, a "php" error, then these will neither be shown or logged.
 
Danny.Domb said:
Apache will log php syntax errors, but not if you are using a function with wrong parameters, or like in my case, a constant that do not exist. These are php error, and therefore will be log in the php log. Chevereto do not simply suppresses on-screen errors, it stop the logging of them completely, so if their is no syntax error, a "php" error, then these will neither be shown or logged.

Ah cheers for another great lesson in the world of php and chev :)
 
Back
Top