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.
Hello, This is nice place you have here, I just want to congratulate you. I wish we may have more places like this. All the Best , S.James
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 :-(

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