How to add a Flickr-based photo gallery to your website: phpFlickr + Lightbox.

Many people struggle with installing and setting up third-party gallery applications and although there are a couple popular image galleries that most web-hosts offer as an easy-to-install application, they can still be frustrating and pose many security issues for the inexperienced.

With the growing popularity of using Flickr to manage photos, more and more people are looking for an easy way to display these photos on their website.

After some playing around, I’ve settled with the phpFlickr and Lightbox combination to nicely display photo galleries on a website. PhpFlickr is a class written by Dan Coulter to act as a wrapper to Flickr’s API, and lightbox is a very simple and elegant way to display photos on a page. This tutorial will teach you how to quickly setup a photo gallery with phpflickr and lightbox on your own website.

So what do you need?

1. A flickr account (a free one with 100mb monthly upload is fine for most). You will also need to know your user ID (usually in the format xxxxxxxx@Nxx)

2. Latest version of phpflickr: can be downloaded here

3. Lightbox 2 java script: downloaded here

4. Obviously, your photos.

What to do:

1. Upload the photos you want to display to your Flickr account. Organize the photos into Sets to best manage the separation of “galleries.” The code below will not work if you have not put your photos into at least one set.

2. Install phpFlickr and Lightbox by easily extracting them to the directory of your choice (ie. /photos/ on your website). Both phpFlickr and Lightbox are ready to go out-of-the-box so you won’t need to play around with them too much if you don’t want to.

3. Include the necessary content in your php file.

For lightbox, the following will go in the header section of your php file:

<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript"
          src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>

and

<link rel="stylesheet" href="css/lightbox.css"
     type="text/css" media="screen" />

For phpFlickr, add the following code in the header section of your php file: (it’s possible the folder might be called phpFlickr-1.3 if you are using the latest version.

<?php
 require_once("phpFlickr/phpFlickr.php");
 $f = new phpFlickr("b16911dd0ec59f16ed66e80711edbdaf"); // API
 $user = "xxxxxxxx@Nxx";
 $ph_sets = $f->photosets_getList($user);
?>

Where the user value can be found in your flickr account.

Now we are ready to use phpFlickr to import and Lightbox to display our pictures from our Flickr sets. Here’s a simple bit of code I use that imports and displays the photos:

<div id="gallery">
 <?php foreach ($ph_sets['photoset'] as $ph_set): ?>
  <div class="photosets">
  <p><h2><?php print $ph_set['title']; ?></h2></p>
  <?php $photoset_id = $ph_set['id'];
  $photos = $f->photosets_getPhotos($photoset_id);
  foreach ($photos['photoset']['photo'] as $photo): ?>
  <div class="photos">
   <a rel="lightbox[]"
     href="<?= $f->buildPhotoURL($photo, 'medium') ?>"
     title="<?= $photo['title'] ?>">
   <img src="<?= $f->buildPhotoURL($photo, 'square') ?>"
     alt="<?= $photo['title'] ?>" title="<?= $photo['title'] ?>" />
   </a>
  </div>
  <?php endforeach; ?>
  </div>
 <?php endforeach; ?>
</div>

The CSS I use for this is:

#gallery {
	position: relative;
	float: left;
	width: 600px;
	padding: 0px 15px 10px 15px;
}

.photos {
	position: relative;
	float: left;
	display: block;
	padding-bottom: 10px;
}
.photosets {
	clear: both;
	padding-top: 1px;
}
.photosets h2 {
	color: #000;
}

The full documentation on both lightbox and phpFlickr is available in their downloaded packages. They are both quite powerful. The example given here is just a simple way to grab all the Sets in your Flickr account and display them in a gallery format.

I recently added this to the site designbyroeland.com. You can check out the example of how it will look here. Simple. Elegant.

EDIT:

If you’re looking to only display one set, this can be done with the following:

<?php $photos = $f->photosets_getPhotos('xxxxxxxxxxxxxxxxx'); ?>
<?php foreach ($photos['photoset']['photo'] as $photo): ?>
<div class="photos">
<a rel="lightbox[]" href="<?= $f->
    buildPhotoURL($photo, 'medium') ?>"
    title="<?= $photo['title'] ?>">
<img src="<?= $f->buildPhotoURL($photo, 'square') ?>"
    alt="<?= $photo['title'] ?>" title="<?= $photo['title'] ?>" />
</a>
</div>
<?php endforeach; ?>

Where the xxxx’s are replaced with the set ID that you can find in your flickr URL for that set. Feel free to republish this guide if you wish, but please try to link back if possible. Thanks!

EDIT (DESCRIPTION):

A lot of people have been asking me how to display the description of the Flickr photo instead of using the title within the lightbox display. Here is the change of code you will need in order to do this (very similar to the code in the tutorial, with 2 changes):


<div id="gallery">
 <?php foreach ($ph_sets['photoset'] as $ph_set): ?>
  <div class="photosets">
  <p><h2><?php print $ph_set['title']; ?></h2></p>
  <?php $photoset_id = $ph_set['id'];
  $photos = $f->photosets_getPhotos($photoset_id);
  foreach ($photos['photoset']['photo'] as $photo):
  $d = $f->photos_getInfo($photo['id']); ?>
  <div class="photos">
   <a rel="lightbox[]"
     href="<?= $f->buildPhotoURL($photo, 'medium') ?>"
     title="<?= $d['photo']['description'] ?>">
   <img src="<?= $f->buildPhotoURL($photo, 'square') ?>"
     alt="<?= $photo['title'] ?>" title="<?= $photo['title'] ?>" />
   </a>
  </div>
  <?php endforeach; ?>
  </div>
 <?php endforeach; ?>
</div>

The difference is the addition of the line $d = $f->photos_getInfo($photo['id']); as well as changing $photo['title'] with $d['photo']['description'] inside the ‘a’ lightbox tag. Unfortunately, this method isn’t very efficient as it calls the description inside the loop, which means it has to check the description for each photo. As far as I know there isn’t a more efficient way of grabbing all the descriptions for the photo set yet. I will keep my eyes peeled for it though.

EDIT (available sizes):

I use the size ‘medium’ in the above tutorial quite a bit. There have been a lot of questions about how to change this to its original size or to a larger size. There are a variety of sizes that are supported which include:

‘square’, ‘thumbnail’, ‘small’, ‘medium’, ‘medium_640′, and ‘large’. If you would like to display another size with lightbox, simply change where I have ‘medium’ to the option you’d like.

Did you find this guide useful? Share it with the social media buttons to the left or follow me on twitter and say hi – I’d love to hear from you if you’ve enjoyed this.

About Adam

Adam is a young entrepreneurial-spirited business owner who loves helping individuals and local business make money online. Adam writes on a few topics including local business Internet marketing, personal and professional development, and the odd rant and tangent as it pops up. Find out more about Adam.

, , , ,

126 Responses to How to add a Flickr-based photo gallery to your website: phpFlickr + Lightbox.

  1. Clare July 18, 2009 at 6:28 pm #

    I’m trying to add this, but I keep getting an error on the foreach (I think it’s line “foreach ($photos['photo'] as $photo):” I suspect this is because I can’t find ‘photo’ defined anywhere?

    The error I get is:

    Warning: Invalid argument supplied for foreach() in /home/xxx/public_html/gallery.php on line 91

    Thanks!

  2. johan July 20, 2009 at 10:02 am #

    Thanks!
    What would be the code to import one specific set?
    - Johan

  3. Adam August 6, 2009 at 8:41 am #

    @Clare

    Sorry for taking so long to reply, I’ve been offline for a while – trying to enjoy the summer :)

    The foreach() defines $photo on the fly there. You would be receiving this error if $photos['photo'] is not an array, as foreach() requires an array. Are you able to paste your code?

    @johan

    I edited my post to include the code for importing one specific set. It can be found at the end of the post.

  4. JeffreyATW August 15, 2009 at 6:25 pm #

    Hey Adam,

    Check your syntax on line 4 of your edit. Seems a rel=”nofollow”> made it in there somehow.

  5. Adam August 15, 2009 at 6:32 pm #

    Thanks, JeffreyATW.

    I had switched plugins to Code Markup by Bennett McElwee to display code in posts. It’s nice but was still getting used to it.

    Not sure how it got in there, but thanks for the heads up – it should be fixed now.

  6. Andrew August 18, 2009 at 6:11 pm #

    If you’re getting the same error as Clare -
    “Warning: Invalid argument supplied for foreach() in …”

    try using instead of

    foreach ($photos['photo'] as $photo):

    this code:

    foreach ($photos['photoset']['photo'] as $photo):

    that did it for me.

  7. Adam August 18, 2009 at 9:21 pm #

    Thanks for the heads up Andrew!

  8. Eric August 20, 2009 at 4:30 pm #

    Hi, this is a great tutorial but it seems that I have problem getting it work.
    This is the error that I got:
    Warning: Invalid argument supplied for foreach()
    which is this line: foreach ($ph_sets['photoset'] as $ph_set):

    Thanks for the help.

  9. Patrick August 24, 2009 at 12:45 pm #

    Hi Adam,

    First of all, Thanks for the great method of integrating Flickr. Everything works out for me, except for the lightbox. Any thoughts after looking at my source code? Thanks!

    Patrick

  10. Adam August 24, 2009 at 12:55 pm #

    @Eric : see Clare’s and Andrew’s comments above, this issue was addressed.

    @Patrick: all your included files look alright. I will check in more detail later, but it does look alright at first glance. Are you using some sort of CMS? Perhaps it’s an issue with it handling the javascript.

  11. Patrick August 24, 2009 at 1:51 pm #

    Thanks for the prompt response. I’m not using a CMS. The site is static for now.

  12. Patrick August 25, 2009 at 4:06 pm #

    Anyone else having problems with the lightbox not working? Everything else works fine.

  13. Mr Ploppy September 10, 2009 at 8:42 am #

    Hi

    I’ve got all of this to work, after using Andrew’s advice above – but would love to know how to add the description as the alt tag rather than just repeating the title.

    I have tried replacing ['title'] with ['description'] but just get errors.

    foreach ($photos['photoset']['photo'] as $photo):?>

    <a rel=”lightbox”
    href=”buildPhotoURL($photo, ‘medium’) ?>”
    title=”">
    <img src=”buildPhotoURL($photo, ‘square’) ?>”
    alt=”" title=”" />

    Cheers

  14. Mr Ploppy September 10, 2009 at 8:44 am #

    Sorry – the code didnt get digested properly:

    foreach ($photos['photoset']['photo'] as $photo):?>

    <a rel="lightbox"
    href="buildPhotoURL($photo, 'medium') ?>"
    title="">
    <img src="buildPhotoURL($photo, 'square') ?>"
    alt="" title="" />

  15. Mr Ploppy September 10, 2009 at 8:45 am #

    Oh bother :-( code still not getting through the input. Not done this before.

  16. J October 20, 2009 at 9:39 pm #

    I can’t get this to work. This is the error I’m getting:

    Fatal error: Call to a member function photosets_getPhotos() on a non-object in…

    Any ideas?

  17. Adam October 30, 2009 at 10:57 am #

    @J Can you provide the page you are trying to implement this on?

  18. Antonio November 13, 2009 at 9:42 pm #

    I also had a problem with my LightBox not working right, then I realized the files I extracted where not be called correctly

    ORIGNIAL—-

    EDIT– (in my case)

    Also I had to use andrews fix

    foreach ($photos['photoset']['photo'] as $photo):

    Otherwise everything works great! Thank you adam, if you need t-shirts printed I’ll be sure to hook you up!

  19. Adam November 13, 2009 at 9:52 pm #

    Thanks Antonio.

    I will add Andrew’s fix into the actual post to avoid confusion.

    Believe it or not I’m actually in the market for some T-shirt printing. I’ll ping you an email on your site.

    Cheers.

  20. Andy Fox January 18, 2010 at 4:06 am #

    Hey,

    Great post. I’m having a bit of trouble with it though…

    All the images show on the page as instead of the image.

    I’m wondering, do you need to have a Flickr API key for this to work?

    Just read about something like this on the phpFlickr documentation.

    Cheers,

    Andy

  21. Dana January 22, 2010 at 3:33 pm #

    Thanks for the great tutorial.

    I got everything working perfect, but I would really like to activate the “Next” and “Previous” buttons to click through the images. Is there some code that I can add to make this work?

  22. Kurt March 6, 2010 at 6:17 pm #

    If you want the next and previous to work per album/set add

    rel=”lightbox[]”

    If you want the next and previous to work for the whole photo stream add

    rel=”lightbox[flickr]“

  23. Kurt March 6, 2010 at 6:19 pm #

    My code was removed for the set. It should say “print $ph_set['id'];” between an open and close php tag

  24. Glen March 31, 2010 at 5:57 pm #

    Is there a way to style the image sizes within the css?

  25. Glen March 31, 2010 at 6:06 pm #

    Hi again sorry I figured that one out – dur!

    Next question. Is there a way to limit the number displayed on the page? Am I being thick again?

    :*)

  26. Simon Ainley April 1, 2010 at 11:47 am #

    Hi There,

    I am working on a site locally at the moment, I have successfully got the feed from flickr into my site, I am pulling the photosets in. However I only want to show the primary image for each set and the children of the set should only be viewable when the primary image is open in the light box, I currently have this code,

    photosets_getPhotos($photoset_id);
    foreach ($photos['photoset']['photo'] as $photo): ?>

    <a rel="shadowbox['']” href=”buildPhotoURL($photo, ‘medium’) ?>” title=”">
    <img src="buildPhotoURL($photo, ‘rectangle’) ?>” alt=”" width=”210″ height=”160″ title=”" />

  27. Adam April 2, 2010 at 1:52 pm #

    @Glen : You could build a count variable into your loop that only displays another picture if count <= however many pictures you want to display. You could use this to limit the number of photosets or photos within a photoset.

  28. Glen April 6, 2010 at 7:53 pm #

    Thanks Adam. Have you any idea how to display images from a Group pool?

  29. Michael April 7, 2010 at 12:42 am #

    I was also curious as to how to display only the primary photo in a set. Thanks so much for the great article.

  30. Renato Barone April 14, 2010 at 5:42 pm #

    Hey man, first thanks for this code! save my day….the only thing than i can’t do is show one set, like others the same error shows to me…

    on line

    i understand the part of the array, but to fix this?

    thanks again and sorry about my english!

    ps. the website is my url test the feature

  31. Matt April 21, 2010 at 3:03 pm #

    I have hundreds of sets, and thousands of photos on my Flickr account.

    I would like my gallery to display only the set names and set thumbnails at first. Then be able to click on the set thumbnail to display that set’s images.

    Is that possible? How hard would that be for me to set up?

  32. Charles May 9, 2010 at 4:08 am #

    I’ve seen this app in action before and thought I’d give it a spin on a super-simple website. However, after following all the directions and not tinkering with anything else, it doesn’t do anything for me. I’m not even getting error messages. Any assistance would be greatly appreciate. I’m pretty new to php, so let me know if I’m missing something.

    http://www.grabbagcomics.com/lively_productions/photogallery.php

  33. Charles May 9, 2010 at 4:56 am #

    Never mind, found my problem! Thanks!

  34. Renato Barone May 9, 2010 at 11:17 pm #

    Can i list my photos in ASC or DESC order?

    Thanks again!

  35. Setanta Sports LIVE May 13, 2010 at 8:08 pm #

    Thanks for sharing very good post.
    How to add a Flickr-based photo gallery to your website: phpFlickr + Lightbox. | Adam Bate
    thanks for post information.
    How to add a Flickr-based photo gallery to your website: phpFlickr + Lightbox. | Adam Bate

  36. Michael May 17, 2010 at 11:10 am #

    I was excited to get a program that was easy to install. And then I got totally stuck on the first step. Do I need to know something about PHP to use this? Step 3 said “3. Include the necessary content in your php file.” I saw several files with the word php in them, but I can’t open them. I feel like an idiot after reading the introduction.

  37. Mike May 18, 2010 at 3:02 am #

    I’m getting this error:

    Warning: require_once(72157623959485727/phpFlickr.php) [function.require-once]: failed to open stream: No such file or directory in /home/gnomepar/public_html/INDY500GUIDE.COM/tt.php on line 28

    Suggestions??

  38. Digital Photo Printing May 23, 2010 at 9:41 am #

    Who do you guys use to print photos?

  39. Toya Underberg May 30, 2010 at 4:01 pm #

    Sensational article bro. This is just a very nicely structured piece of writing, just the information I was looking pertaining to. Bless you

  40. Tim June 8, 2010 at 8:12 pm #

    Great tutorial! I just have one question, how do I have it display only the primary photo for each set?

  41. aigo June 8, 2010 at 10:58 pm #

    hi, it’s aigo, thanks for your sharing

  42. MattG June 28, 2010 at 1:18 pm #

    Can anyone tell me if it’s possible to make the images to go the “large” view, when clicked?

  43. Victor Quiroz July 2, 2010 at 6:46 pm #

    wow, thankyou so much, it helped me alot n_n

  44. Bucko July 22, 2010 at 11:12 pm #

    Hi,
    I’m having the same problem as what had been brought up at the beginning of the comments where I get:

    Warning: Invalid argument supplied for foreach() in /home4/bridalcl/public_html/rubberonroad.com/photos.php on line 76

    I followed the instructions from Andrew to replace the foreach() statement but still get the same result. I tried putting the code not into my site template but in just a blank php but got the same thing (just on line 50 instead of 76 because it’s a shorter code.

    Any suggestions? Thanks in advance!

  45. Luke July 29, 2010 at 9:06 am #

    Hiya,

    Thanks for the code. Is there a way to inport particular ‘photo sets’, so different sets can be used on different pages of my site?

    Regards,
    Luke

  46. Andre Reitz August 18, 2010 at 11:30 pm #

    Hey man, another newbie doubt!

    I’m making a lot of comments so, fell free to erase the others hehe!

    So, can’t I put image sizes manually? like square but with 200×200?
    Or it’s only possible to use the sizes on phpFlickr “large, medium, square and etc”?

    The problem is that if I use retangular and square images they look weird on page when using “small” sizes, so I wanted to use all square but bigger, is this possible?!

    Thanks very much!

  47. Chris J August 31, 2010 at 12:49 pm #

    Hi folks. I’ve tweaked this example (many thanks for doing the original legwork!) so that it initially displays a list of all current sets – then you can click on a set and it will show all the thumbnails for that set only. If you want to use the source, send me an email.

  48. Locksmith Knoxville TN September 27, 2010 at 9:48 pm #

    For reasons unknown i’m receiving a blank page once i attempt to post a comment,do you recognize how come its heading?i’m implementing oprea web-browser

  49. Dean Q September 30, 2010 at 12:21 pm #

    Hey Chris,

    I’d love to take a look at your code and use it, if I may. I couldn’t get your email, so I decided to comment here.

    My email is qssdean@gmail.com.

  50. Dean Q September 30, 2010 at 12:37 pm #

    Hi Adam,

    I’ve tried, but can’t seem to get it to display pictures from a specific set. I get the error: Warning: Invalid argument supplied for foreach() in.

    Was wondering if you could give me the code onwards? Sorry, I’m a real noob at php. :(

  51. Dean Q September 30, 2010 at 12:41 pm #

    @Adam: Ahhh ok I found the problem. Thanks very much for the code! Love how the way it works. :)

  52. simon October 7, 2010 at 7:27 am #

    I am trying to use the phpflickr whilst using shadowbox rather than the light box on here as I prefer its simpler design.

    However I am trying to show one individual set using the code you gave provided, no matter where I put it within my code it doesn’t seem to work, unless I’m getting it wrong some how? I put it in the gallery div, and tried putting it in the header, but can’t work it out??

  53. Clare October 20, 2010 at 6:03 am #

    Hiya,

    Some people are complaining they can’t see the galleries properly in Firefox & IE – I can see them fine in both.

    Also, will this be updated to use phpFlickr3.0?

    Thanks,

    Clare.

  54. Chris October 29, 2010 at 4:18 pm #

    OK, I managed to get Lightbox working on it’s own, but really struggling to integrate it with Flickr!

    The Lightbox function is working with the above code, although all images are broken and when I click on the single broken image a Lightbox pops up with a spinning wheel of death. It just seems as if I am unable to connect to our Flickr account.

    Any ideas? Thanks!

  55. Tim December 15, 2010 at 5:35 pm #

    Hey,

    The code for showing only one set isn’t working here. No errors, but it shows nothing. Must i add something (i have added the correct ID from my set)?

    The other code (for all sets) works fine.

  56. Siobhan January 14, 2011 at 3:47 pm #

    How can you make the images in the sets display horizontally instead of vertically?

  57. Dan February 2, 2011 at 7:44 am #

    @Tim 15. Dec, 2010 at 5:35 pm

    The array is still multi-dimensional so line 2 should read:

    That’ll do the trick. Hope this helps.

    Dan

  58. Chris Diplock February 6, 2011 at 9:15 pm #

    WHERE IS THE DONATE BUTTON???
    If ever a tutorial warranted a donate button it was this one, my only minor complaint is this line: $f = new phpFlickr(“b16911dd0ec59f16ed66e80711edbdaf”);

    which I worked out was the API Key but its not marked up as that.

    Put a donate button on, email me and I will sort you out, great work spent over 2 hours looking for a solution and this one is perfect!!

  59. Chris Diplock February 6, 2011 at 9:47 pm #

    Plus I have a very simple very clever code revision, change the line:

    <a rel="lightbox"

    to this:

    <a rel="lightbox[]”

    and this puts the different sets into groups which means you can go next and previous through the photo’s belonging to each set.

  60. Adam February 7, 2011 at 10:49 pm #

    @Chris Diplock

    Thanks for the kind words. I have added a donate button per your suggestion – I guess it can’t hurt.

    I’ve also made a few changes you mentioned so people can get the most out of the code.

    Thanks.

  61. Andrew March 18, 2011 at 9:30 am #

    I get this error ?

    Warning: require_once(phpFlickr/phpFlickr.php) [function.require-once]: failed to open stream: No such file or directory in /home/ison/web/arti-test/gallery.php on line 17

    Fatal error: require_once() [function.require]: Failed opening required ‘phpFlickr/phpFlickr.php’ (include_path=’.:/usr/share/php:/usr/share/pear’) in /home/ison/web/arti-test/gallery.php on line 17

    Cheers

    Andrew

  62. Adam March 18, 2011 at 2:28 pm #

    Andrew – it sounds like the file is missing or in the wrong spot: “failed to open stream: No such file or directory”

    require_once(“phpFlickr/phpFlickr.php”); <– that line of code is failing meaning it can’t find phpFlickr/phpFlickr.php. Make sure you have phpFlickr in the proper folder and note the cases as well. Also, it’s possible you might need to define the full path to the files, such as:

    /home/ison/web/arti-test/phpFlickr/phpFlickr.php (assuming that is where it is)

    Hope that helps.

    Adam.

  63. Kevan March 29, 2011 at 8:49 am #

    Hey Adam, love the tutorial – got it working quite fine.

    I’m testing it at the moment here: http://moreinmind.com/eva/portfolio.php – apologies ahead of time if you’re on a resolution smaller than 1680x wide – I haven’t done the second CSS for smaller resolutions yet.

    My question is this: Is there any way to change the size of the images when they pop up? I can’t seem to figure that out.

    Cheers!

  64. Adam March 30, 2011 at 9:50 pm #

    Thanks, Kevan. Glad you enjoyed the tutorial.

    Your site is looking great – it does cut off on my macbook though.

    Try changing the line buildPhotoURL($photo, ‘medium’) from ‘medium’ to ‘large’ or ‘original’. Make sure you have these sizes available in your flickr album though.

    Let me know if that did the trick.

    Cheers.
    Adam.

  65. Kevan April 1, 2011 at 3:35 am #

    Brilliant! That worked perfectly. I had to go with ‘large’ – using ‘original’ created a “photo can not be found” error from Flickr. It didn’t even occur to me to go over that code – I just assumed it was a problem with the .JS file.

    Thank you sir! Now all I have to do is size the photos properly and whip up a second CSS so there’s no cut off on smaller resolutions.

    Cheers!

  66. Adam April 4, 2011 at 1:54 am #

    Great to hear that worked!

  67. mark April 9, 2011 at 5:42 am #

    thanks adam! awesome and simple solution which was exactly what i was looking for. i also had to use the fix for the “EDIT” portion to use “foreach ($photos['photoset']['photo'] as $photo):” instead of “foreach ($photos]['photo'] as $photo):”.

    one other thing i used was a slight change of your css to style the photos slightly:

    .photos {
    position: relative;
    float: left;
    display: block;
    padding: 2px;
    margin: 8px;
    background: #fff;
    border-right: 2px solid #999;
    border-bottom: 2px solid #999;
    }

    thanks again!

  68. Adam April 11, 2011 at 11:10 am #

    Thanks, Mark. I added the fix into the EDIT section as well. Glad you found some value in it!

  69. Brad April 13, 2011 at 3:36 pm #

    First off, I love the writeup for this tutorial. Good stuff and thanks so much.

    Two quick questions: 1) Is there a trick for displaying the photo description rather than the title? 2) Your demo shows the close button, but mine are not showing up … any ideas?

    Thanks!

  70. Brad April 13, 2011 at 6:39 pm #

    OK, question one still stands, but I figured out the answer to #2. You have to define the whole path to the closelabel.gif in the lightbox.js file.

  71. christy April 14, 2011 at 5:47 pm #

    Exactly what I was looking for! However, I’m getting this error:

    Warning: Invalid argument supplied for foreach() …

    I’m getting the error when I try to use all sets or a single set. Thx for any help,

  72. Adam April 15, 2011 at 4:24 pm #

    @Brad without actually diving in to test, you can try this. For the occurrences of:

    $photo['title']

    in the alt and title attributes, try changing them to:

    Try changing the above line to:

    $photo['description']

    Unfortunately, I’m not sure that photosets_getPhotos() returns the description as I used it above, so if that doesn’t work let me know and I will do it using photo_getinfo();

    @christy

    I need a little bit more information than this. I use foreach() a couple times in the code, do you have a line number that it’s failing on? If you read through all the comments you might be able to find the answer to this.

  73. christy April 15, 2011 at 10:13 pm #

    It’s line 58, which is:

    The full error is:
    Warning: Invalid argument supplied for foreach() in /home/content/c/3/i/c3idesign/html/kmi/tesflkr/test2.php on line 58

    I’ve read thru a few times and tried using both all sets and a single set.

  74. Adam April 16, 2011 at 3:28 pm #

    Can you paste me your lines, 57, 58, and 59?

  75. christy April 16, 2011 at 9:32 pm #

    These lines (57, 58, 59) are from the full gallery:

    I get the same error when I try to display one set only, lines 57, 58, 59 are these:

    photosets_getPhotos(’72157626498379008′); ?>

  76. christy April 16, 2011 at 9:32 pm #

    I have a flickr account tied to my yahoo account. Could that have anything to do with it?

  77. christy April 16, 2011 at 9:34 pm #

    Trying again as the php code didn’t paste…

    /*

    */

    /*
    photosets_getPhotos(’72157626498379008′); ?>

    */

  78. christy April 16, 2011 at 9:36 pm #

    photosets_getPhotos('72157626498379008'); ?>

  79. christy April 16, 2011 at 9:37 pm #

    Trying once more with brackets removed:

    div id=”gallery”
    ?php foreach ($ph_sets['photoset'] as $ph_set): ?
    div class=”photosets”

  80. Adam April 16, 2011 at 10:56 pm #

    Sorry, looks like the code plugin doesn’t work properly with comments!

  81. christy April 16, 2011 at 10:59 pm #

    yes, that’s what I have. I had to remove the brackets for it to display for you. I don’t want to take up your time, I was just hoping to be able to get this to work.

  82. Adam April 16, 2011 at 11:03 pm #

    If you’d like to send me a message via my contact form I will take a closer look at your code.

  83. christy April 17, 2011 at 1:47 pm #

    Adam, my error (in user id). Thanks for the help, and great utility.

  84. Adam April 19, 2011 at 10:07 am #

    @Brad I have put up a solution for displaying the description of a photo using the getInfo() function. It is a little inefficient, but I hope it helps.

  85. Peter April 19, 2011 at 9:09 pm #

    Any idea what is causing this error?
    Warning: Invalid argument supplied for foreach() in /home/xxxxx/public_html/my_site/Flickr/index.php on line 28

    Thank you
    Peter

  86. Adam April 19, 2011 at 10:10 pm #

    Hey Peter, if you read above christy had this same issue. It seems there was a problem with her user ID.

    If you want to send me a message via my contact form I’d be happy to take a look, please include lines 27, 28, and 29 from your code as well.

  87. Dustin April 30, 2011 at 6:46 pm #

    Very nice! Easy to implement too. I will use this for sure.

  88. Aldo Alfaro May 24, 2011 at 11:26 pm #

    Hi, Thank for the great tutorial. I easily implemented the code to show only one set. What I’m trying to accomplish now is to show the list of sets with only the primary photo of each set (instead of every picture in the set), is that possible?

    Also, is it possible to select only certain sets, for example those with the word “nature” in the name?

    Thank you in advance for your answer.

  89. pestevez May 30, 2011 at 5:55 pm #

    Hi, this client code bring me well the title of the photo sets, it’s ok, but only show me red Xs, where no build the URL, please help me to resolve this, thanks .)

  90. 0161-Jon June 2, 2011 at 12:41 pm #

    fantastic tutorial thank you!!

    on the site I am building using this, every time I try and specify a set it causes errors, if I just use the full code for all photos it works fine.

    Takes a very long time to load the gallery page though when clicked from another page on the site, is there any way around this? Or at least a way of displaying a loading images message?

    thanks again!!

    jon

  91. michelle June 25, 2011 at 11:33 am #

    Your tutorial is great, however the prototype.js file is conflicting with another js file i am already using slideshow.js would you have time to take a look to see if you know why? I am new to js so any help would be appreciated.
    This is how the site should look without the gallery page: http://www.sfagan.co.uk/
    thanks,
    Michelle

  92. scrib July 6, 2011 at 5:59 pm #

    Great tutorial! Works wonderfully.

    Two questions:

    1. How do you limit the total number of photos retrieved from a given set? In other words, how do you only display 10 pictures from a set of 45?

    2. After answering #1 above, is there any way to make the 10 photos retrieved random, so that 10 different photos are retrieved each time the page reloads?

    I can use CSS to limit the size of the container DIV so that only a set number of pictures are visible, but I am using the EDIT (DESCRIPTION): method for the hyperlink titles and a photoset with a lot of pictures takes forever to load. So please give me the PHP method, if possible, for limiting the number of photos.

    Thanks!

  93. Richard July 7, 2011 at 7:59 pm #

    Thanks for this, it’s very useful code…I’m loving it. I have a question/challenge…

    How do I display my photos in albums instead of thumbnails? i.e. display only album covers, then when clicked on, open lightbox and display the rest of the images in that particular album.

  94. Prutser July 25, 2011 at 4:47 pm #

    Best Adam,

    Thanks for the create script, it works very well for us.

    Is it possible to display only the first 3 sets?
    Is it possible to display the sets behind each other, not in an line below?

    Thanks in advanced

  95. sachin walvekar August 22, 2011 at 7:32 am #

    Sorry Adam, I did not want to bother you……….but I am having same problem as with peter,

    Invalid argument supplied for foreach() in C:\wamp\www\website testing flickr\phpFlickr\example.php on line 21

    since I was getting an error, I tried to use the API in the example code supplied by you. I have check the API key…………

    I am wondering what solved the problem of Peter and Christy.

    Thanks for sharing your knowledge………….Now I am waiting for it to work.

    Cheers,

    sachin.

  96. sachin walvekar August 22, 2011 at 10:19 am #

    Sorry Adam I used my username instead of user id…………..which you have mentioned needs to be in the format…….xxxxxxxx@NXX.

    Here is a site I found to get this…………could not find where I could get it on flickr

    http://idgettr.com/

    Need to only replace the “username” and click on the button “find”

    Thanks once again for posting an excellent tutorial.

  97. Michael August 28, 2011 at 10:52 pm #

    Thanks, Adam for the awesome tutorial! It was VERY simple to get working and online! Once getting it setup, I realized I have a TON of sets with even more TONS of photos in each set!

    Do you think there’s a way to create a drop down field with the titles of the sets, and then when the Set title is selected from the drop down, it only shows that set’s photos?

    Thanks!

  98. Jan September 6, 2011 at 5:13 am #

    Hi Adam – this is great but I also have a problem with the single set version ie9 says:

    Warning: Invalid argument supplied for foreach() in /home/aclearw1/public_html/absolute-websites.co.uk/test2.php on line 33

    line 33 is:

    I’ve got the other ‘multiple set’ code working perfectly but I would prefer to use the single set version if possible.

    Thanks in advance
    Jan

  99. JCM September 9, 2011 at 5:39 pm #

    Hi Adam. Excellent job on the tutorial, the code works great!

    I would like it to only display a certain number of images per page, so that I can then add a ‘next’ link to go to the next page. I noticed that Flickr API has a rule that you are only supposed to display 30 photos per page. What I am thinking is something kind of like this person does here (http://net.tutsplus.com/tutorials/php/how-to-create-a-photo-gallery-using-the-flickr-api/), but I don’t really know php that well. Let me know if you have any thoughts and thanks again!

  100. Daniel September 13, 2011 at 6:04 am #

    Hi Adam,

    Thank you for this tutorial, I needed exactly this and it’s taken me a while to find you. (this tutorial) Great stuff. Quick question:

    I’d like to display only one photo set, but display the description of the photos rather than the title. I tried combining the two code bits (single set + description instead of title), but it didn’t work. Can you help? Thank you.

  101. Sam September 14, 2011 at 1:37 pm #

    Hey Adam,

    Great tutorial… Many thanks! I’ve been looking for a solution like this for a while now. The only issue I’m having is that when I display a single set, the set title isn’t displayed. Any help would be appreciated.

    Thanks,

    Sam

  102. Adam September 15, 2011 at 9:22 pm #

    @Sam Great point. I just noticed I left out retrieving the title to the single set display:

    Simply add these two lines above the existing code:

    < ?php $setinfo = $f->photosets_getInfo(’72157627541782067′); ?>
    < ?php print $setinfo['title']; ?>

    You can wrap the second line around h2 tags or whatever you use for a title, they just wouldn’t let me display them properly here.

  103. Adam September 15, 2011 at 9:36 pm #

    Sorry for the delay getting back to everyone:

    @Jan If you could email me your line I’ll be able to help – it looks like the comment box didn’t allow the line.

    @JCM Yes, that looks like a great gallery. It should be able to guide you through doing it over multiple pages. When I get some extra time I’ll take a closer look at it.

    @Daniel you should be able to combine the two pieces. Add in the:

    $d = $f->photos_getInfo($photo['id']); ?>

    And then use the

    title=”< ?= $d['photo']['description'] ?>“> in place of the title one.

  104. Daniel September 20, 2011 at 8:14 am #

    Thank you very much Adam. -I got it to work now.

    If you don’t have time to tackle the following – don’t worry about it at all!
    There might be others, who have wondered like me, whether there’s a line of code that will automatically adjust the size of the Lightbox in case the loaded flickr image is bigger than the browser window. I have seen WordPress Lightboxes do that – not sure if this can be implemented in the Lighbox used here.

  105. Rich October 8, 2011 at 5:13 pm #

    Is it normal that after implementing this gallery on a page, that there is a page delay of around 10 seconds when navigating to it? Is this the php calling all the images from flickr? Is there a way to get the page to load faster, but have the gallery itself delayed with a spinner or a loading image?

    Thanks

  106. Tabatha October 14, 2011 at 12:20 am #

    I’m trying to create a link inside the photo description so that they can download the original photo by clicking on it instead of going through flickrs site. It works properly on the flickr website but when not on the gallery. The link displays with the sets and makes the pop up not work. Any ideas?

  107. Tabatha October 14, 2011 at 1:04 am #

    Nevermind I figured it out. This would be the link <a href="buildPhotoURL($photo, ‘download’) ?>”>Download and add in “download” => “_b_d” to the buildPhotoURL function in phpflickr.php

  108. Tabatha October 14, 2011 at 11:31 pm #

    I can’t seem to get it so that the original photo is displayed. Even using original it’s giving the wrong URL. Is there a fix for this?

  109. Adam October 18, 2011 at 7:55 pm #

    @Tabatha – I’m not sure if ‘original’ is supported anymore to be honest.

  110. Adam October 18, 2011 at 7:59 pm #

    @Rich – 10 seconds seems a bit high. Some delay is expected as it needs to pull the images from flickr, but it should load the page first. That is, you should see the images appear as they load, while the rest of the page has loaded.

    Let me know if you are still having this issue and I’d be happy to take a peek.

  111. Doniani November 6, 2011 at 11:34 pm #

    Thank’s guy.

  112. Haider November 17, 2011 at 2:33 am #

    Tabatha, I had the same issue. Did the following instead of building the URL…

    $sizes = $f->photos_getSizes($photo['id']);
    foreach($sizes as $size) {
    if($size['label'] == “Original”) {
    $url = $size['source'];
    $download_url = str_replace(“.jpg”, “_d.jpg”, $url);
    }

    this is assuming all of your originals are .jpg’s

    I tested it out and it works without issue, at least for me so far.

  113. Adam November 26, 2011 at 1:45 am #

    @Haider – thanks so much for the info. I’ll be sure to add it in when I (finally) get around to updating this post.

  114. Paul December 15, 2011 at 12:32 pm #

    If you want the description to be returned with the other photo data, you should include it in the ‘extras’ argument as follows:

    $photos = $f->photosets_getPhotos($photoset_id, ‘description’);

    Then you can use it like:
    <a href="buildPhotoURL($photo, ‘medium’) ?>” title=”

  115. jeya January 12, 2012 at 1:37 am #

    hi Adam,

    this post is cool.. too intelligent. No words…

  116. Murtaza January 20, 2012 at 5:00 pm #

    Adam,

    Awesome tutorial. got the phpflickr module to work right way, but i am have some trouble with the light box.
    I dont get any error messages, but when i click on a photograph, it doesn’t open up in a lightbox, just goes to a different page.

    Any suggestions?

  117. Adam January 20, 2012 at 5:13 pm #

    Hey Murtaza – I’m not sure without seeing the code. As far as I know everything above is still working. Perhaps if you copy and pasted some of the code that the formatting didn’t follow correctly (can happen with quotes and the like).

    Be sure you’re including the library as well. Sorry I can’t be much more help.

  118. Anik January 25, 2012 at 2:53 pm #

    I am also trying this code and having the same error of foreach statement
    “Warning: Invalid argument supplied for foreach() in C:\wamp\www\Verbena_New\gallery.php on line 52″

    in this particular code foreach ($ph_sets['photoset'] as $ph_set):

    please help me out

  119. Anik January 25, 2012 at 3:41 pm #

    At last I am successful to add this script to my site. Thanks Adam
    but there is few more things I wanted to do. I want to add the description of each image and the comments by the users. Please help me out

  120. Alturiak January 27, 2012 at 10:15 pm #

    How do I use this code to access the group pool? Thanks!

  121. Adam January 28, 2012 at 2:29 pm #

    @Alturiak

    You’ll need to use the function specific for retrieving group pool photos which is pools_getPhotos(), similar to how we use the above function to retrieve photos from user sets.

    For more info you can check out the API for group pools here:

    http://www.flickr.com/services/api/flickr.groups.pools.getPhotos.html

  122. Adam January 28, 2012 at 2:34 pm #

    @Anik – for the comments, you’ll need to use comments_getList() to get a list of comments from each photo. Unfortunately the comments do not get returned with photosets_getPhotos() so you’ll need to use the variable $photo from the code above to grab and return comments_getList($photo) and cycle through the comments to display however many you want.

  123. Alturiak January 28, 2012 at 8:23 pm #

    @Adam

    Can it show me as doing? Because, I am not managing to do to work. I am not a programmer, I only followed what you described above. If it can help me, I will be thankful!

  124. Jeanee February 1, 2012 at 11:24 pm #

    HI Adam, Great tutorial! I have the gallery working correctly live online, but have not been able to get the images to display correctly in WAMP Server’s localhost. Instead, small symbold appear where the photos should be. Do you have any thoughts on what may be preventing this from displaying correctly in WAMP? Thanks again for the tutorial and any insight you might have!

  125. denise February 3, 2012 at 3:20 pm #

    I was interested in the questions asked above:
    Hi, Thank for the great tutorial. I easily implemented the code to show only one set. What I’m trying to accomplish now is to show the list of sets with only the primary photo of each set (instead of every picture in the set), is that possible?

    but I don’t see an answer to it?

  126. Tim February 3, 2012 at 5:23 pm #

    If your script is failing off the bat, as evidenced by the

    Warning: Invalid argument supplied for foreach()

    message, likely you are trying to use your Flickr account name instead of your Flickr id.

    you could fish around for it on Flickr, or just use this handy utility: http://idgettr.com/

    Adam, thanks for the slick script!

Leave a Reply