Using Imagecache and CCK Imagefield to Add an Image in Drupal

Figuring out how to get Drupal's imagecache module to work with CCK's imagefield can about as fun as a trip to the dentist. Although I found a lot of articles analyzing the benefits of using Image vs. CCK imagefield, I could not find one source with clear instructions on how to implement imagefield with imagecache. So, here's my stab at an explanation of how to get imagecache to work with imagefield.

1. Install and activate CCK, imagefield, and imagecache. Go to Admin > Content types and select the content type that you want to add an image field to. Click on "Add field" to add an image field. Then, give your field a title, select Image and create your field. The name of my field is "related_images."

create cck image field

2. Set your imagecache presets at Site Building > Image cache. My preset is has the namespace "homepage" and is set to resize with a width and height of 100.

drupal preset namespace

3. Go back to the content type that you added a CCK image to by selecting Content Management > Content types. Click on the name of the content type you previously added an image to. In the content type's menu, select "Display fields."

display cck image fields

4. Look to the right of the listing for the image field you created. Set the teaser and body to the namespace you set in step 2. My namespace is "homepage."

set display of cck image

5. Add a new article of content using the content type that has your new image field. Upload an image as part of the post and make sure that your image displays at the width and height you set in "Display fields."

6. If you want to have your image display in a template such as node.tpl.php, the following code will print the image and image title for a CCK image field called field_related_images:

  1. <?php print $node->field_related_images[0]['view']; ?>
  2. <?php print $node->field_related_images[0]['title']; ?>

When you enable imagecache,

When you enable imagecache, which is great, you do not need to associate an imagecache image scale/crop/size thingie with a particular CCK field or CCK content type. You simply have to call for the image you want, using the imagecache name for the crop/scale/size preset thingie (that's the proper technical term) and the file path to the original image (created by imagefield). You do that with code like this: print theme('imagecache', 'files/imagecache/your_imagecache_preset', $field_example_image[0]['filepath']); (filepath is literally what you use, that's not an example, it gets the original filepath for you). If I remembered that wrong the right code is in a comment on a Lullabot thread about this.

Excellent explanation. Thank

Excellent explanation. Thank you.

Display Fields has such limited use when everything is running fine that you sometimes forget to even check there!

thanks

You saved my life.
Thanks a lot.

Exactly what I was looking

Exactly what I was looking for, I didn't think to look in the display fields bit! thanks a lot.

imagecache not working linux

hi,

Using Drupal 5 and Imagecache.I have problem in linux for imagecache process, but working fine with windows machine.

Regards,

Thiyagarajan Veluchamy

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.

More information about formatting options