on my site, I have a page with the picture of the day
I used the function getRandomImages() for that.
but I would only use the images of a single album, not all images of the gallery.
there is the function getRandomImagesAlbum() for that, but this function does not have an image per day.
how can I do to get a daily random image from a specific album?
thanks for your help
Comments
$potd = unserialize(getOption('picture_of_the_day'));
$image = NULL;
if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {
$album = new Album($_zp_gallery, $potd['folder']);
$image = newImage($album, $potd['filename']);
}
if (is_null($image) || !$image->exists) {
$image = getRandomImageAlbum($rootalbum);
$potd = array('day' => time(), 'folder' => $result['folder'], 'filename' => $imageName);
setOption('picture_of_the_day', serialize($potd));
}
`
Caveat: This code had not been tested.
i have tried to use this script, but it doesn't work.
can you explain how it works and the variables to use?
gets the stored value of the current "image of the day"
`$image = NULL;`
`if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {`
If the image of the day is current then create an image object from the stored data
`$album = new Album($_zp_gallery, $potd['folder']);`
`$image = newImage($album, $potd['filename']);`
`}`
`if (is_null($image) || !$image->exists) {`
If there is no image of the day (or it was obsolete) then get a random image from $rootalbum--the album object of the album from whence you want the images
`$image = getRandomImageAlbum($rootalbum);`
`$potd = array('day' => time(), 'folder' => $result['folder'], 'filename' => $imageName);`
`setOption('picture_of_the_day', serialize($potd));`
Store the data on this image for future page refreshes (see above)
`}`
I followed your advice and wrote the following code in the Codeblock1 of a new page.
`
<?php<br />
$potd = unserialize(getOption('picture_of_the_day'));
$image_of_the_day = NULL;
if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {
$specific_album = new Album($_zp_gallery, $potd['folder']);
$image_of_the_day = newImage($specific_album, $potd['filename']);
}
if (is_null($image_of_the_day) || !($image_of_the_day->exists)) {
$specific_album = new Album($_zp_gallery, "temp/essai");
$image_of_the_day = getRandomImagesAlbum($specific_album);
$potd = array('day' => time(), 'folder' => $result['folder'], 'filename' => $imageName);
setOption('picture_of_the_day', serialize($potd));
}
if (is_object($image_of_the_day) && $image_of_the_day->exists) {
echo "";
echo "getCustomImage(700, null, null, null, null, null, null, true))."\" alt=\"" . html_encode($image_of_the_day->getTitle()) . "\" />\n";
echo "";
}
?>
`
but it does not work and I always have a random image that is provided.
- the image is chosen throughout the gallery (and not in "temp/essai" album as requested).
- the image changes each refresh of the browser.
Can you help me to solve my problem ?
If you keep getting a new image on the refresh it means one of two things.
1. the first "if" statement is not taking the "true" path. That would indicate that the date is alway different from the POTD date.
2. the result of setting up the image in the "true" path is failing somehow. That could only be if the image does not somehow exist.
So, we need to determine which case is the issue. Add some debugging code to your theme to see which path you have taken. For instance, in the "true" path place an `echo "
TRUE path";` and in the code that makes a new random image place `echo '
No image:";if (is_null($image_of_the_day) echo " image is NULL"; else echo " image must not exits";`
Once you determin what went wrong you can put in debug code to see why.
i follow your instructions like that :
<bloquote>
<div id="daily-image">
<?php
global $_zp_gallery;
$potd = unserialize(getOption('picture_of_the_day'));
$image_of_the_day = NULL;
if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {
echo 'true path';
$specific_album = new Album($_zp_gallery, $potd['folder']);
echo 'titre album :'.$specific_album->getTitle().'';
$image_of_the_day = newImage($specific_album, $potd['filename']);
}
if (is_null($image_of_the_day) || !($image_of_the_day->exists)) {
if (is_null($image_of_the_day)) echo "image is null".'';
if (!($image_of_the_day->exists)) echo "image not exits".'';
$specific_album = new Album($_zp_gallery, "temp/essai");
echo $specific_album->getTitle().'';
$image_of_the_day = getRandomImagesAlbum($specific_album);
echo $image_of_the_day->getTitle().'';
$potd = array('day' => time(), 'folder' => $result['folder'], 'filename' => $imageName);
setOption('picture_of_the_day', serialize($potd));
}
[...]
?>
</div>
</bloquote>
Results :
- "true path" is displayed : the date is not different from the POTD date, but the title of specific_album is empty
- "image is null" and "image not exits" are also displayed ; the title of specific_album and image_of_the_day are displayed.
according to you, where is the problem ?
BTW, you must be getting PHP script errors since if the image is NULL, the next statemet that uses it as an object will fault. So I think you best find your CGI error logs and find what errors you are getting. That may also help us.
$specific_album = new Album($_zp_gallery, "mariage");
echo $specific_album->getTitle();
$image_of_the_day = getRandomImagesAlbum($specific_album);
echo $image_of_the_day->getTitle();
- $specific_album is the good one
- whatever I do, $image_of_the_day is taken in all the gallery, and not only in mariage's album.
where can I find my CGI error logs ?
But I think I see the problem. getRandomImagesAlbum() expects the folder name of the album (if you are not using the current album). Looks like you are passing an album object.
there is the code :
$specific_album = new Album($_zp_gallery, "mariage");
$image_of_the_day = getRandomImagesAlbum($specific_album);
with that code, $specific_album is an album object, don't you think ?
yes, $specific_album is an album object and not the folder name of the album as you told me in a previous post :
`
<?php<br />
global $_zp_gallery;
$potd = unserialize(getOption('picture_of_the_day'));
$image_of_the_day = NULL;
if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {
echo 'true path
';
$specific_album = new Album($_zp_gallery, $potd['folder']);
echo 'titre album :'.$specific_album->getTitle().'
';
$image_of_the_day = newImage($specific_album, $potd['filename']);
}
if (is_null($image_of_the_day) || !($image_of_the_day->exists)) {
if (is_null($image_of_the_day)) echo "image is null".'
';
if (!($image_of_the_day->exists)) echo "image not exits".'
';
$image_of_the_day = getRandomImagesAlbum('mariage');
echo $image_of_the_day->getTitle().'
';
$potd = array('day' => time(), 'folder' => $result['folder'], 'filename' => $imageName);
setOption('picture_of_the_day', serialize($potd));
}
[...]
?>
`
with that, it ok, the random image is taken in the good album "mariage".
...but, the image changes each refresh of the browser (the initial problem still the same).
the first "if" is ok, but the album returned by
`
$specific_album = new Album($_zp_gallery, $potd['folder']);
`
seems to be empty
I finally understood the problem
the following code was wrong :
`
if (is_null($image) || !$image->exists) {
$image = getRandomImageAlbum($rootalbum);
$potd = array('day' => time(), 'folder' => $result['folder'], 'filename' => $imageName);
setOption('picture_of_the_day', serialize($potd));
}
`
because it is a copy/cut of getRandomImages() function.
$result['folder'] and $imageName are unknown, so picture_of_the_day didn't have the correct values stored and read.
the following code to have a daily random image from a specific album works well :
`
<?php<br />
global $_zp_gallery;
$root_album = 'mariage';
$potd = unserialize(getOption('picture_of_the_day'));
if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {
$album = new Album($_zp_gallery, $potd['folder']);
$image = newImage($album, $potd['filename']);
}
if (is_null($image) || !($image->exists)) {
$image = getRandomImagesAlbum($root_album);
if (is_object($image) && $image->exists) {
$potd = array('day' => time(), 'folder' => $root_album, 'filename' => $image->getFileName());
setOption('picture_of_the_day', serialize($potd));
}
}
[...]
?>
`
in fact, it would be a nice improvement to modify getRandomImagesAlbum() function to have a daily random image from a specific album.
could you do that in a next release of zenphoto ?