6 ms·
Responsive images
- frabjoused 5y agoHad to recently build a helper function in our templating engine to handle image tags - it's amazing how much markup you have to generate to get an image just right factoring pixel density, source sets, lazy loading and explicit width / height.
- topicseed 5y agoFunny thing if you have WordPress. When you upload images, you may have compressed your image when saving the file from Photoshop, but WordPress automatically crops that image to various sizes based on your theme and plugins. Some of these cropped files can often be larger than the original full size file. So make sure you use a CDN or plugin that optimizes all image files.
- chiefalchemist 5y agoThat's I believe that's incorrect. WP won't "crop up", unless you're using some special plugin. In fact, for example, if you have 3 images sizes registered, say 100px wide, 500px wide, and 1000px wide, and you upload an image 800px wide, native WP will not produce the 1000px wide image. It can't. It will only resize / crop down. It won't take an image an stretch it.
- deleted 5y ago[deleted]
- SquareWheel 5y agoI believe what they're saying is that the thumbnails Wordpress generates may be larger in file size because they're not rigorously optimized like your original image may be. There's browser plugins that will loop through your entire media library and ensure they're optimized. TinyPNG and Smush are well-known ones.
- chiefalchemist 5y agoI'd like to see that. That would mean either WP is adding meta to the image file (which I'm pretty sure it doesn't do), or WP is making a lower quality jpg in higher which - without a special AI-y algorithm - isn't going to yield true higher res.
- onli 5y agoI have definitely seen that happen with the PHP blog engine that runs my blog, either with the GD image functions or with the imagemagick integration. Probably something like: Take an optimized image, like an indexed png, load it as raw image data, scale it down, save it in the same format but not optimized. The Wordpress code looks similar.
- superfad 5y agoI've had this happen when uploading a highly optimized image (manually optimized with Squoosh) 2560px wide, then WP generating a 1920px wide version and the KB was larger. I believe I was using the free version of Smush at the time and had to change. Now using reSmush.it.
- topicseed 5y agoYes, that's exactly what I meant. I optimise my uploaded images highly and when WP created the various sizes, the smaller pixel sizes often end up heavier in kb than the original. So I have to optimise them all, now it is done automatically.
- nicbou 5y agoI've been using them for a while, with the help of this exact guide. They significantly improve speed, especially on mobile.
- tofukid 5y agoIn addition to the lesser known srcset and sizes <img> attributes, there's also the loading="lazy" attribute, which adds native lazy loading of images. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-loading https://developer.mozilla.org/en-US/docs/Web/HTML/Element/im...
- darekkay 5y agoOn my blog, I usually create and include 6 files for the same image: - 1x vs. 2x dpi for high-density monitors - png/jpg + webp + avif I don't create additional screen-optimized image versions, as the work would require 6 additional files per screen resolution. It's already quite some work, but I think it is worth it. Especially AVIF is often significantly smaller than other formats. I recently wrote a short guide on this topic [1]. [1] https://darekkay.com/blog/avif-images/ https://darekkay.com/blog/avif-images/
- leipert 5y agoRan into the same problem with my a recent website. I think I am still going to create at least one smaller version for mobile. Additionally to your technique I defined a really small image as a background image in order to get a “progressive loading for avif” kind of effect on slower connections. https://gutshaeuser-ostpreussen.de/ https://gutshaeuser-ostpreussen.de/
- genghizkhan 5y agoGhost (https://ghost.org/ https://ghost.org/) does this automatically. They used to do it for featured images till v3, v4 takes this up a notch and automatically creates different sized images for every image in your blog post. It's extremely nifty and convenient.
- tyingq 5y agoA fair amount of old php shopping cart software has done this for ages. Not for responsive design, but because they need thumbnails for category/search pages, normal images for a product page, and zoomed in photos for user detail clicks, etc. Typically, there's an imagemajick script or similar that's used on "product photo" upload.
- Santosh83 5y agoOn large monitors, browsers are still picking the image tagged as 1x, and not the 2x or 3x versions, because the DPI of the monitor is 1x, but since the screen dimensions are large, the 1x image becomes pixelated or just too small.
- bruce343434 5y agoI can't really take a blog post seriously that claims to introduce responsive design, yet still uses pixel defined sizes like it's 2005. 1200px is nothing on a 4k monitor. In my opinion your best bet is to forget about all this px-fuckery and drop the `<img>` all together. Use a `<div>` with a `background-image`, and use media queries to control how the `background-image` is cropped, zoomed, or whatevered. In general, `<img>` is a huge pain in the ass. It seems to defy all known layout rules and models, and I pretty much never want it to take up the x*y pixels that the image source is. I want it to take 100% of the width, or contain and crop it in 100% of the height, but all of this is notoriously annoying with an `<img>` tag. Seriously, use a `div` with a `background-image`. The semantics of having an image in an `<img>` tag don't matter, no one cares about it and your users won't notice it.
- Santosh83 5y agoGooglebot will care about it, especially for a site that is primarily images (as opposed to images being merely decorative)?
- rhdunn 5y agoIf you are using CSS, you will still have CSS media queries to say "load an image of this size when on a device with this maximum/minimum size" so you can send smaller sized images to devices with smaller screens. Using the HTML markup allows the browser to load the images sooner instead of having to wait for the CSS to be downloaded, parsed, and evaluated. And there is nothing preventing you from selecting a larger image on a 2K/4K display if you have it, or using CSS to provide additional cropping, zooming, etc.
- have_faith 5y agoCSS object-fit and object-position do the same thing for img tags.
- bruce343434 5y agoI've tried those but I just can't get it to work the way I want for some reason, while never having an issue with background-image.
- rikroots 5y agoOne thing I learned about responsive images - <img> elements with a srcset attribute - while developing my JS canvas library is that if you're relying on those <img> elements to as the source for your ctx.drawImage() calls then the whole thing blows up in unexpected ways when the user changes their browser's size.
- rikroots 5y agoIn case anyone's interested, this is how I solved the problem so my library can work with responsive images: https://scrawl-v8.rikweb.org.uk/learn/second-lesson https://scrawl-v8.rikweb.org.uk/learn/second-lesson
- chiefalchemist 5y ago> So, with these attributes in place, the browser will: - Look at its device width. - Work out which media condition in the sizes list is the first one to be true. - Look at the slot size given to that media query. - Load the image referenced in the srcset list that has the same size as the slot or, if there isn't one, the first image that is bigger than the chosen slot size. > --- Where many people's understanding falls short is the essential importance of the sizes value. In short, get that wrong and the browser is more likely to pick a suboptimal image from the srcset. Put another way, focusing on the scrset - which many seem to do - doesn't help if the sizes value misleads the browser about the nature of the image needed. I found this artcle some time ago and it changed my understanding for the better. It's not new, but it doesn't need to be. https://ericportis.com/posts/2014/srcset-sizes/ https://ericportis.com/posts/2014/srcset-sizes/ p.s. I do a lot of WordPress work. It's fine with srcset but usually pretty bad with sizes equal. Even Gutenberg isn't much better. That's right, 35% of the internet is doing this poorly.
- Santosh83 5y agoSizes is needed when you specify the intrinsic width of the image with the w descriptor I believe. Alternatively you can specify resolution factor (1x, 2x, etc) in which case the sizes attribute is not needed and the choice of which image to download is placed on the browser in a more autonomous manner than specifying sizes.
- chiefalchemist 5y agoYou might be correct. Regardless, the choice is always on the browser, as it should be. It's the only one who knows the screen res, how wide and tall, the markup, etc. With all that it picks. Again, that article I shared does a great job of explaining. I'll re-read to find out about 1x, 2x, etc.
- superfad 5y agoThe only time the sizes attribute is not needed when using srcset is when the rendered size of the image is fixed. One example would be a logo that is less than 320px wide (so it fits even on mobile). In that case you can add x descriptors to give a higher quality logo on retina screens. e.g. <img src="logo.jpg" alt="Logo" srcset="logo.jpg, logo-2X.jpg 2x"> Alternatively use an svg :) But if the image depends on the viewport width then 'srcset' (without x descriptors but with w descriptors) and in combination with 'sizes' is the way to go.
- splitbrain 5y agoOne thing that's annoying about responsive images is that the displays with highest density are built into the devices with the potentially shittiest bandwidth. So on a image heavy site, I might deliver 3x sized images (which may several megabytes) to an top generation iPhone in the middle of nowhere (like rural Germany) on a Edge connection... I wish there was a CSS media query that would also consider the current network connection.
- tjchear 5y agoOr better yet, give client control over the reported media sizes, so they can ask for a lower quality but faster loading website if they want to.
- Santosh83 5y ago<img> srcset with resolution descriptor is supposed to consider network speed when making a decision to download from a set of resolutions.
- Y-bar 5y agoThis! But wasn't progressive JPEG and AVIF pretty much designed to alleviate this problem though? Webp does not seem to support progressive loading however.
- Nextgrid 5y agoProgressive formats still require the device and network to allocate the bandwidth appropriately, so you'd want required traffic reaching the device first, and then images. The problem is that in most cases everything is just thrown more or less at once towards the device and then latency and packet loss goes through the roof and nothing loads.
- darekkay 5y agoThere is a prefers-reduced-data media query draft [1] which addresses the issue. It doesn't actually check the bandwidth, though (although I believe there are options to put a mobile phone into "reduced data" mode below a certain bandwidth). [1] https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-data https://developer.mozilla.org/en-US/docs/Web/CSS/@media/pref...
- jokoon 5y agoThanks for posting this. Making responsive webpage with CSS alone is not trivial. Already discovered recently about loading="lazy" when loading a lot of image in a page...
- phiresky 5y agoI never understood why the `sizes` attribute has to be so complicated. Why do I have to decide on which image to choose based on the display size instead of it automatically choosing the best image based on the size the image is displayed as? It makes it impossible to choose the "correct" image without using JS if the amount of space the image will take up of the viewport is determined in some relative manner (i.e. basically always). For example, If I have a container with width: 10em, and then in there an image with width: 50%. I want to just e.g. specify 3 alternative images with widths 200px, 400px, 800px. The browser knows the exact pixel size the image will actually be, so why can't it just choose the best one? Why do I have to specify which one to use based on the viewport width instead of the actual image width? It's impossible to calculate that unless your image is a known fixed ratio of the viewport or by asking the layout engine for the computed width (with JS). There's an answer about this being impossible by one of the spec authors here: https://stackoverflow.com/questions/38684928/correct-sizes-for-img-srcset-in-a-container-element https://stackoverflow.com/questions/38684928/correct-sizes-f... But it doesn't explain at all why it was done this way, I don't even know when I would ever prefer to specify the chosen image in the way the spec currently requires.
- extra88 5y ago> If I have a container with width: 10em, and then in there an image with width: 50%. I want to just e.g. specify 3 alternative images with widths 200px, 400px, 800px. Basically, you want container queries, something long desired. It’s actively in the works but it’s first going to be for elements. Once that is working, it can inform how to implement it for responsive images.
- chrismorgan 5y agoContainer queries are a red herring here; they don’t change anything at all. The question being asked here is “why doesn’t it just use the image’s size?” and the real answer to that is that you don’t want to wait for the CSS to all be loaded before fetching any images at all—that’d be a pessimisation, not an optimisation. Hence the sizes attribute cares about viewport sizes, and always will do. Container queries will almost certainly never get involved in this, because, along with the rest of CSS, they’re evaluated after image loading begins.
- yboris 5y agoJPEG XL may solve all these problems instantly: It is a progressive image format which has an amazing feature: you can terminate the bitstream at any point during the download and have a good looking image. So servers could keep the high resolution of an image but send over just the first (for example) 20% when client is on mobile, and send 50% of the file for laptop, and all 100% when users are on a 4K television. https://cloudinary.com/blog/time_for_next_gen_codecs_to_dethrone_jpeg https://cloudinary.com/blog/time_for_next_gen_codecs_to_deth... https://cloudinary.com/blog/improve_the_web_experience_with_progressive_image_decoding https://cloudinary.com/blog/improve_the_web_experience_with_...
- lpasselin 5y agoFor those wondering, JPEG XL is not currently supported by browsers https://caniuse.com/jpegxl https://caniuse.com/jpegxl Progressive JPEG is already supported but JPEG XL seems much better. First time I hear about JPEG XL. I have experimented with progressive JPEG a couple of years ago but switched to webp since the various public Google page scores give a better score for images in webp.
- yboris 5y agoJPEG XL is already in the latest Chrome behind a developer flag: chrome://flags Search for `jxl` (pronounced "jixel") and enable it. Then visit https://jpegxl.info/ https://jpegxl.info/ as well as the gallery: https://jpegxl.info/jxl-art.html https://jpegxl.info/jxl-art.html