Web Performance 101: Tuning Tips for Images
Optimizing images is a necessity as images will continue to take up major space on websites.
Join the DZone community and get the full member experience.Join For Free
visual content is an integral part of modern websites; even the most minimalist website designs rely on sharp clear images to perfect end user experience. the variety of screen sizes available today — from smartphones and tablets to laptops and desktops — makes it important to use high-quality images that render properly on any viewport. blurry or low-resolution images can make the website design look shabby, which can ultimately have a negative impact on the end user experience.
the size and type of images used on a website can directly impact website performance. optimizing images frees up a lot of bandwidth and in effect reduces the overall page load time. before implementing image optimization techniques, it’s important to take inventory of all the images on the page and eliminating those that are unnecessary. css effects like shadows and gradients provide a lot of design flexibility that can be used to replace images. similarly, instead of using images with text web fonts can be used to display unique typefaces. in this article we discuss some of the major image optimization techniques, starting with the image type/format.
choosing image formats
there are three standard image formats that are supported by all browsers (gif, png and jpeg); you should choose the format that is best suited for each section of the page. the diagram below helps narrow down the image format based on what the design requirement is:
in addition to these, there are newer image formats like webp and jpeg xr that offer better performance. webp and jpeg xr have a number of advantages over the standard formats like png and jpeg – image size being the major advantage. a webp image is 26% smaller than a png and up to 34% smaller than a jpeg. it delivers both lossless and lossy compression, and supports transparency (alpha channel) just like a png.
|tech terms explained|
|lossless compression||image metadata is removed while compressing, the final rendered image is exactly like the original.|
|lossy compression||unnecessary pixel data is removed from the file to reduce the file size. the quality of the final image varies from the original.|
not all browsers are currently compatible with webp. to use webp images on your site, you will need to implement a workaround or configure the .htaccess file on the server to display the webp image only when the browser supports it.
image compression reduces the overall file size which makes it easier to transfer and render the image. online retailers depend on high-quality images to display the products they sell. without clear images, the chances of a customer buying the product becomes very low; however, using high-resolution images can slow down a website considerably. in such cases, lossless or lossy compression algorithms can be used to compress the images to create manageable file sizes.
lossless compression creates smaller images by removing unnecessary metadata, the image is rendered without degrading the image quality. on the other hand, lossy compression removes pixel data to compress the file size and the resulting image is of lower quality. compression should also be implemented when using svg files which is an xml based image format that contains a lot of metadata.
let’s look at the impact compression has on different image formats:
using css sprites
web sites use a number of images to display navigation buttons and social icons. sprites combine such simple images into a single image, each image in the sprite can be mapped and displayed using css. even though the resulting image is large, the total file size of an optimized sprite image is usually lower than the sum of each image in the sprite. the main advantage of sprites is not having to use multiple http requests to download each image.
the introduction of http/2 has had a significant impact on the overall page load time, but studies comparing load times of sprite sets vs. individual icons over http/2 show that sprites are still a relevant part of front-end optimization techniques.
serve scaled images
another important and easy technique to optimize images is to resize it based on the display size. sending a large image to be displayed as a thumbnail will only add to the load time as the browser has to re-scale the image before rendering it. the image file size is calculated by multiplying the total number of pixels with the number of bytes used to encode each pixel so we are delivering extra pixels to the browser when sending large images. the table below illustrates the impact of such unnecessary pixels.
you can see that even though the difference in the display size and the natural image size is negligible (10px), the overhead increases as the screen resolution increases. to avoid taking up more bytes for unnecessary pixels, it’s important to scale the images based on the display area.
optimizing images is a necessity as images will continue to take up major space on websites. it’s even more significant when we consider the increasing number of users accessing websites over their mobile phones and other devices. by implementing the optimization techniques discussed above, we can cut down a lot of overhead on the server and make a vast improvement to the web page load time.
Published at DZone with permission of Kameerath Abdul Kareem, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.