A while ago, Angela Byron of Lullabot wrote an article comparing Image and Image Exact Sizes with CCK Imagefield and Imagcache.
The examples given in the article are clear, but if you are using views, you will need to do some more stuff to get what you want.
What my client wanted was the ability to upload a picture of any size, and have a preset size of it appear in one place, and another in another place.
These business requirements translate to two different views that end up as blocks on the front page, using the same content type.
Here are the steps necessary to do this:
First, create the views with the CCK imagefield field in it. For this particular case, we needed the title, author, timestamp, text and tags.
Follow the instructions in Angela's article on how to create presets. In my case, I needed two of them, both of type scale, one for 122 pixel width, and the other for 68 pixel width. By the way, imagecache refuses to work if you give it only a width, so enter something in the height as well.
Then use the views theme wizard, select the view, and click Generate Theme. This will give you two snippets per view. One goes in the template.php for the theme, the other goes in a file called views-list-something.tpl.php.
Now, you need to change the image field in the view from something like:
<div class="view-field view-data-field_image_fid">
<?php print $field_image_fid?>
</div>
To this:
<div class="view-field view-data-field_image_fid">
<?php $file = _imagefield_file_load($node->node_data_field_image_field_image_fid); ?>
<?php $img = theme('imagecache', 'width122', $file['filepath']); ?>
<?php print l($img, "node/$node->nid", null, null, null, false, true); ?>
</div>
The key here is the obscure field $node->node_data_field_image_field_image_fid which was impossible to guess, but with some print_r()s it was revealed to be the one to use. This field holds the file ID of the image, and can be used for the imagecache retrieval.
Now, for the other view, repeat the steps above, with a different preset to get a different image size for the other view.
When the views are placed in blocks, they will show different sized scaled image, and the original remains the same.
Enjoy ...
Comments
Visitor (not verified)
Thanks!
Thu, 2009/05/14 - 14:37Awesome. Exactly what I was looking for. Saved a lot of time trying to figure this one out. It would be convenient if Views could recognize thumbnails associated with CCK image field types so that this kind of code-foolery could be avoided.
Visitor (not verified)
Does this work on field.tpl in D6 / Views2?
Wed, 2009/10/14 - 09:42I am having issues getting this to work.
SETUP:
Drupal 6.14
Views 6.x-2.6
CCK 6.x-2.5
FileField 6.x-3.1
ImageField 6.x-3.1
ImageCache 6.x-2.0-beta10
SCENARIO:
I want to print an imagecached preset image to a field.tpl file:
Theming a 'page-display' 'grid-view' field.
I am theming a views field tpl called:
views-view-field-myfieldname-fid.tpl.php
I can print a bunch of other fields on the tpl without issues but images don't work for some reason (?)
When I try the above code (modified with my field names and imagecache preset names), I get an error message saying:
"Fatal error: Call to undefined function _imagefield_file_load() in /home/users/..... on line 10"
which coresponds to the first line of:
<?php $file = _imagefield_file_load($node->node_data_field_image_field_image_fid); ?>
<?php $img = theme('imagecache', 'width122', $file['filepath']); ?>
<?php print l($img, "node/$node->nid", null, null, null, false, true); ?>
Is there something I need to put in template.php for this to work?
Does this code work for views2?
Is something escaping me?
Any help much appreciated.
Thanks!
Visitor (not verified)
Solved
Wed, 2009/10/14 - 21:13solved:
In Drupal 6 this:
_imagefield_file_load
should be changed to:
field_file_load
My final output (slightly modified) is
big thx to gogasan for the enlightenment!
Visitor (not verified)
This solution didn't worked
Mon, 2010/03/29 - 03:58This solution didn't worked for my view. I came up with this solution for Drupal 6:
It's working for me.
This article hepled me a lot. Thanks.
http://daipratt.co.uk/drupal-getting-the-image-path-of-a-cck-field/
Ericka (not verified)
Works!!
Mon, 2012/10/22 - 02:07Works for me!
Thanks so much.
Rob Sullivan (not verified)
Images and Sizes
Thu, 2014/03/13 - 13:46Finally thank you so much for posting this I have been searching high and low for this information. I wish there was a way to get the exact search you want on google, sorry off topic. The code up there is perfect. Thank you Angela Byron of Lullabot and 2bits
Pages