1

I have a node type where I'd like to display images as both thumbnails and full size images, so I'd like to use different image cache settings on the two image types. I don't want to use views to do this. I know that I can output a field twice using "print render($content['field_name']); " in my node.tpl file, however I wouldn't be able to manage the display separately on the two outputs.

Does anyone have a solution?

1

You can get the image path by using following code:

<?php
var_dump($node);
?>

Now find the path of the image and write the following code:

<?php
theme('image_style', array('style_name' => 'stylename', 'path' => 'image path', 'alt' => 'image alt', 'title' => 'image title', 'width' => 'some width', 'height' => 'some height')));
?>

Where you can define your own style name. I hope you know how to create image styles. Just Goto admin/config/media/image-styles and add your style and use it in above code. Use above code twice to print images in two different style.

Entire code:

// filename relative to files directory
// e.g. 'masthead.jpg'
$filename = $node->field_image['und'][0]['filename'];

// relative path to raw image in 'scheme' format
// e.g. 'public://masthead.jpg'
$image_uri = file_build_uri($filename);

// relative path to 'styled' image (using an arbitrary 'banner' style) in 'scheme' format
// e.g. 'public://styles/banner/public/masthead.jpg'
image_style_path('banner', $filename);


// html for a styled image
// e.g. '<img typeof="foaf:Image" src="http://mysite.com/sites/default/files/styles/banner/public/masthead.jpg" alt="" />'
print  theme('image_style', array('style_name' => 'banner', 'path' => $image_uri));
  • I forgot to mention each node will have multiple images in the image field. If I add this code to my node.tpl.php file, will: "$filename = $node->field_image['und'][0]['filename']" get all the individual images in the image field and output a value for each or do I need to put it inside some sort of foreach loop? – JamiePatt Oct 4 '12 at 5:26
  • @InfluenceJamie: You can create a separate tpl for each node type like node--nodetype.tpl.php – mohit_rocks Oct 4 '12 at 5:55
  • Yeah, that's not what I was asking, I know that I can have a separate template like node--news_articles.tpl.php and node--blog_posts.tpl.php. What I'm asking is, I will have multiple images in a single node as in the node type might be houses, and for each house there are multiple images in the image field. Do I need to put the above statements into a foreach loop in order to have each image printed twice with the desired image cache? – JamiePatt Oct 4 '12 at 10:27
  • I think in that case you have to use foreach() loop only. And display print both the image cache or which ever you want – mohit_rocks Oct 4 '12 at 11:49
  • I'm having trouble wrapping this statement in a foreach loop. If I use your code as is, it outputs the first image in my image field. However, if I try and wrap the statement in a foreach loop in order to output every image in the image_field I get an error. Any ideas on how the statement could be best wrapped in a foreach loop in order to achieve what I'm trying to achieve. – JamiePatt Oct 5 '12 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.