2

I want to remove width and height properties from img tags in certain content type

Currently it show's like this

<img src="http://somesite.com/dostavka-slider-ng.jpg?itok=KDymQiug" width="938" height="300" alt="blablabla" title="blablabla" />

but I want

<img src="http://somesite.com/dostavka-slider-ng.jpg?itok=KDymQiug" alt="blablabla" title="blablabla" />
  • i'm having same issue with my image in view, i didn't find any option to remove in template file , currentlty image.html.twig is getting applied. You have any idea? – rks Nov 12 '18 at 4:46
3

Here are some ways how to achieve this:

  • Override it with CSS !important (something like width: auto !important;);
  • Override field.tpl.php
  • Copy node.tpl.php to the theme folder and rename it, like node--MY_CONTENT_TYPE.tpl.php.

Example:

/* node--MY-CONTENT-TYPE.tpl.php */
...
<?php
$img_url = image_style_url('large', $node->field_image['und'][0]['uri'];
?>
<img src="<?php print $img_url; ?>" />
...
  • thanks, overriding with CSS was the simplest way, though maybe not the best – Lambrusco.Newbie Dec 26 '13 at 12:44
  • If you do anything like the last suggestion you lose alt and title tags and cannot make the site accessible. – acrosman Dec 8 '17 at 17:25
8
function mytheme_preprocess_image(&$variables) {
  foreach (array('width', 'height') as $key) {
    unset($variables[$key]);
  }
}

copy this on template.php (replace "mytheme" to ur theme name) but you need to control over this because it will remove dimensions of all images in your theme.

  • Yes - the only way I can see to filter this is by file name which means it isn't very useful. – Interlated Sep 30 '17 at 0:50
2

Sorry, i can't use comment function -> need min. 50 points :-/

Do you want to display images in image field, you can use field template function. You can create .tpl file with the field name in you theme folder and edit the code.

The templates that are possible to override include:

  • field.tpl.php
  • field--field-type.tpl.php
  • field--field-name.tpl.php
  • field--content-type.tpl.php
  • field--field-name--content-type.tpl.php

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.