Darren Mothersele

London based Drupal Web Developer
Facebook Twitter Drupal.org LinkedIn RSS Feed

Image Resizing with CCK, Imagefields, Imagecache and PHPTemplate

If you have a CCK content type with an image field you get display options that allow you to change the image size on teasers and full node views. First you set up your image presets using the imagecache settings. I usually let users upload at any size, and deal with the resizing as required. Here's how to ensure you get best results from imagecache, and how to embed imagefield images into your PHPtemplates using the theme functions provided by imagecache:

In the imagecache options set up presets for each size you are going to need. You get several options for resizing. You want to use scale so the images are not distorted, and use outside dimensions so that the image is at least as big as your required thumbnail. Then use clip to remove any image outside of your thumbnail area. This means that your thumbnails are always the same size. I prefer this to using inside for the scale options, as that causes the images to come out at various different sizes. I like thumbnails to all be the same size, and cropping is usually acceptable.

I use the scale inside option to reduce pictures for screen size display. This works well for images used in full node templates as I'm not as fussy about making them all the same size.

Now you've got a CCK content type with an image field, and you have imagecache setup to give you the images at the size you need. Now you need to display them in your template.

The CCK field display options gives you the opportunity to select a size for the image as displayed in either the teaser or node view, but in your template you might want more control than this. That's when you need to turn to the theme functions provided by imagecache. In this code snippit I render a thumbnail from a $node object with an imagefield called event_photo:

<?php

print theme('imagecache', 'thumbnail', $node->field_event_photo[0]['filename'])
?>

In your own template substitute thumbnail with the imagecache preset name as required, and field_event_photo with your CCK field name.

You might want to link the image to something. Here's how you do this and render the correct Drupal path (it may have been aliased by an administrator):

<?php

print drupal_get_path_alias('node/'.$node->nid)
?>

Put that in an <a> tag around your themed image display code from before.

Update

I recommend upgrading to the latest version of Imagecache - The interface is much improved and "crop and resize" are now available as a combined option.

imagecache

Am I changing all the instances of $node->field_image_cache[0]['filepath'] to $node->field_image_cache[0]/files/imagecache/YOUR_PRESET_NAME/['filepath']? in the entire module?

preprocess alt attribute from imagecache in views

Hi there - maybe you can help... I am trying to output two versions of the same image file from a view. One with the alt attribute "side" and the other with the alt attribute "thumbnail" - but for the life of my can't figure out how to do it...

This is what I've got so far...

function mytheme_preprocess_views_view_fields__panelsgallery__page_1(&$vars) {

$fields = $vars['fields'];

$slideurl = $fields['field_upload_image_fid']->content;

$vars['slide'] = theme('imagecache', ‘gallery’, $slideurl, 'side', $title, $attributes);
$vars['slide_thumb'] = theme('imagecache', ‘gallery’, $slideurl, 'thumbnail', $title, $attributes);

}

Any ideas?

argument order?

The theme_imagecache() function looks like this:

<?php
theme_imagecache
($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE)
?>

Which means your call to theme('imagecache'... should look more like this:

<?php
$vars
['slide'] = theme('imagecache', 'slide', $slideurl, 'slide', $title, $attributes);

$vars['slide_thumb'] = theme('imagecache', 'slide_thumb', $slideurl, 'thumbnail', $title, $attributes);
?>

I've assumed your presets are called 'slide' and 'slide_thumb' but swap that for whatever you are using.

If that's not what you're asking, try putting this in your template file to check what vars are being created:

<?php
print_r
(get_defined_vars());
?>

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: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Recent comments

Latest Posts