How to show random picture?

Anyone up to the task of writing code that displays a random image from all the galleries?
«13

Comments

  • Here's a bit of code that I hacked together. It doesn't print a link to the image at the moment, but that's fairly easy to add. I have this running as random.php in my /zen directory and it grabs and displays a random image on refresh. I'll adapt it tomorrow to make it more useful as an insertable object.

    `<?php<br />
    require_once('template-functions.php');

    $randomImage = getRandomImage();

    print "imagegetSizedImage('200')."' alt='".$randomImage->getTitle()."'>";

    function getRandomImage() {

    global $_zp_current_image;

    if(db_connect()) {

    $random=query_single_row('SELECT images.filename, images.title, albums.folder FROM images INNER JOIN albums ON images.albumid = albums.id ORDER BY RAND() LIMIT 1');

    $image = new Image(new Album(new Gallery(), $random['folder']), $random['filename']);

    return $image;

    } else {

    return false;

    }

    }

    ?>`

  • Works perfectly! If I manage to add the permalink myself, I'll post it here ;)
  • Hmm. I was close, but no success. Let me know if you manage to add a link.
  • trisweb Administrator
    Don't forget that image object it returns is mighty powerful - you can get everything from it - link, thumbnail, full size, any size, next/prev image in the album, the album itself (and thus, all images in the album including the image), etc etc.

    Randomness is a good idea though. When I implement sorting for the next version I'll add a random option, and also a "get random" method on both gallery and album (so, getRandomAlbum, and getRandomImage on both Album and Gallery depending on what scope you want to grab it from). Yay.
  • Is there a particular reason that getImageLinkURL is defined in template functions and not in Image?

    It's not that there's anything wrong with it being in template functions, it's just harder to access there if you're working outside of the normal zenphoto contexts. Maybe define all of the functions that are currently called in template functions with no parameters to accept an optional image object and fall back to $_zp_current_image if no Image is passed?
  • Is there any way I can call these random pictures on my Wordpress pages? For example, at my site, I use a flickr badge to display random photos. I'm trying to switch away from flickr and would like to keep this feature...
  • Since you were using the flickr badge style before, I fashoned this using the same technique that they use.

    First save this into your zen directory as random.php

    `<?php<br />
    require_once('classes.php');

    $imageSize = (isset($_GET['s']) ? $_GET['s'] : 200);

    $randomImage = getRandomImage();

    $imageLinkURL = getImageLinkURL($randomImage);

    print "document.write('imagegetSizedImage($imageSize)."' alt='".$randomImage->getTitle()."'>')";


    function getRandomImage() {

    global $_zp_current_image;

    if(db_connect()) {

    $random=query_single_row('SELECT images.filename, images.title, albums.folder FROM images INNER JOIN albums ON images.albumid = albums.id ORDER BY RAND() LIMIT 1');

    $image = new Image(new Album(new Gallery(), $random['folder']), $random['filename']);

    return $image;

    } else {

    return false;

    }

    }

    function getImageLinkURL($image) {

    if (zp_conf('mod_rewrite')) {

    return WEBPATH . "/" . urlencode($image->getAlbumName()) . "/" . urlencode($image->name);

    } else {

    return WEBPATH . "/index.php?album=" . urlencode($image->getAlbumName()) . "&image=" . urlencode($image->name);

    }

    }

    ?>`

    Then put this into your page:
    ``

    Also, you can pass a size in like so if you don't want the default size (200):
    ``

  • trisweb Administrator
    True. I'd say you're going a bit too far out of the zenphoto architecture. Though I understand your desire to create something like this, this is what I would call a "hack".

    Just know that I'm planning on making a "sidebar generator" widget that you can give some arguments like (total width, number of colums, number of rows, random or not, which album(s) to use) etc. It'll then create a DIV with columnsXrows images in it that's exactly as wide as you want.

    To those of you who don't want to hack yet, remember this is a developer preview release, and I wouldn't reccomend switching away from flickr just yet.

    reidab - regarding the placement of getAlbumLinkURL - yes, there is a reason. It does different things for different template contexts. If you're in an image, then the link points to the page of the album where the image resides; if not, then it just points to the album. Let's just say that if you're "working outside of the normal zenphoto contexts" then I'm not going to help you ;-)
  • Yes, this is definately and shamelessly a quick and dirty hack. I've been looking through the code since I wrote this and I've gotten a much better now for how things are actually structured.

    Thanks for writing clean, OO code! It's such a wonderful break from the stuff that I have to slog through on a day to day basis.
  • reidab,

    Did you manage to get the link for the code you posted first?
  • I managed to bend it to my will. Thanks for your code, reidab. This is what I ended up with:

    `<?php<br />
    require_once('zen/classes.php');

    $randomImage = getRandomImage();

    $imageLinkURL = getImageLinkURL($randomImage);

    function getRandomImage() {

    global $_zp_current_image;

    if(db_connect()) {

    $random=query_single_row('SELECT zp_photos_images.filename, zp_photos_images.title, zp_photos_albums.folder FROM zp_photos_images INNER JOIN zp_photos_albums ON zp_photos_images.albumid = zp_photos_albums.id ORDER BY RAND() LIMIT 1');

    $image = new Image(new Album(new Gallery(), $random['folder']), $random['filename']);

    return $image;

    } else {

    return false;

    }

    }

    function getImageLinkURL($image) {

    if (zp_conf('mod_rewrite')) {

    return WEBPATH . "/" . urlencode($image->getAlbumName()) . "/" . urlencode($image->name);

    } else {

    return WEBPATH . "/index.php?album=" . urlencode($image->getAlbumName()) . "&image=" . urlencode($image->name);

    }

    }

    ?>`

    To then show the random image, I use this code:

    `<?php echo "<a href='".$imageLinkURL."'>imagegetThumb()."' alt='".$randomImage->getTitle()."'>"; ?>`

  • Just a quick question. In every instance of the `getRandomImage()` function, the SQL statement fetches not only the filename and folder which are used to "make" the image, but also the title. This isn't used anywhere and I'm curious as to why it's fetched at all.
  • trisweb Administrator
    True, but it really won't hurt that much. It's a hack to begin with, so why are you surprised? Tweak it as you like.
  • Joen, I like how you've incorporated it directly into your theme. Just wondering where exactly you put the file and what code did you use to call up the random photo. Thanks.
  • beej,

    I would actually recommend against integrating things too closely at this time. ZP is still beta, and there are some ugly hacks in my integration, currently. I would recommend waiting until the core becomes a bit more mature.

    That said, if you still want to integrate things, there's a brief explanation here:

    http://www.noscope.com/journal/2005/09/zenphoto-08-released#comment-4569
  • hi guys,

    i have been trying to implement the random image funtion onto the front page of my ZP theme, but i cant seem to make it work. Im not clear on how to do it. Any pointers?

    Thanks
  • Thanks reidab,
    I've tweaked sometime with your hack and put a zenphoto badge on my site.

    See you,
    l.
  • Can anyone advise on how to tweak this code to give a Flickr style badge for use in the Wordpress sidebar (2 images across, 4 down for example).

    My php is shocking :-(
  • Can anyone possibly help me with this if they have a moment?

    I need to recode this to give 2 kinds of output:

    1) Flickr style square thumbnails x4 in a sidebar
    2) A large square image on the homepage, which is a scaled crop of the fullsize image.

    Many thanks

    James
    PS. No-one ever answers my posts :(
  • Hi folks,

    Cany anyone advise on this - surely it's not that hard to change the hack above to output thumbnails instead of a full pic?

    (If you actually know what you're doing that is! :-)
  • 3stripe,
    I understand that you may want this really really bad as I am sure others do as well, however you really must be patient as this is not a request forum but a support forum and we will and can help when we can. For me personally, I am attempting to write one and have found it difficult to work with the above code to get multiple images to display with it and am working on another method. This however has been put on the back burner because of home improvements/school/work and other tasks which take a far greater priority than creating a flikr badge let alone make a fully functional one in 3 days.
    I havent mentioned my "project" to anyone because I knew that I didnt have the time to be held to it and counted on for it.

    Why is it that many people seem to be getting frustrated with the speed as to which things are getting updated/resolved with zenphoto. I'll admit, zenphoto isnt developing as fast as I would like but zenphoto is still a very young and small project. yes it has the ability to get bigger and I beleive it will, but until the project gets big enough and draws real developers to this forum who are willing to help, the response time for requests like this are going to take some time. Trisweb I am sure has a full plate with 1 going to UC Berkley and 2 going there for computer science.. that is not easy.. along with recent security vulnerabilities he has to take care of and all the absolute crazyness with the push for subalbums. He does not work for us, he is doing a favor for us by creating a very nice application which he is offering to us for FREE. We should all appreciate it and help out where we can as opposed to criticize and pound him and his project.

    Basically everyone needs to be patient, most people are finishing up on their school year right now, just getting back from spring break and what not.. Put yourself in his shoes, UC Berkley computer science major, finishing up on your degree, maintaining a life outside of school and a job to pay for you needs, and creating a free piece of software.. which order would your priorities be?
  • Hey there,

    Thanks for taking the time to reply...

    I 100% understand the need for patience! Zenphoto really is a great project, and I'm ready and willing to make a donation when they will take them :-D

    I guess I was posting another request on the off chance that someone outside of the development team could help me... so sorry if it sound't like i was impatient with them in particular.

    ... anoyhow, I've got a website 95% finished (http://www.paulryding.com/index.php/) and now just need these two snippets of code to make it perfect - otherwise I wouldn't be so vocal. In the meantime I'll carry on trying to work all of this out myself.
  • This person just finished making what you were looking for...

    http://www.ruicruz.com/zenshow/
  • :-D :-D :-D
  • thank you the publicity :)

    I have some more plans to develop the plugin even further but time is very scarce over here.
  • pedant Member
    hi all

    i want to add this feature, i have a main page that i want to add this to, can someone post simply how to do it and what info i need to change to get it to work? I am new to this php stuff so im struggling a little!
  • Has anyone managed to get this working properly yet? I've tried the above code but get mysql errors. I wonder if it's because I've upgraded to the latest release. Anyone????
  • Climberusa-

    I have managed to cobble together a bit of what you were looking for from this site. An example is at http://www.thinkdreams.com/zenphoto. The random image plugin (Zenshow) is working on my front page (running Wordpress 2.0.3 and zenphoto 1.0.3) at http://www.thinkdreams.com. I modified the stopdesign theme to my own color scheme, and added a functions.php with the random functionality. I also managed to get some exif functionality as well, outside of zenphoto, so if you upgrade, you won't lose any of the functions. I won't be releasing a theme on what I've done, since I just modified what others have already done. But I'd be glad to show anyone that asks what I did to get it to work. All information was either found on these forums, or done through copious amounts of research online. I love the zenphoto application, and I've already created 3 other converts to zenphoto including a photographer, a real estate agent, and possibly a baby boutique.

    Craig.
  • Daxeno Member
    ThinkDreams,

    How did you integrate that function in the stopdesign theme? Do you mind sharing it with us? ;)

    Daxeno
  • daxeno-

    here is my custom functions.php file.

    <?php

    // Get Random Images

    function getRandomImages() {
    $result = query_single_row("SELECT images.filename, images.title, albums.folder FROM images INNER JOIN albums ON images.albumid = albums.id ORDER BY RAND() LIMIT 1");
    $image = new Image(new Album(new Gallery(), $result['folder']), $result['filename']);
    return $image;
    }

    // work in progress - commented out
    // function getRandomImages() {
    // global $_zp_gallery;
    // return $_zp_gallery->getRandom();
    // }

    // Get Random Images Link

    function getURL($image) {
    if (zp_conf('mod_rewrite'))
    {
    return WEBPATH . "/" . urlencode($image->getAlbumName()) . "/" . urlencode($image->name);
    }
    else
    {
    return WEBPATH . "/index.php?album=" . urlencode($image->getAlbumName()) . "&image=" . urlencode($image->name);
    }
    }

    ?>
Sign In or Register to comment.