开发者

What are the benefits of using the real image height and width?

开发者 https://www.devze.com 2023-03-09 18:41 出处:网络
What are the benefits to using an images real height and width when rendering HTML?That is if I have an image that 100x100 pixels but I want t开发者_StackOverflow社区o display something that is 95x95

What are the benefits to using an images real height and width when rendering HTML? That is if I have an image that 100x100 pixels but I want t开发者_StackOverflow社区o display something that is 95x95 pixels, should I resize the image on the server or can I let the browser handle this? I'm really looking for a general rule along with the reason. Thanks.

<img src="image.jpg" style="width: 95px; height: 95px;" alt="an image" />


Browsers handle resampling/interpolation of bitmap images differently when they're the ones resizing the images, which may or may not be controllable with CSS. This is elaborated on in detail in this post on Flickr's dev blog. If you resize the image in an image editor or by using server technology, you'll get consistent results across browsers.

In your case, a difference of under 5 pixels around will probably not be too much (unless you're concerned about pixel perfection!), but if you're trying to squeeze dimensionally or binarily large images into small rectangles, it's best to resize them beforehand. The reduced image dimensions and file size will help with bandwidth savings.


If you're always going to use the same height, it would make sense to resize it once beforehand - in as nice a way as possible - rather than getting everyone's browser to do it. After all, you can verify the results, make any tweaks you need, use the best software you can find and not worry about how long it takes to perform the resize etc.

On the other hand, I wouldn't expect the results to be very different visually, and unless you're talking about really slow computers (or a lot of images) it's probably not going to take up that much client CPU time. You may want to check the render time on slow mobile phones though, particularly if you're targeting mobile users.


if the sizes are significantly different then you should have a large versin and a small version:

like in your example, 100px and 95px shouldnt be a big deal but lets say you have large images and you want to display thumbnails it will be better to create multiple versions of the images.

The advantage will be: 1 - faster download time when you just want to show thumbnails. 2 - More consistency on different browsers 3 - I am sure people can add at least another 100 advantages in here


Just re-size the image. Otherwise the full 100X100 image will be loaded (which weighs more than 95x95 - and difference in size gets bigger the more you're trying to scale it with html). So it'll take an unnecessary long time to load an image that will not be displayed in its full size. If you scale them down a lot using html and you have a lot of images on the page - that's a lot of wasted traffic and reduced speed.

The whole width/height thing was used when connection was very slow and everyone used phone lines with ancient modems - that way you could see the proper layout of the page before all images were loaded (and it took a while to see all images after seeing the page with all the text).


You should resize the images on the server when it makes sense. I doubt if resizing a 100x100px image to 95x95px will save you more than a couple of KBs so you're probably OK in this case.

If the difference (dimension-wise) is significant e.g. on master page you show a 100x100px thumbnails and on the detail page you have 640x480px image then you better create two versions of the image. This will make your thumbnails page load faster and you'll only serve the minimum amount of data.

On the other hand if the difference (dimension-wise) is not significant then serving two images will actually double the amount of data transferred.

Here is what Google's PageSpeed have to say about it: Serve scaled images

0

精彩评论

暂无评论...
验证码 换一张
取 消