How do set cache duration for image resize?

I’m using Cloudflare Image Resizing to serve images from S3. After implementing it, CF billed me for 800k image resize request in October. That’s ok coz I thought that maybe it’s just the initial request. In November, it billed me for 900k image resize request. I thought that cached images are no longer billed? (i.e. only the first resize is billed). And if that’s the case, the only reasonable explanation is that the images are not being cached for a long time. Questions: 1. Is there a way to set the cache to be the entire year? 2. If so, how would I do that? 3. Would I add a Page rule? 4. Should I instead set the cache control on S3? Not entirely sure how to do that but if that’s the only way, I will investigate.
13 Replies
Chaika
Chaika13mo ago
You can check the docs here on caching: https://developers.cloudflare.com/images/image-resizing/url-format/
Resized images follow the same caching rules as the original image they were resized from, except the minimum cache time is one hour. If you need images to be updated more frequently, add must-revalidate to the Cache-Control header. Resizing supports cache revalidation, so we recommend serving images with the Etag header. Refer to the Cache docs for more information.
i.e You can use cache rules/page rules to force caching on the source image and IR will respect that For example, something like this: (the Images I use are immutable, if yours aren't you might want lower. You can match on hostname like subdomain or on path using Starts With for example)
URL format · Cloudflare Image Optimization docs
You can convert and resize images by requesting them via a specially-formatted URL. This way you do not need to write any code, only change HTML …
No description
Chaika
Chaika13mo ago
Just keep in mind cache is not guaranteed at all. If your images are unpopular, they will be evicted far before TTL. TTL is just a max, that's all Cloudflare's working on a brand new Image Resizing Pricing scheme: https://blog.cloudflare.com/merging-images-and-image-resizing/ Based on unique transformations per month, due to the unpredictability of cache. It was delayed though due to a focus on reliability "code orange" after the incident earlier last month
SML
SMLOP13mo ago
Thank you. I will use the page rules then. That’s a mix of what i imagined I had to do when I saw the bill. Thanks for confirming that page rules will be what I should be using ❤️ oh the config moved to cache rules. (was looking for it forever) — you are a god send! thank you!!1 ❤️
Chaika
Chaika13mo ago
MeowHeartCloudflare yea Page rules will be eventually going anyway, replaced with all of the specific rule variants like Cache Rules
Unsmart
Unsmart12mo ago
@SML the new pricing model is released as an fyi 🙂 can change here: https://dash.cloudflare.com/?to=/:account/images/delivery-zones
SML
SMLOP12mo ago
Thank you! actually I have a separate related question to Images — there’s this origin rule in the settings that I never understood what it meant. Specifically: I understand that if I have a transform that originated from my site, then it will run it, but would it prevent google to also see it if i turned the origin on? What I meant is — 1. Can I make sure that it will only transform images that come from my domain (and ideally localhost), but ignore the rest? 2. If an image is already transformed by our domain, could Google Images still read it? 3. And mainly the use case is — I really don’t want anyone else to use our website as an infinite image resize machine, because of potential abuse, but I also don’t want enabling it to prevent images from showing up on google. What should my settings be exactly? actually i just saw this:
In 2019, we launched Image Resizing, which can optimize and transform any publicly available image on the Internet based on a set of parameters.
You can use it to resize images from any domain? How would you do that? There are some youtube thumbnails being hosted on our site that I have always thought that I would have to display them directly from YouTube. Is there a KB to know how I might do that? (i know that i have many questions but not knowing how to set these things correctly has been causing issue. I can open a new ticket if you prefer) re: pricing — is this calculation correct: Current: $117 664,944 requests If all these are uniques, since I set images to cache for 1 year, then 664 x 0.50 = $332 Is that correct? it’s so challenging… 😅 I can’t change it afterwards after opting into the new model, right, so…
Deanna
Deanna12mo ago
Can I make sure that it will only transform images that come from my domain (and ideally localhost), but ignore the rest?
You can limit transformations to work only on images from your domain by unchecking "Resize images from any origin" https://developers.cloudflare.com/images/get-started/#enable-transformations
I really don’t want anyone else to use our website as an infinite image resize machine
Transformations can be requested through this URL format: <zone>/cdn-cgi/image/<options>/<source> where <source> represents the original image that gets transformed When "Resize images from any origin" is enabled, then publicly available images (like ones from Wikipedia or Youtube, for example) can be used as the source. The transformed image still gets served from your domain, so this setting doesn't affect how images appear in search results Disabling the setting will prevent this use case (since you won't be able to transform images outside of your domain) Your math looks correct, but I'd be surprised if your usage in October represents unique requests. In general, caching is highly unpredictable and we often see cases where images get (unexpectedly) ejected from cache, so setting cache to one year isn't a guarantee
Erisa
Erisa12mo ago
You can also use Workers to transform images from anywhere even if that setting is disabled, so a custom Worker to transform images based on more custom criteria may work depending on the use case (though this adds charges for Worker requests of course)
Unsmart
Unsmart12mo ago
Yeah the math is "correct" but mostly just seems a misunderstanding of how the pricing works. Take the number of total images you have and multiply it by the number of transformations you do. So if you have for example 10,000 images with 3 transformation options that would total 30,000 unique transformations assuming all of them get requested. This would be $15/mo savings of over $100.
Deanna
Deanna12mo ago
If you're unsure about the new pricing, send me your domain and I can pull the unique requests from your account. Feel free to DM here or send me an email [email protected]
Unsmart
Unsmart12mo ago
I wonder if it might be possible / worth it to do something like the new workers pricing did where it showed your current usage vs what the new bill would be on the updated model NODDER
Deanna
Deanna12mo ago
Likely worth it — I gotta look into how possible / when it'd be possible
SML
SMLOP12mo ago
Thank you! I will DM here.

Did you find this page helpful?