A simple Ajax image gallery system for drupal
Few days ago, I was chatting with EclipseGC about his imagefield gallery module, and the future features that will be implemented for the release of the drupal 6 version. Of course, you can't have two drupal addict like us to talk for more than 2 minutes without having Views 2 mentioned at one point. But this time, Kris described a technique to create an ajax image gallery system using views in a quite simple and awesome way. I think he got this trick from Quicksketch, that himself must have got it directly from god, as it's so simple it's beautiful.

I felt really dumb that I never thought about it before, and now it's your turn to feel dumb... here's it how it works:
For this gallery system, you need :
- Drupal 6
- Views 2
- CCK
- Imagefield
- Imagecache (optional... but is imagecache optional, really?) which also needs the ImageAPI module
The concept is the following: having a content type with a multiple value imagefield, that are hidden in the node, and listed in a view block that you can display wherever you want.
CCK
So first, of course, install all the required modules. Once they are all installed and activated, go to /admin/content/types and create a new content type that you can call "gallery" or "album"...or whatever you want really!

Then in 'manage field', add a new field with the type 'image'. This image field should have a number of values set to either 'unlimited' or to at least '2'... obviously... Most of the other settings are up to you. It would make sense to have this field required, but I seam to have missed this when I did this screenshot...

Then, in the 'Display fields' tab, make sure that you set the 'full node' view as <hidden>. We're hiding this field by default because we will be outputting the images in a view block that we will place right beside the node.

Once you've done this, your content type is pretty much ready to go, we now need to create the magic View. Of course, by using a custom content type, you open your gallery to all the flexibility that the CCK module offer!
ImageCache
Imageca che will make sure that your images are resized to the right sizes for the gallery. You have to create two imagecache profile: one to resize the images when viewing them in the gallery, and one to create the gallery thumbnail. First go to /admin/build/imagecache, and click on 'add a new preset'. Pick a name and then add the action to perform. For the viewing profile, I chose the "Scale" action set to a width of 600px, a height of 300px, and optionnaly you can allow upscaling.

For the thumbnail of the gallery, I chose the "Scale and Crop" action, set to 200px by 200px. Of course, you can adapt this settings to your needs, but you need at least these two profiles before mooving on to the view settings.
Views
You now need to create a new view using the Views 2 module. Go to /admin/build/views/add, choose a name and description for your view, and make sure you select 'Node' for the view type. Then click 'next' to set up the view. The important basic settings that you need to have are the followings:
- Style: Unformatted (just to make sure the output is simple)
- Row style: Fields
- Use Ajax: Hell Yeah!
- Use pager: Mini (or full, but you need a pager)
- Items per page: 1
So it should look like this:

Then you need to select one field to output, so in the filter section, click on the '+' icon, then select the 'content' group. You should see the image field that you created for the gallery content type listed there. Select it, and then click on 'Add'. I usually hide the label of the field. Make sure you select the right imagecache profile to output the image. It should be the 'viewing' profile you set earlier. Also make sure that the image is not as a link, but just as a plain image.

You will also need to Set some filters, so in the "filters" section, click on the + link, and in the node group, add Node: published (set as YES), and Node: Type (set as your gallery content type).

Once you're done, there is a last thing you need to do, it is to set an ARGUMENT. As the view is not the node, it doesn't know which node you'll be viewing. So basically, the idea is to ask the view to check which node we are viewing by looking at the URL of the page. For example, if we are viewing the node number 198, the URL should be like this: /node/198 . If the view knows which node we are viewing, it also knows what images have been attached to the node. The beauty of this is that it works even if you use the Pathauto module, as the view knows if the URL is an alias of the node or not.
So click on the '+' link of the argument section, and select the Node: Nid (node ID) argument. Thanks to the hard work off Merlinofchaos, arguments are now very easy to set up. You are going to tell the view to find the node id in the URL, and that means that you are going to provide the default argument, so select "provide the default argument". Once you selected this, you should see a new list appear. In this list, select the value "Node ID from URL". Once this is done, you only need to check the node type you want to act on, so if your content type is called 'gallery', check the box in front of 'gallery'.

That should be the last default setting you need to set, and you now need to add a 'block' display. The block should get all the default settings you already set. Save the view.
Block and region
You now have a gallery content type, and a block view to display the images, but you also need to place the view block in the appropriate region of the page. I created a region right on top of the content of the page, and dropped the block in it. If you don't know how to create a custom region, read this: http://drupal.org/node/292028#comment-1027837

Test drive
Now you just need to create a new gallery, and when viewing the full node, the view block should now show each image one by one, in the dimensions set through imagecache, and with a nice little ajax pager to browse them. You can now enjoy all the Views 2 theming possibilities to theme your gallery.

A gallery page.
If you need a page that lists all the galleries, you just need to create a page display in the view, and make sure that you override the view default settings, so the page display does not have any argument, and that it lists more than one item of course :) . Maybe you might also prefer to use a 'grid' style to display the albums.
Embedding the view in the node template
Another nice little trick is to actually embed the view in the node template. This way you don't have to create a region just for the gallery system, and it is so simple that it would be a shame not to do it this way! The only thing you need to do is to edit the node template of your theme, and paste a little snippet of code that will tell drupal which view and which display to embed. The code is like this :
<?php print views_embed_view('view_name', $display_id = 'display_id');?>
Just replace the view_name by the name of your view (the name of your view is shown in italic characters in the title of the page when you edit it), and replace display_id by the id of your display. For example, if your display is the first block of the view, its ID is block_1 .
So for my view, this is the snippet I had to paste in the node.tpl.php file of my theme :
<?php print views_embed_view('gallery', $display_id = 'block_1');?>
There you go, you now have an easily themable ajax gallery system on your site ! I wouldn't say it is the perfect solution as I would prefer each image to be nodes, but this would require some module development, and that's not an option when you are a simple designer like me!
- Login to post comments













Broken link
Heads up, broken link ("imagefield gallery ") in the leader text; thought you might like to know. Feel free to moderate/delete the comment when fixed.
thanks, fixed :)
thanks, fixed :)
thumbnails
Just what I was looking for!
any advice on adding thumbnails below the images?
would like to show image with thumbnails below.
Try making an attachment in
Try making an attachment in your view. In the view you ceated for this, add a new 'attachment' display that'll display all the images as tiny thumnails. Then, make sure you make this display attached to the block.
Only downside, you can' make the images clickable I think...
did not work
this did not work
with a default 6.9 Drupal install and just the modules you listed installed
untill I made sure I had "Group multiple values" unchecked when editing fields.
still cant display imagecached images in pager, only default images...strange
I don't find where I can
I don't find where I can set the Full Node to hidden.
Very cool. And very neat.
Very cool. And very neat. It's very easy to setup and perfect if you have users that need to create their own image galleries and you'd rather not have them ftp'ing images to a folder on the server or creating a dozen or more nodes.
As I'm using this technique, the only thing I see it needs is to cycle through the images while in Lightbox mode (if you use LIghtbox with the image field and appropriate imagecache setting.) I can click an image and it displays in Lightbox but no nav arrows for previous/next images.
Thanks again!
imagefield title
Nice & simple...but
how can u print imagefield title (image description) in views2 along with eatch image
($item)? is there a way through views tpl theme files? Do u have an idea? views do not reveal imagefield extra info fields like ordinary cck fields.
I guess the real place to
I guess the real place to ask this is either the views or imagfield issue page:
http://drupal.org/project/issues/views?states=all
http://drupal.org/project/issues/imagefield?states=all
Page View: seperate mini pager for each gallery
Hi, Thanks for this great howto. I like the idea of having several Image galleries on a single page with a mini-pager for each gallery. This allows users to view thumb images likea slide show.
However this type of view only works if items per page =1. If I create multiple galleries they all appear as one slide show.(obviously). How can I show image galleries separately each with its own mini pager.
Here is what I have now http://stanislites.net/beia/gallery
Thanks, and about those image nodes . . .
Hello there,
First off, thanks for sharing your knowledge with the rest of the community - ever since I got into web design, (not that long ago) I haven't ceased to be amazed by how willing most people are to share their knowledge. I found the bit on embedding views into node templates helpful - I'll be able to make good use of it.
Also, you mentioned near the end of the article that you would prefer each image to be a node. If I'm not mistaken, that is what this ( http://drupal.org/node/144725 ) method of gallery setup accomplishes - I've been working with it, and while to my mind it still leaves some things to be desired, I do believe the images come out as nodes.
You will notice quickly that the method laid out is for Drupal 5, but I've set it up in Drupal 6 with little extra effort.
Regards,
-B
http://drupal.org/node/14472
http://drupal.org/node/144725 works well where images are nodes. The tutorial above has galleries as nodes with multiple images within a gallery. A user creates a gallery that he may choose to Tag.
My use case is
1. Have multiple galleries per page ( (say) 4 rows x 5 col - each thumbnail 100 x 100 pix )
2. Each gallery displays one picture with a mini pager for each gallery
3. A user can click through the thumbnails of a specific gallery using the mini pager
3. On clicking any image of a gallery a larger image is displayed (say 640x 480)
4. The description of each image should appear under the image
How can I do this with views
Thanks
And with list format?
Great article!
Curious if anyone is having problems setting the style of the view to 'HTML list'. Set to 'HTML list' and with the argument as described in the article, all images are grouped together in one list item. With the argument removed, each image correctly is in its own list item.
Any thoughts on how to correct this?
Cheers,
SWW
Naturally..
.. having spent three hours on this problem, I post here and then immediately solve the issue.
One needs to make sure "Group multiple values" is unchecked in the fields!
Pager Issue
The tutorial is great, however in my experience, when the node id argument is added, on the pager it displays the total number of images that are available to the node not the number of images that are actually uploaded on the node. In other words if you allow 6 images to be uploaded on the node but only upload 4, the pager will still display '1 of 6 ' rather than '1 of 4'. If anyone else has had this issue or better yet a solution for this issue I would greatly appreciate it.
showing next page on a node?
OK, I dont know if this is similar but you decide. If it is not, please kick me out of here immediately :)
Here is the issue:
For example I have a content type called "News". And when I view a News node, I want to show the previous and next News items (by post date) on a block in that node. Like a book page as you know...
It sounds very simple to do this using views and an argument, but I cant seem to find a way. Any ideas?
Custom Pager
A great module exists for that : http://drupal.org/project/custom_pagers
Thanks... This is exactly
Thanks... This is exactly what I was looking for. I didnt try it yet but it seems really fantastic module...
Cheers,
Sinan
Cannot Create gallery
I followed the howto to the letter on a site using drupal 6.10, but I can't seem to create a gallery. It comes up in the "create content" menu and gives me an entry page, but the page itself only has text fields and no image upload fields, much less multiple. The only thing I can see that was at all different is that there was no "image" option for the gallery's field, but two boxes. When the first was set to "file" and the second to "image" it gave me the same options present in your howto. However, like I said, when I go to "create" a gallery there is no place to upload any images.
Any ideas?
Add imagefupload module for enhancement
Have you tried the Image FUpload module at http://drupal.org/project/image_fupload to be able to upload multiple images using Flash into a single cck image field?
Image FUpload works at expected, but when I follow this tutorial I can't seem to get a pager to appear in my block view. (strange) It only shows the first image that was uploaded.
Anyways, great tutorial. Thanks for write up. Looking forward to any other how-to you got.
Getting it to work
For anybody that is having trouble getting this to work please see the following issue.
http://drupal.org/node/366419
The problem that can happen is that the pager fails to show.
Hope that helps someone.
what?
I'm totally lost, can't get it to work... :(
Found something maybe useful
After having problem displaying the bloc, I've checked "Validate user as acces to the node" and it worked.
Thanks for the tip
How does this all fit in the Database
Hi,
Love this solution. Elegant, great. But I would like to use it to connect to a web app. So I'm trying to figure out how this all gets stored in the database...but it's a bit unclear. Do you have any idea how the inner workings of Drupal get all these images to show ? Where does he find this info ? The "files" table stores the images. But how do we know which images are part of which gallery :-) ?
Thx for this solution.
Cheers
Ok found it
Ok, I found it :-). I'll put it here in case anyone else is interested.
The "node" table contains the gallery (obviously). Its NodeID is referenced in "content_field_image", which associates this NodeID with FileIDs, from the "files" table. That's how you can retreive images for a specific gallery outside of Drupal.
Don't you just love Drupal :-) ?
Cheers