I have a problem to set image dimension on various position. I want a 300x100px in top, 100x75px for thumbnail and other dimensions on other place. In WordPress I can just add "width" attribute to set image width, also class. Example: //img src="location" width="100" class="myclass" alt="". In drupal I can't found how to set this.

I know, I can set in image style with various dimension but too many dimensions. I think, it will increase my hosting disk.

How to set width attribute on image in Drupal block/views like [img src="location" width="value"]?

2 Answers 2


You can try hook_preprocess_image in template.php

function <THEMENAME>_preprocess_image(&$variables) {
    // If this image is of the type 'thumbnail' then assign additional classes to it:
    if ($variables['style_name'] == 'thumbnail') {
        $variables['attributes']['class'][] = 'myclass';
        $variables['attributes']['class'][] = 'yourclass';
        $variables['attributes']['width'] = 'yourclass';


function <THEMENAME>_image_style($variables) {
  $style_name = $variables['style_name'];
  $path = $variables['path'];
  $style_path = image_style_path($style_name, $path);
  if (!file_exists($style_path)) {
    $style_path = image_style_url($style_name, $path);
  $variables['path'] = $style_path;
  if (is_file($style_path)) {
    if (list($width, $height, $type, $attributes) = @getimagesize($style_path)) {
      $variables['attributes'] = array('class' => 'fullwidth');     
      $variables['width'] = '400px';
      $variables['height'] = '400px';
  return theme('image', $variables);
  • thanks for the answer Nikhil. But, I'm still confuse, How can I use it in views/block? For example, I want use that function on my sidebar block. where I can set the width, and how to use it in other block with different dimension?
    – sumedi
    Dec 12, 2012 at 14:04
  • @sumedi, if you place this code in template.php you will be able to use it anywhere. The only way to limit this is with the image-style name. The Attribute will be automatically get added to the image. You cannot edit them through views/blocks.
    – niksmac
    Dec 12, 2012 at 15:12
  • thanks for your help. so, I have to make some image-style (may be two) for some purpose other than default image-style(thumbnail, medium, large). and I can set a class or width for each image-style. sorry, I'm new to drupal.
    – sumedi
    Dec 13, 2012 at 9:41
  • yes thats what i said.
    – niksmac
    Dec 13, 2012 at 9:46
  • I'm still found a problem. The code is added a class on my image_style, but I get warning: Notice: Undefined index: style_name in resep962_preprocess_image() (line 4 of /home/sumedi/public_html/drupal-7.17/sites/all/themes/resep962/template.php).
    – sumedi
    Dec 13, 2012 at 9:56

Hi create custom image style in admin section for the following url


then you will set in views section, after adding image field in views you need to find image style drop down option to select your created custom style

hope fully help this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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