Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of Drupal's greatest weakness is its lack of native support for graphics. If you watch the Drupal Essential Training Series from lynda.com, you might remember the procedure we use to get them into a Drupal site. It was pretty messy. You had to upload the file, figure out where it was stored, hand write HTML to place it in the content and finally change the setting just to get the things actually show up. So let's look at a better way. Drupal's poor handling of images in its core package created a vacuum that was filled with several solutions in the form of downloadable Modules. If you browse through them on drupal.org, it's easy to get confused by their shear number and the fact they all appear to do exactly the same thing. We are going to look at just one of these Modules called ImageCache.
I like it because in addition to letting you upload and place graphics fairly easily, it can be set to automatically process them in interesting ways that become especially useful for putting in the context of a data driven site. It's in wide use on Drupal sites throughout the Internet despite the fact that running it effectively requires you to download and install four separate Module packages. They are all included in Acquia Drupal by the way, which you can download at acquia.com. Setting up ImageCache and all its supporting parts is a hassle but believe me, it will be worth it once we start setting up views later in the course. You will need to install four Modules. ImageAPI, which is a behind the scenes module with no interface of its own. FileField, which lets you upload files into CCK fields. ImageField, which extends FileField and is specific to images.
And ImageCache itself, which does all the fancy image processing. We have already downloaded and uncompressed them and dragged them into the Modules folder of our Drupal installation. If you need help with this step, see the video expanding your site's capabilities with modules in the lynda.com Drupal Essential Training Course. To turn on these Modules as always you go to Administer, Site building, and Modules. On the Modules page, you will need to turn on Modules in two different groups. There is a new group called ImageCache and there are also some new Modules turned on in the CCK group at the top of the screen. I'll show you those right now.
Scroll down a bit and you see FileField. We'll turn that on and also ImageField. Scroll down a little bit further, and we come to the ImageCache group. I'm going to turn on all of these except for ImageMagick. I generally don't use it because it's a command line tool. That's a little bit beyond what I need. However, you may want to look into it, and of course documentation as always is on drupal.org. But for us, we'll just turn on these four in the ImageCache group, scroll to the bottom, and Save configuration. There is one side note I want to mention here. The ImageField Module is incompatible with the other popular Module that's called simply image. If you have downloaded and installed that one, weed (ph) through ImageField's documentation to understand the conflict and how to get out of it. Now we are basically all set up, we only have two more steps to go. First, to add the ImageField to our Person Content type and secondly, to give ImageCache instructions that will let to do it's magic.
We are going to start by going to our Person Content type, once again that's Administer, Content management, Content types, and I'm going to add fields to my Person Content type. Specifically, I'm going to add a headshot so we can see the faces of all the people in our database. As before we add a new field by going down to this New field form and filling it in. I call it Headshot. The field will be called headshot. And under file type we now have a different selection, File. We select that and select Image. We then click Save to go on to the next screen, which lets us set our attributes for this headshot field.
As with Numbers there are specific options that you can set for this kind of CCK field type. For example, you could decide you don't want to allow people to upload GIFs by deleting this extension here, or you want them to be able to upload a different type of image file. You can specify a Minimum resolution or a Maximum resolution, so people don't upload anything too big or too small. Here is one change we are actually going to make which is to change the path. I'm going to create a folder inside our files directory called headshots, just so that we can keep them all together in one place instead of having all of our files end up loose in that files directory.
If you have installed token as we did in an earlier video, you can see that you can actually create folders that are specific to the file. So that if for example the user name Cindy uploads files, they will be put in a folder labeled Cindy and so forth. Scroll down. We'll make one or two other very small changes. In File size restrictions, I recommend that you do put a Maximum upload file size there because otherwise people might upload these enormous files which will crowd up your server. I'm just going to say you can't upload anything more than 5 megabytes. We'll scroll to the bottom, and Save.
As always I recommend that you take a look at the other options there, so you can understand what's available when you create an ImageField type. I'm going to rearrange this a little bit so it looks better when people are filling out the field. And Save it. We have now added a Headshot field to our Content type called Person. Now let's go back and edit a node of that Content type to see how this works. We are not actually done and in fact there are two important things we have to do to make the image appear the way we wanted but that will be clear after we create this node. We'll go back to the home page and will look at this Tom record, and we'll edit it. And here we have a place we can put that Headshot, if we click Browse, we could then go and find it. The one we'll use is called tom-the-scream. Open it and click Upload. And after a moment there it is. It's been put into our Drupal installation. However, you still have to save in order to make sure that it's in the node.
Now that we have done that and uploaded the Headshot, you can see one of the problems, it's much too big. This is where the ImageCache Module comes in. We are going to set some specific rules for ImageCache so when someone uploads a file, it not only makes sure that it's not above a certain size but it forces it down to whatever size we want it to be. To do that, we'll scroll back over. Go to Administer, Build, and ImageCache. The way ImageCache works is you set up what are called Presets. In a Preset you can say how large the file can be and a few other things as you will see in a moment. I think it's easier to show than to tell, so will add a new preset and I'm going to call this one, 100x100. As you can see, you are limited to using only alphanumeric characters, underscores, and hyphens as in many places throughout Drupal. Create that new preset and then I can set whatever actions I want. In this case I'm going to scale it down to a certain size, so I click on Add scale. As you can see there are many other options, for example, you can rotate each image that somebody uploads but I'm just going to make it scale.
I will make the width only 100 and the height only 100. And I'll allow upscaling. What that means is if someone uploads an image that's only 50x50 pixels, it will make it larger so it's 100x100. It will look pixelated but it will at least be the same size as all the other images. I'll add the action, good, and it shows us a preview here. If I wanted to I could add other action, so that after scaling it would rotate, it could sharpen and so forth. But I'll just say update preset, make sure everything is good. Good, my preset is saved.
I am going to go back to ImageCache and create one more because I'm going to use that later in this series. Add new preset, 200x200 and you can guess what this is going to be. I'll scale it to 200x200 and add action. Very good. And you can see now that what's it going to look like. Now let's go back and take another look at that node. We'll go back to the home page and click on Tom de Nada. Yes, still too big. But we can change that by going into Content type.
We click on Administer, Content management and Content types. And once again we click manage fields, this time we go to Display fields. Next to Headshot, as with the other fields you have several options. You can change it so it's not only the raw image but the image that you just created using ImageCache or rather that ImageCache automatically created for you. I'm going to change that so it's not 100x100 image and when you click on it, it goes to the node. And I'll do that both for the teaser and the full node. And Save it.
Now let's go back to our home page and see what that looks like. Much better. Finally, I'm going to do just a little bit of clean up, I'll go back to our Content type, go back to Display fields and I'm going to take away that thing that says Headshot. Obviously, it's a Headshot and Save it. Let's take one more look at our page. Looks good in the teaser, looks good in the node itself. Terrific. That was a pretty complicated process, so let's review. There are five steps to adding images using ImageCache. The first is to install and enable all of the Modules that you will need. The second is to add an ImageField to your Content type. In our case it was the Person Content type. The third is to add ImageCache preset so that you will end up processing the files that people upload to be exactly what you want. The fourth is as always change the way it appears on the page by changing the Display fields settings within your Content type. And finally, start uploading files and see how it goes.
So you might ask, what exactly does that get us? In one way, entering images this way is less flexible than if you just wrote HTML into the node. For example, you can't put the image in the middle of a block of text or make it flush left. But for a data driven site, you have to do it this way because a image becomes a separate data object that you will be able to play around with later. And ImageCache makes it a highly flexible data object by letting you specific how the graphic will appear in different contexts. We have only scratched the surface here. A search for the word ImageCache on the drupal.org website leads you to all sorts of neat effects that you can make it apply, such as reflection, recoloring, and so on. In short, a mastery of ImageCache can make your graphics look as good as the rest of your site.
Get unlimited access to all courses for just $25/month.Become a member