How to Load Random Flickr Images with PHP

This isn’t rocket science, but I did search Google extensively before building my own script.  Save the script as Random_Flickr_Image.php (or whatever) and reference it in your img tag.  For example, <img src=”/images/Random_Flickr_Image.php”/>.

Edit: I’ve updated the script to it works again. Flickr must have updated the image element class from photoImgDiv to photo-div.


<?php
$doc = new DOMDocument();
@$doc->loadHTMLFile("http://www.flickr.com/explore/interesting/7days/");
$xpath = new DOMXpath($doc);
if($xpath){
   $url = $xpath->query("//td[@class='Photo']/span/a/@href");

   @$doc->loadHTMLFile("http://www.flickr.com".$url->item(0)->nodeValue);   
   $xpath = new DOMXpath($doc);
   
   if($xpath){
      $url = $xpath->query("//div[@class='photo-div']/img/@src");
      $im = @imagecreatefromjpeg($url->item(0)->nodeValue);
      if($im){
         header('content-type: image/jpeg');
         imagejpeg($im);
      }else
         echo "error";
   }
}
?>
You can leave a response, or trackback from your own site.
  • http://johanronstrom.se Johan Ronström

    Nice!

    No way to get them bigger than the 7-days page preview, I guess?

  • Jamie

    I’m positive that there’s a better way that gets the larger sizes. I didn’t play around with it long enough to figure that out. But at the very least, you could load the page that the thumbnail points to and then screenscrape the image url. Probably just a few more lines of code.

  • Jamie

    I just tried a couple of things out. Without logging in to Flickr, it doesn’t look like you can see the magnifying glass on the pages and view larger images. However, I did find that the <div> on the page with class=”photoImgDiv” had a larger image than the one in the 7-day preview.
    I’ve changed the code to use that version instead of the smaller thumb.

  • Pingback: Random Flickr Image Slider Puzzle Using Canvas Tags | PlanetB

  • Pingback: Johan Ronström » Blog Archive » Image game

  • http://twitter.com/yovkov yovkov

    This would do the job for big images:

    loadHTMLFile(“http://www.flickr.com/explore/interesting/7days/”);
    $xpath = new DOMXpath($doc);
    if($xpath){
    $src = $xpath->query(“//td[@class='Photo']/span/a/img/@src”);
    $url = $xpath->query(“//td[@class='Photo']/span/a/@href”);

    // $srcc = str_replace(“_m”, “_b”, $src);
    $srcc = $src->item(0)->nodeValue;
    $srcc2 = str_replace(“_m”, “_b”, $srcc);

    echo ”;
    }
    ?>

  • Anonymous

    @yovkov – Awesome. Thanks!

  • http://fwrite.org Adriaan
  • http://www.guillaumes.ca Guillaume

    Hello,

    Thanks for the code !
    Would you please provide us a little bit more help. I am not abble to make it work..

    Do I am the only one ?

  • http://www.facebook.com/dirk.krause Dirk Krause

    @guillaume: nope you are not. doesn’t work for me either.

  • http://www.facebook.com/dirk.krause Dirk Krause

    seems to be a php4 vs php5 issue.

  • http://twitter.com/JamieBeach Jamie Beach

    I’ve updated the code. Looks like Flickr changed the class name for the image from photoImgDiv to photo-div.

  • http://www.facebook.com/dirk.krause Dirk Krause

    Ah, i didn’t see the difference while looking at the source. Works like a charm now. Thx, Jamie!

  • http://www.facebook.com/flashbuddy Flash Alexander

    Fun script. I threw in timthumb to resize on the fly: 
    http://centralcoastwedding.com/aflickrtest.php

    [code]echo '';[/code]

    How about grabbing photos from a particular set or with a particlar tag, or a random photo from a particular group?