Christoph Posted October 20, 2011 Share Posted October 20, 2011 Hi there! Is there a way of getting rid of the width and height attributes in img-tags? I want to use my images uploaded in ProcessWire in a responsive website, but when width and height dimensions are specified, the max-width approach doesn't work. Thanks for help! Link to comment Share on other sites More sharing options...
Adam Kiss Posted October 20, 2011 Share Posted October 20, 2011 It depends on what images are you talking about. It can be either image field, or images used in tinymce (text) field. In case of image field, the answer is simple - just generate your output without those attributes. As with TinyMce, this will be tiny bit harder-you'll have to run preg_replace on your text field, like so: <?php $content = preg_replace('<<<regexp>>>', '', $body->content_field); instead of <<<regexp>>> will be your image attributes targetting regexp-i'm on iphone so it would kill me to come with it, but i'm sure you'll find something acceptable on google Link to comment Share on other sites More sharing options...
ryan Posted October 21, 2011 Share Posted October 21, 2011 This should work: $page->body = preg_replace('/ (width|height)=.\d+./', '', $page->body); I just kept it simple since I don't think there are any other tags with width/height attributes that PW's TinyMCE would use. Link to comment Share on other sites More sharing options...
slkwrm Posted October 21, 2011 Share Posted October 21, 2011 Ryan, doesn't it delete all the width=dd occurences on a page, not just in html attributes? Just in case. Link to comment Share on other sites More sharing options...
Adam Kiss Posted October 21, 2011 Share Posted October 21, 2011 @slkwrm: yes it does, although not on page per se, only in '$page->body' field. Not to make assumptions, but there shouldn't be much more other width='' occurences, if you use HTML5/XHTML1–those are generally only used on images (and I can see valid use on table/tr/td elements). Link to comment Share on other sites More sharing options...
slkwrm Posted October 21, 2011 Share Posted October 21, 2011 Thanks, Adam. Still, it's a good thing to remember in case some of your output's gone Link to comment Share on other sites More sharing options...
Christoph Posted October 24, 2011 Author Share Posted October 24, 2011 Thanks for your help guys! Ryans's method works great. There's one more question: Do I need to use it on every field were the editor is involved or is there a way to get rid of it for the whole site/template? Link to comment Share on other sites More sharing options...
ryan Posted October 24, 2011 Share Posted October 24, 2011 In this case you are modifying content at runtime to change it before it's output. You could setup something in your header include (if you using one) to take care of all your fields at once. Or you could create a Textformatter module to do it for you. This one would be particularly simple: /site/modules/TextformatterResponsiveImages.module <?php class TextformatterResponsiveImages extends Textformatter { public static function getModuleInfo() { return array( 'title' => "Responsive Images", 'version' => 100, 'summary' => "Remove the width tags from images so they can be responsive." ); } public function format(&$str) { $str = preg_replace('/ (width|height)=.\d+./', '', $str); } } Place in /site/modules/, install it, and then select it for any of your fields where you want it to apply. Link to comment Share on other sites More sharing options...
Christoph Posted October 31, 2011 Author Share Posted October 31, 2011 Thanks again, Ryan! The module works great! Link to comment Share on other sites More sharing options...
apeisa Posted November 3, 2011 Share Posted November 3, 2011 (edited) I just edited the TinyMCE settings and removed width and height from valid elements (input tab). I left img like this: img[src|id|class|alt] EDIT: There is much better solution than this. Just leave width and height intact and use max-width: 100%, height: auto on your css. More info here: Edited January 3, 2013 by apeisa 2 Link to comment Share on other sites More sharing options...
ryan Posted November 3, 2011 Share Posted November 3, 2011 Great solution! This is a far superior solution to using the module. Link to comment Share on other sites More sharing options...
Christoph Posted November 4, 2011 Author Share Posted November 4, 2011 Apeisa, could you tell me where to make these changes for TinyMCE? THX! Link to comment Share on other sites More sharing options...
apeisa Posted November 4, 2011 Share Posted November 4, 2011 Apeisa, could you tell me where to make these changes for TinyMCE? THX! This can be made through PW admin - just edit the field where you have tinyMCE inputfield, then under Input tab there is "TinyMCE Advanced Configuration Options" label and under that there is "Valid elements". Remove img[width,height] or just replace everything with this: @[id|class],a[href|target|name],strong/b,em/i,br,img[src|id|class|alt],ul,ol,li,p[class],h2,h3,h4,blockquote,-p,-table[border=0|cellspacing|cellpadding|width|frame|rules|height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor|scope],#th[colspan|rowspan|width|height|align|valign|scope],code,pre Link to comment Share on other sites More sharing options...
apeisa Posted April 2, 2012 Share Posted April 2, 2012 I just edited the TinyMCE settings and removed width and height from valid elements (input tab). I left img like this: img[src|id|class|alt] There is one big drawback using this method: after this if you edit image, tinyMCE/PW loses the image width/height setting you have gave it, and image has it's original size again. Link to comment Share on other sites More sharing options...
97s Posted December 13, 2012 Share Posted December 13, 2012 This worked perfect for me. I build responsive websites, and basically I just took out the height parameter out of the tinyMCE. I left the width, as my clients can still use it to determine the maximum size they want the image to be, then with the CSS rules for fluid images it shrinks and automatically adjust the height on its own. Link to comment Share on other sites More sharing options...
adamspruijt Posted December 13, 2012 Share Posted December 13, 2012 Am I missing something here? I've never had a problem with this working img { max-width: 100%; height: auto; } I've never needed to remove either the height or width attributes, or are you trying to accomplish something different than what I am thinking? 3 Link to comment Share on other sites More sharing options...
apeisa Posted December 13, 2012 Share Posted December 13, 2012 I just recently discovered the height: auto; trick myself, and yes, it does solve the problem very nicely. Link to comment Share on other sites More sharing options...
adamspruijt Posted December 13, 2012 Share Posted December 13, 2012 Ok good to hear Apeisa, I was going to mention it but started wondering if I was misunderstanding what the desired result was. Link to comment Share on other sites More sharing options...
NooseLadder Posted December 15, 2012 Share Posted December 15, 2012 I can confirm as well that height:auto works without making the other changes. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now