post-page

Removing Width/Height from the Image Uploader

33
responses
by
 
on
May 29th, 2008
in
WordPress Hack, WordPress Plugins

Reader Vivien writes in:

Is there a way to prevent WordPress from inserting the width and the height for images in the new 2.5 media manager?

In short, yes, but it requires you to insert some code into your theme’s functions.php file.

Fortunately, there is a WordPress filter we can use called image_downsize, which takes in three arguments (a boolean, an attachment ID, and a size string).

add_filter('image_downsize', 'my_image_downsize',1,3);

All I’m doing in the above filter is setting the filter name, the function to call (my_image_downsize), what priority I want the filter, and how many arguments the function takes.

From there, I mimic the function image_downsize in the ‘wp-includes/media.php’ file, but do not return a width or a height. As a result, when the image is sent to the editor, no width or height is present.

function my_image_downsize($value = false,$id = 0, $size = "medium") {
	if ( !wp_attachment_is_image($id) )
		return false;
	$img_url = wp_get_attachment_url($id);
	//Mimic functionality in image_downsize function in wp-includes/media.php
	if ( $intermediate = image_get_intermediate_size($id, $size) ) {
		$img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
	}
	elseif ( $size == 'thumbnail' ) {
		// fall back to the old thumbnail
		if ( $thumb_file = wp_get_attachment_thumb_file() && $info = getimagesize($thumb_file) ) {
			$img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
		}
	}
	if ( $img_url)
		return array($img_url, 0, 0);
	return false;
}

Download the Code

Here is a sample functions.php file of the code presented in this article.

I also used Andrew’s plugin generator to quickly put together a plugin that I will creatively call No Image Width or Height (download link). It will accomplish the same thing for those not comfortable with code. Just unzip, place in your WordPress plugin’s folder, and activate.

Thanks Vivien for the interesting question.

heading
33
Responses

 

Comments

  1. shadaik (6 comments.) says:

    Neat. Now, is there a way to make WordPress not assign a new class to every single uploaded picture? It is unneccessary and once you get beyond, let’s say, 1000 pics, it presumably starts to slow down the page.
    What exactly do these classes contain that makes them file-specific anyways?

  2. Julie (3 comments.) says:

    I am curious why you wouldn’t want the height and width declaration.

  3. Andrew (31 comments.) says:

    This is so necessary. Thanks Ronald. I will get the plugin later.

  4. phoenix (2 comments.) says:

    I am experiencing two things with the latest version of WP.

    1. Widgets are preventing other scripts such as blogrolling and twitter to run. Am I missing something to allow these to run along with the widgets?

    2. The picture and audio uploader are not loading and coming back with http errors. Is there a fix it patch that I missed?

    I look forward to any help you can give.

  5. no says:

    Worst practice ever. Don’t duplicate a function to edit a tiny amount of functionality. Find the appropriate filter or start a trac ticket for it to be added.

  6. Matt Algren (5 comments.) says:

    Ditto on the image classes. It’s a (minor) PiTA to remove them from every image. I have no idea what they’re for (Why does each image have a unique class?) and apparently no one else does either.

    Not sure why I’d want to remove the automatic height/width assignments. I thought that was recommended for Google etc. Have I been misinformed?

  7. Kevin (1 comments.) says:

    Many thanks. I was looking for a plugin to do just this yesterday. And it would be nice to remove that class tag too.

  8. Ronald Huereca (66 comments.) says:

    @Julie, Matt,
    I don’t know, you’ll have to ask the reader who asked the question. It was still fun figuring out how to do it.

    @No,
    If you do some basic research and look at the function in media.php, you’ll see I had no choice but to provide some duplication. Providing a ticket for this is nonsense. I’m using a filter if you would re-read the article. I suggest before you call something a worst practice and rank an article low, that you at least know what you’re talking about.

  9. Ronald Huereca (66 comments.) says:

    For those wanting to remove the classes, I’m afraid there’s no easy way to do it except by modifying core code.

    It’s around line 99 in the get_image_tag function in ‘/wp-includes/media.php’. You could also regex it out by using the image_send_to_editor filter, but that’s a little more advanced.

  10. Brian (2 comments.) says:

    I hacked this change in the wp-includes file the day I downloaded 2.5 – good to see there’s a better way of doing it

  11. Ajay (209 comments.) says:

    @Ronald, I agree with Julie above. It is a bad idea to knock off the width and height of an image. Keeping that in place ensure that the browser lays out the page properly.

    Anyway, I use IImage Browser for inserting my images. I prefer that over the WordPress editor anyday.

  12. Comme une image (3 comments.) says:

    Well! I needed this. Many thanks to make it so easy for me (I prefer the plugin way, that is theme-independant). I prefer to handle width and height via CSS, which is a better practice, especially if you plan to change your theme sooner or later.

  13. scott (1 comments.) says:

    Thank you for this post! The new image uploader in WP 2.5.1 tends to incorrectly assign image dimensions when inserting a larger image at full size. This is a nice fix so my clients don’t have to go into each image’s properties after insertion to remove/correct the height and width. Thank you for sharing this!

  14. Ronald Huereca (66 comments.) says:

    @Ajay,
    Perhaps, but this post was a response to a reader question. I’m indifferent whether it’s left in or not. Judging from some of the comments, it appears some people needed this.

  15. Monika (40 comments.) says:

    if an img tag has no width and height the browser has to read the header information from the image. sometimes this slow down your page or the browser has to correct the image size and this is absolut “unstylish” ;)

    maybe it is a better way if someone would like to change his theme, but for this “better way” you pay a good price: long load time and a bad screen layout while the browser is loading your page.

    something I can’t understand, but ok ;)

    regards
    Monika

  16. Trisha (16 comments.) says:

    Monika is right – not putting in the image width and height definitely slows down your page load – if you have multiple images on the page the load drag can be noticeable, but even a small increase in time it takes to load will cost you in terms of SEO – Google penalizes slow-loading pages, and also penalizes you for images that have no size declarations. Not worth it to me to not just include them in the img tag. Using CSS to control your image size is not the same thing at all. And using a large file size with smaller CSS styling doesn’t speed up page load time at all – the file’s size in terms of bytes doesn’t get reduced just because you “declare” it to be smaller using CSS.

    All that being said, quit criticizing the author – Ron is just answering a question, and gave a good solution to what she needed – even if what she needed was not a good practice.

  17. Ronald Huereca (66 comments.) says:

    For those wanting the ability to remove the class, I’ve put in a feature request in the WordPress trac. Hopefully it’ll make it in 2.5.2.

    http://trac.wordpress.org/ticket/7065

  18. billn (1 comments.) says:

    Or can you modify the ‘black-box’ to make the width what you want – eg instead of 500px for large, what about 565?
    Cheers

  19. ia (4 comments.) says:

    People are wondering why someone would want to remove IMG width and height attributes, and here’s my answer: because it’s a horrible way to resize images when they’re not the same dimensions as the image itself. When I upload images into WP that are wider than 500px, the width attribute still says width="500". And I don’t want that. WP should at least measure the exact size of the image before setting the IMG attributes.

    As for image classes, I think the align classes are a good idea.

  20. Monika (40 comments.) says:

    ia this is not correct.
    Administration Panel==>miscellaneous==>set the medium width to whatever you would like to have and it works like a charm

    and after a click to the full size image I have images with 900px width.

    kindly regards
    Monika

  21. raincoaster (5 comments.) says:

    I sure wish this were doable in WP.com. I have Sandbox on one blog and Monotone on the other, so the arbitrary 400 pixel width for large size images is COMPLETELY irrelevant to me. I have to strip it out every single time.

  22. MaestroGP says:

    Omitting height and width declarations in your img tags is poor form. There is never–never–a reason to leave out the height width declarations. And never resize an image using the height and width declarations. If you need to include an image in two different sizes within the same page or website, upload two different versions of the image and tag appropriately.

    If you do either of these, it shows you care more about being lazy and making it easy for yourself than you do about your readers.

  23. ia (4 comments.) says:

    Monika, I always choose full size, not medium, and it still ends up inserting width="500". I know that this can easily be fixed in several ways, but I just don’t like this intrusiveness. And it’s not a very desirable way of writing HTML.

    MaestroGP,

    The “align”, “border”, “hspace”, and “vspace” attributes of the image element were deprecated in HTML 4.01.

    The “align”, “border”, “hspace”, and “vspace” attributes of the image element are not supported in XHTML 1.0 Strict DTD.

    from: http://www.w3schools.com/tags/tag_IMG.asp

    It may be a bad practice for loading images more quickly, but it is a bad practice for web standards generally, and for resizing images through HTML specifically.

    I agree that you should never resize images with HTML, which is what WordPress is doing now…in my case, at least.

  24. phoenix (2 comments.) says:

    I love that you guys have this to complain about. I love that you hate having to go in and manually remove or change width and height. It is simple html code… and it is not that hard to make changes to. I am 52 (today) and I can write code and there is no reason why you can’t either. What I would love though is for someone to at least answer my questions from above. What I would love is to be able to complain about picture images, but I can’t because the dang thing doesn’t even work!!!!!!

    Maybe I missed some secret protocol about not asking questions that are not pertinent to the article… or maybe I am just looking in the wrong place. When I did a search for my problem, it brought me here… so I apparently am the only person with WP that has this problem.

    OK, I got that off my chest. If anyone knows of a patch or simple fix, please let me know. Thanks.

  25. Otto (215 comments.) says:

    The reason that it is inserting “width=500″ into your images is because your theme is not taking advantage of a new WordPress feature in 2.5.

    WP 2.5 introduced the whole media uploader thing, and one of the features it offers themes is to tell WordPress exactly how wide the main “post content” area is. Then it will not allow images to be any wider than that, because wide images often screw up themes by pushing the sidebars way out there. What with the commonplace digital pictures being published by newbies who don’t know how to resize properly, this is a good thing.

    However, it does mean that your theme needs to tell WordPress how wide, maximum, images should be allowed to be (when inside a post). The theme can do this very easily. Just add this inside the theme’s functions.php file:
    $GLOBALS['content_width'] = 500;

    Change the 500, obviously. 500 is the default value if this value is not specified by the theme itself.

    See? No need to go to extreme measures. Just fix the theme to be 2.5 compatible.

  26. PRand (1 comments.) says:

    i usually size my image to the size i want prior to uploading (width critical) for all my blogs (each blog has it’s own width critical specifications)

    where i get into trouble is one theme that converts all main posts images into thumbnails once they move off the main post roster and go into sidebar posts with thumbnail images. if i leave the width/height declaration the thumbnails stay at the size as declared in the original upload. so i size my images for max width on my theme and remove width/height declaration.

    it sounds like this theme could be coded to handle this issue from the discussion above

  27. ChrisR says:

    I am new to WP but just spent 2 weeks learning PHP in prep (related to my job)–while I have no problem with adding the width and height info, what I don’t get is why WP 2.5 actually changes the name of the image to tack on the dimensions. And if I change the dimensions of the image and replace the image in Uploads, WP doesn’t seem to recognize the new dimensions. Am I missing something?

  28. Comme une image (3 comments.) says:

    This usefull plugin is no more compatible with WP 2.7.
    It sets downloaded images size to 0×0 !

  29. Paul (1 comments.) says:

    Worked a charm, was driving me kind of mad so very glad to get rid of those pesky attributes, I know there there for a reason but thats coved by my CSS and makes things far more efficient.
    Thank’s so much

  30. Philippe says:

    Thank you so much for the tip and the plugin. Been looking for such a solution for a while to keep the aspect ratio of images when using CSS max-width or max-height.
    Installed on 3.3.2, so far working well.



Trackbacks/Pingbacks

  1. [...] Removing Width/Height from the Image Uploader [...]

  2. [...] Removing Width/Height from the Image Uploader – In diesem Beitrag wird beschrieben wie man die automatische Angabe zur Breite und H?he bei dem Bild-Upload in WordPress entfernen kann. [...]

Obviously Powered by WordPress. © 2003-2013

page counter
css.php