Hi
I'm working on Drupal 7 with a custom content type from node--content_type.tpl.php. I try to output an image from an image field with a custom code. It's sad that I only know how to output the original image from field/image directory. This is the code I used:
<?php global $base_url; echo "<img src='".$base_url."/sites/default/files/field/image/".$node->field_image['und'][0]['filename']."'>"; ?>
What I want to do here is output the thumbnail image of that original image. For example the thumbnail image from this directory: sites/default/files/styles/medium/public/field/image
Could it be possible? Sorry for my lack of PHP and Drupal knowledge, I'm new to them.
Any help would be appriciated!
Bee.
Comments
.~.
In Drupal 7,i am not sure of the process but,a way to do it in drupal 6 may help you.(It will be almost similar,not sure though.).
Image cache module will help you to create presets of different sizes and these presets can be used to display images in many ways.
Install the module and create a preset to display your required size of thumbnail.Then go to content types adminstration page ( Administer › Content management › Your_content_type ) .There you will find a link called Display Fields (admin/content/node-type/Your_content_type/display ).There you will find a drop down menu (next to the image field) in which you will find the previously created preset.Select the preset and save the settings.Now try creating new content and see if it works.
Thanks koneru, the CCK is now
Thanks koneru, the CCK is now in the core of Drupal 7 and this way works too! We can choose which field to display in the body, teaser, rss or search results...but it is really hard to customize the way those contents display. When I edit a content type teaser and attach an image field, I can only choose the thumbnail to display in the teaser and that is all. It should have an extra format field so we can control the output of that field.
No way using the UI to do that work so I try to use PHP template. Because Drupal can render the image field as a thumbnail so I think that there should be a way in the core, I found these lines:
The trick is inside
drupal_render($item)
. I'm trying a new approach to field--body--content_type.tpl.php but still get stuck in that file.Any idea?
theme('image_style')
there is a theme('image_style') which is roughly analogous to theme('imagecache')
Looks something like:
might be an easier approach.
http://modelagnostic.co.uk/
Koneru had it right - I
Koneru had it right - I struggled with this, and reading this post reminded me of something I'd seen.
Home » Administration » Structure » Content types » Product » Manage display
You can set how fields are displayed for the Default view (or Teaser view).
These are obeyed in theme templates (like node--content_type.tpl.php) when you simply do this:
print render($content['field_custom_image']);
So in Manage Display, if you choose Thumbnail, the template will render the thumbnail version of the image. I also get a dropdown with all image sizes I've defined (I've added a custom image style, as well as the default original/thumbnail/medium/large).
I'm still struggling with D7's lack of obvious documentation, as there's no obvious link between writing a theme template, and using the admin screens to configure how the theme renders.
Here's my comment about it with regards to another problem I had when trying to render a text field:
http://drupal.org/node/1109274#comment-4286316
The image_style_url() function
This is a very handy little function in Drupal 7.
The image_style_url() function takes the URI and converts into the relevant URL for the image style provided. Note, you need to use public:// rather than the base url of your site for this function to work.
The great thing is that if the image hasn't yet been created then Drupal does it automatically!
Great, thanks. Just a note
Great, thanks. Just a note that you have forgotten a closing " at the img tag above
Style not found....
Hi,
If I use
$style = 'thumbnail'
theimage_style_url($style, $img_url)
function fails. I found out that the subfunctionfile_stream_wrapper_valid_scheme('thumbnail')
returns false.But 'thumbnail' is in the list of admin/config/media/image-styles.
What is going wrong?
Bye,
Martin
Fixed code
The above code is missing a quote. here it is fixed:
works, very helpful
This works and is very helpful. The only thing to note is if you're using the private filer system, I believe the code would look like:
doesnt work for me =( tried
doesnt work for me =(
tried to do this way:
print theme('image_style', array( 'path' => $field_work_image['ru'][0]['uri'], 'style_name' => 'thumbnail'));
doesn`t work to.Both variants returns only
~sites/default/files/styles/thumbnail/public
but not the correct path. They even doesnt create a thumbnail.Don`t know what the problem and how to show thumbnails in node.tpl.php, maybe someone knows how to solve it?
Thanks .Works like a charm
Thanks .@roberthernandez
Works like a charm
Wow Thanks for the snippet. I
Wow Thanks for the snippet. I tried to do the same in drupal 6 way and suddenly couldn't find the "filename" value of the image!
image_style_url() function is really helpful.
BTW, "The great thing is that if the image hasn't yet been created then Drupal does it automatically!" This feature was there in drupal 6 too. If the thumb image was not present, it used to get generated as soon as we call the image.
Thanks for the code!
I altered it a little to get the path from a cck image field (imageone) in a node. '$style' didn't seem to like a value of 'none' for the original image but I just created a new style named 'default' and didn't add any effects to the style, it worked.
You might like to try
You might like to try this:
You'll notice that the $node object in your example has
['und']
which is the language element of the field... change the language and your code probably won't work. It's much cleaner to render fields as above. :-)I have field_gallery field
I have field_gallery field which, obviously, holds gallery images for the node.
How to render the view for the first item in that field (eg. field_gallery[0]) ?
You could try
You could try this:
You can cycle through the above to render all your gallery images:
search results
I need a solution for rendering fields in search results. This did the trick and well. Thanks!
You use field_view_value
You use field_view_value instead
for instance:
I don't think you can just
I don't think you can just throw a zero into the 3rd argument.
See first comment here: http://api.drupal.org/api/drupal/modules!field!field.module/function/field_view_value/7
This should work, however (note that my node is the $entity object)
Rendering an image preset
I still don't get this! I have 3 image presets: thumbnail, small & medium and I want to print them using php. In Drupal 6 I used:
<img src="<?php print 'sites/default/files/imagecache/<strong>thumbnail</strong>/' . $node->field_image_cache['0']['filepath']; ?>" />
Can anyone possibly tell me what the equivalent of this is for Drupal 7? Apologies, my php skills are not the best.
You'd better use the previous example
The previous example shows it all. To print out a complete HTML image tag including all the image's properties (width, height, title, etc.), use:
If you insist on doing it your way you can use:
But this only works if the field's language is undefined. You could retrieve the field's language first like this:
But this is all too much trouble since Drupal can generate a complete image tag using the first piece of code. Good luck!
Laurens Meurs
wiedes.nl
If you are using Media module
If you are using Media module and format is rendered file then you should change the settings array as below where custom_view_mode is your own view mode.
Thanks. This works for me.
Thanks. This works for me.
I used this
Still no way
... Still no way using the UI to do that work... so I try to use a clean PHP template and dont work jet.
This works fine to me.
https://drupal.org/comment/6865930#comment-6865930 works fine to me.
Thanks for sharing
image style in custom node template
You can then do the following:
print theme('image_style', array('style_name' => 'large', 'path' => $node->field_blog_image[LANGUAGE_NONE][0]['uri']));