4

Does anybody know how to remove width and height?

<img src="/sites/default/files/styles/large/public/2016-03/
dummy-640x480.jpg?itok=9_RdHYGq" alt="mein dummie" typeof="foaf:Image"
class="img-responsive" width="480" height="360">

I want that the img tag looks like this

<img src="/sites/default/files/styles/large/public/2016-03/
dummy-640x480.jpg?itok=9_RdHYGq" alt="mein dummie" typeof="foaf:Image" 
class="img-responsive">
5

In your theme open file THEMENAME.theme, example in my case THEMENAME is bartik and file is bartik.theme add code below:

function bartik_preprocess_image(&$variables) {
  if (isset($variables['attributes']['width']) && isset($variables['attributes']['height'])) {
    unset($variables['attributes']['width']);
    unset($variables['attributes']['height']);
  }
}

Clear all cache and see result.

| improve this answer | |
1
/**
 * Implements template_preprocess_image().
 */
function THEMENAME_preprocess_image(&$vars) {
  if ($vars['style_name'] == 'IMAGE_STYLE_NAME') {
    // Remove explicit width & height declaration.
    unset($vars['width'], $vars['height'], $vars['attributes']['width'], $vars['attributes']['height']);
  }
}
| improve this answer | |
  • 2
    hook_preprocess_image() doesn't normally get the style name, in its array parameter. That is a value hook_preprocess_image_style() gets and passes to hook_preprocess_image(). Since the first isn't necessarily invoked from the latter, this is the less generic code. – kiamlaluno Dec 24 '18 at 14:01

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.