Now, the problem is not everyone uses the same phone as me. I wish they did. That would make my life much simpler. However, even if you just look at iPhones, there are tons and tons of different sizes. So here, if I compare an iPhone 6 and an iPhone 12 Pro Max, you can see that the same image using the width of the screen and a 3 to ratio will be wildly different.
So if you only have one source of truth image that you're going to upload, you might be tempted to just upload the image in 1290 by 860 and everyone else will have to scale. The problem with that is that now your lower end devices that are already struggling for the ones bearing the burden of having to convert this image to the size you have available. Thankfully, this is a problem that has been solved. If you're using a CDN, pretty much all modern CDNs will have this functionality or you can build your own where basically the device calculates the area that they need, and they ask the image in specific size. So, your server would go, oh, you want 750 by 500, I've got this in the cache, there you go. Or if they don't have it, they would calculate it on the fly, save it in the cache for the next time, and return it.
The problem with this solution is that there is still too many different images. So, the first time, firstly, the first time we generate the image is going to have a couple seconds of cost where the image is being generated, so the user has to wait. And secondly, if you're using a managed service, it gets really expensive because most of these services will charge you based on transformations, so you're much cheaper, much better off using the cache if you can. So, the way to get around that is usually you'll end up having an array of supported sizes. So, I've gone for quite a few here. So, I've gone for like 15, but you will have supported stops where you say that I'm going to support an image that's 600 width or 1,200 width. And so, you calculate the width that you need the image in and you'll take the next best image, which means that you're still not having to rent anything too difficult, but you're still getting the optimization of not having to do so many on the fly transformations.
All right. I've talked a lot about this. I did do a demo as well. So, you can try it out. So, this is something that's the most fun, I think, if you can try it on your phone. I wasn't expecting this to be quite so big. Oh wow. Okay. I'm not sure I can do this. I guess you can see the bits on the right, which is the important part. So, the code is on the left. If you can't see it, just trust me.
Comments