Building A Better Drupal Photo Gallery

There are a lot of ways to build a image gallery in Drupal, but most of them are very complicated, require users to make a view for each gallery, and don't end up looking the way you want.

Don't make them think!

For G.Willikers! Toy Store I set out to build a better photo gallery that would allow users to easily create new gallery pages and upload images to them – without having to know anything about cropping images or how to create a new view.

The end result is an new, previously undocumented (as far as I can tell) image gallery that is intuitive to use and easy to set up.

Check out the G.Williker's! photo gallery to see the final product.

Before I set out to build my own gallery, I tried all the modules, and read as many blogs and handbooks pages as I could. The problem with most modules is that they can't be easily customized. Other directions require you to create "image gallery" taxonomies and upload images individually, and then style the page with views making it impossible for an end user to do without serious training. This is what I found to be the easiest way.

Using CCK, Image Field and Contemplate

To do this you are going to need CCK, ImageField, and Contemplate. Image Cache, Thickbox and Views are optional.

  1. Create a content type. Let's call it "Imge Gallery", and leave all the fields body, title, etc...
  2. Next add an image field and call it something like "gallery" Alow multiple images to be uploaded. Enable custom title text if you like.
  3. Go to Image cache and create your perfect thumbnail size. I usually scale the image to a slightly larger size then I want, center it, and then crop it to size. That's what I've found works best.
  4. Create Content > Image Gallery, and upload a bunch of images to this page. Also add some words to the Body.
  5. So after you've uploaded all your images, go to Display Fields within your image gallery content type and and select Thickbox:(name of imagecache thumbnail). Make surethe label is hidden.
  6. Now your ready to style your page with Contemplate. I throw the title in there, and the body text, and I add some quick directions to viewers like: click on the image to see it bigger....
  7. Then I do something like this:
    <?php if ($node->field_gallery[0]['view'] > '') : ?><?php print $node->field_gallery[0]['view'] ?><?php endif; ?>
    for as many images as I have. For the purpose of this site, I limit them to 24 images per page. I also have the option of adding text like this: <p><?php print $node->field_gallery[0]['title'] ?></p>within the statement if the desing calls for it.
  8. [update class="9.27.2008"] An alternative way to write step #7 in Contemplalte is so that you have more control over the size of the popup image in Thickbox. Writing it this way essentially eliminates step number 5, and gives you more control over the thumbnail and popup sizes in Imagecache.:
    <?php if ($node->field_gallery[0]['view'] > '') : ?><div class="gallery">
    <a href="/files/imagecache/Gallery_Display/<?php print $node->field_gallery[0]['filename'] ?>" title="<?php print $node->field_gallery[0]['title'] ?>" class="thickbox" rel="gallery-gallery"><img src="/files/imagecache/Gallery/<?php print $node->field_gallery[0]['filename'] ?>" alt="<?php print $node->field_gallery[0]['title'] ?>"  /></a>
    <p><?php print $node->field_gallery[0]['title'] ?></p>
    </div><?php endif; ?> [/update]
  9. All that's left then is styling the page in CSS.

After you have created your gallery page you may want to make a landing page which links to all your super cool galleries. Check this out.

  1. In Contemplate, create a teaser view that takes the first image and the page title. It might read something like this: <a href="/<?php print drupal_get_path_alias("node/$node->nid"); ?>" title="<?php print check_plain($node->title) ?>"><?php print $node->field_gallery[0]['view'] ?><p><?php print check_plain($node->title) ?></p></a>.
  2. Go to Views and create a block and select Teaser list. Select the node type in the filters and sort whatever way you want.
  3. Attach the block to a page and style with CSS. It really don't get easier then this.

I'm interested to find out if these steps were helpful. Please let me know in the comments below. Feel free to digg this page or quote it in the Drupal handbook.

Comments

Greetings team primal media. I'm not sure why no one has commented on this post yet, but it is fantastic. Have you guys/gals found a way to mass upload images using this method? This is the only roadblock we're facing when building drupal galleries without using the image import module. Thanks for tips and site looks great.
hi there, I'm using drupal 6 for a site and im trying to make a simple gallery exactly set up like the one that is in the example page. However, im a newbie when it comes to cck, views, and setting up content templates and I know its because of that reason. I am having a difficult time after direction 5. Is there a more detailed version of these directions available. I feel like im miss reading the steps 6-8 and making the updates and changes in the wrong places. Any help will be appreciated. I would really like to get this figured out because the example is exactly how I will be setting up about 5 different galleries on other sites so i really need to get it down. cheers, pboden

The first time I tried to figure out how to change the Display Fields settings in Drupal took me a couple hours because I couldn't figure out what the instructions were referring to. Since discovering this helpful shortcut I use it all the time when building templates.

To change the Display Fields settings, edit your content type which we call "Image Gallery" in the example above. At the top of the content type you will see the following selections: Edit, Manage fields, Display fields, Add field, Add group, Template.

Select the Display Fields option, and change your Gallery Full option to accommodate your Thickbox imagecache settings, and then the teaser should reflect your thumbnail imagecache setting.

The Display Fields is also a helpful place to keep the label from showing up above new fields.

I hope this clarified your question. I’m sorry that there are not more detailed instructions I can refer you to, because were the first to document this process. Best of luck.

you might want to check this link: http://drupal.org/node/251096 Let me know if you get it to work, I'm playing with it but no luck yet
I use Dimm's imagefield patch on a site and you can upload multiple images, up to however many you set in the imagefield settings. I used imagefield gallery rather than contemplate, and css. Using lightbox2 rather than thickbox. Here's the gallery listing: http://www.velocitynation.com/gallery Click through for sample galleries.
A noob here, I have been trying to get a photo gallery working for a week now. I just can't seem to get it to work. I like the this layout but just can't seem to locate some of the things mentioned here. It seems like I need more of a Dummies guide to this. I am not sure which part of the modules are turned on or even where to locate part this guide talks to. So would anyone be able to provide an more detailed guide? I am running Drupal 6 if that matters.
I haven't tried this with Drupal 6. These directions are written for Drupal 5.9.
Thanks! Nice straightforward way to achieve a gallery. I kept veering towards overly complicated solutions so i welcome this tutorial.
Hi, perfect tutorial, but steps 6 and 7 deserve more detailed description. I was not able to make gallery working (I do not have any experience with php) just pasting your code into template does not work. Could you please spare some more knowledge? Thanks:)
Hi! I can`t get the teaser view working- it`s not linking to gallery page, so maybe you could write more detailes on what shoud we leave and what to take out from existing code.
I have many images for a gallery, how do I create pager in content template's body field ?
Thank you for posting this information and sharing this drupal gallery solution. There seems to be no end to the things you can try to do when using Drupal - or the trouble you can cause yourself in trying to achieve the results you want! I think it helps if you have some programming skills and coding experience to be able to understand how to tweak and work with things. Unfortunately these things are not part of my skills. Thus a nice guide like this is so useful. I'm going to try this and hopefully it will solve my gallery problem, which I have been working on for a little while now.
Great Tutorial, I found that for multiple images, the value was in Data Settings (when one clicks configure in the manage fields section of the content type.)
Thanks for the tutorial. It gave me a good jumping-off point to start, and I really appreciate it. Here's a good replacement for line 7. This way the galleries are dynamic, and you don't have to manually enter lines for each photo:
    <?php foreach((array)$field_gallery as $imageview) print $imageview['view']; ?>
I used this as a basis to allow all my users to create their own (multiple) galleries, and each gallery has all the advantages of a regular node (comments, permissions, etc.). Thanks for such a good description and such a simple solution for great galleries.
Markle, Thank you for the dynamic code insertion (above), but how would that work if you wanted the title to print below the thumbnail?
Well, it's not horribly pretty with my in-node solution, but with a Views page I guess this would work pretty well:
    <?php foreach((array)$field_gallery as $imageview) { print $imageview['view']; ?> <?php print $imageview['data']['title']; } ?>
This is for Drupal 6. I don't know if 5 has enough of the same array structure for this to work, but I'm sure it's somewhere in the array if you can find the key for it.
Thanks for this tutorial! I have been trying to find a solution for node-based galleries but in vain... I think your tutorial might help me but I have a couple of questions: 1) The examples given show only a few images per gallery. What if the number of images in a gallery gets 100 or more? I can imagine the maintenance of 100 images on the imagefield edit page would become cumbersome. 2) If we have a large number of images, can they be displayed in pages using this method? Thanks!
I don't imagine that 100 images per page would be a problem. Go for it!
Themers Should Check This Out. Brilliant Work. You Did Well
Hi guys. This tutorial seems to be realy useful, but now I'm experiencing slight dificulties and don't know what to do. I have done everything up to point 6 correctly (at least hope), but with the point 7 I don't know exactly what to do.

I have used the code

<?php foreach((array)$field_gallery as $imageview) { print $imageview['view']; ?> <?php print $imageview['data']['title']; } ?>

by Markle and put it into the field "body template" in Contemplate settings of Gallery Content. Unfortunately no thumbnails are displayed on the page, inspide of the fact some HTML code is produced:

<div class="content"><a href="http://www.banhof.web44.net/sites/default/files/contrast_of_evening.jpg" title="" class="thickbox" rel="gallery-52"><img src="http://www.banhof.web44.net/sites/default/files/imagecache/imgch/contrast_of_evening.jpg" alt=""></a> <a href="http://www.banhof.web44.net/sites/default/files/the_light_scratch.jpg" title="" class="thickbox" rel="gallery-52"><img src="http://www.banhof.web44.net/sites/default/files/imagecache/imgch/the_light_scratch.jpg" alt=""></a> </div>

It looks like Image Cache doesn't work (if I put the link of thumbnail into browser's adress line, it doesn't work and the ImgCache directory is empty). Access rights to Image Cache directory seems to be alright. Can anybode help me and tell me what's wrong? Thanks a lot!

Note: point 8 I understand is optional, but I have tried it and it's even worse - no HTML is produced in that case, node seems to be totaly empty :-(
Hi What I need to do to create a separate category (folder) for pictures ? For example I want to put all pictures related to flowers in "flower" folder... Your working example show this thing but I don't know how to do... Thanks!

We're very happy to see this post has been useful to so many people. Many of the questions that are being asked are explained in detail in the steps above. This process really couldn't be simpler. Like the last person's question about how to create a seperate category -- read the instructions. 

For those of you who are not familiar with Contemplate, all you need to do is select the body or teaser Variables from the list provided. Click on the "Body Variables" to see the values that have been created based on the names you provided. There is no need to learn how to write PHP, it's just point and select the value you want.

Unfortunately, I don't have time to support this Photo Gallery, because it is so simple to create, and this blog is not a tutorial on how to use Contemplate, so if you need additional help learning to use Contemplate try Google instead of asking for a tutorial in the comments.

Ok, thanks for your reply! my last question: your documentation is compatible with drupal 6 ? Thanks!
Yes, this also works with Drupal 6.
Exist a way to do this as array ? <?php if ($node->field_gallery[0]['view'] > '') : ?><?php print $node->field_gallery[0]['view'] ?><?php endif; ?> I need to do the same thing for all photo... is pretty hard for a person who use the album and don't really know drupal.
How have you dealt with ampersands in the image filenames, i have tried this out and its great but any image with an ampersand in does not display. Help Help Help!
well can someone tell me how to make them appear like a grid like on this site mine appear verticale one top another
THANK YOU! I spent forever trying to get it to work with 2 content types - Galleries and Images, and relating the two. Your way is SO much simpler and more manageable.
Hi, I was working on something similar and found your site. It was very helpful and I really appreciate your post!
Great notes...! I am just not sure where you add the code on pt 8? Does it go on the Body of the Image tempalate?
Thanks for sharing. I just thought I'd add a comment to make the code you're using a bit more efficient: instead of using:
    <?php if ($node->field_gallery[0]['view'] > '') : ?><?php print $node->field_gallery[0]['view'] ?><?php endif; ?>
      And then repeating for each image you want to show. Try:
        <? for ($i=0;$ifield_gallery);$i++) { if ($node->field_gallery[$i]['view']) echo $node->field_gallery[$i]['view']; } ?>
          this will do exactly the same thing as your code, only you just have to put it in once and it will automatically display all of your images, rather than you having to put a new statement for each one. It should speed up your page a bit too!
          A million thanks for the above code!!!
          Hey um this looks awesome except that I have no idea where to put the #7 and #8 code and the update that pologringo posted. Any assist is appreciated thanks....... looks real cool. ALso is this for 5 or 6 drupal versions.
          Hey, thanks for the great walkthrough! I've setup my gallery with some troubles but it works! I think I have only one problem left: Anonymous users cannot see the images ! What could be the solution, please help?
          @dolugen your problem is permissions. I ran into the same problem on a Drupal 6 site recently, and you have to enable permissions for Anonymous to see the image type.

          To get dynamic gallery building, try this for your gallery builds with Title Text under image.

          Notice: I named my field "imge_gallery" so yours might be different.

          field_imgegallery); $i++)

          { print $node->field_imgegallery[$i]['view']; print "

          "; print $node->field_imgegallery[$i]['data']['title']; print "

          "; } ?>
          Thank you so much. I was making a gallery website with drupal, and this code above help me a lot. Once again a million thanks
          Thank you! I've searched all day for something like this, I've tried everything, every tutorial..once again, thank you :)
          After days of searching this was the best solution!
          Great job. Simple to implement and powerful. What I like in particular is that I can swap out thickbox with other solutions as I come across them. Keep up the good work.
          This is brilliant. I've been trying to get image galleries working in Drupal for ages and it all seems so convoluted and needlessly complex - you've nailed it here, thank you so much for such a solution!
          Hi, Thanks for the tutorial, it makes a lot more sense than some I've seen! However, like some others I seem to come unstuck around instruction 7. I think I've done everything corectly, but when I go to view my gallery, I get a page full of my large uploaded images, rather than the thumbnail versions. Please, has anyone any ideas? I'm a Drupal newbie - I thought to familiarise myself, I'd build a client website in it. However they require a photo gallery that they can upload to themselves. If I can't make this work then I'll have to abandon Drupal and build his site in my normal way, using a bespoke CMS. Can anyone help? Thanks, Willo
          Very helpful! Thank you for writing this up
          Hi, this is brilliant work. Very helpfull and detail. I will try this for my photo gallery project. Great thanks for sharing this.
          Thank you once again. I have some trouble with the teaser list for all galleries: I think step 1 suggests to write the code given there in the contemplate teaser text area for the "Image Gallery" content type. My problem: It seems that this field doesn't affect the teaser output. In the teaser list (view), only the text from the body is displayed. Am I missing something?
          I had been using views & cck - this is much much better, and easier for a client to add new galleries! Good work!
          Thanks for writing this down. I just have one question. At step 9 you mention CSS, which css files should I edit? Greetz and thanks again.

          Add a comment

          This field will not be shown publicly.
          • Web page addresses and e-mail addresses turn into links automatically.
          • Lines and paragraphs break automatically.
          • Allowed HTML tags: <a> <em> <strong><ul> <ol> <li>
          blog