Image resizing with Workers

Hi all, I’m working on image resizing with Workers.

I expect the Workers to resize images without changing its image URL with the code below, but it just returns the original images.

addEventListener("fetch", event => {
  return event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
  let url = new URL(request.url)
  const imageRequest = new Request(url, {
    headers: request.headers,
  })
  let options = {  
    cf: {
      image: {
        fit: "scale-down",
        width: 100,
        height: 100
      }
    }
  }
  const response = await fetch(imageRequest, options)
  return response
}

The sample image URL is here:

Is there any reason why it doesn’t work?

Thanks,
Ryo

Image Resizing is available today for Business and Enterprise Customers. To enable it, login to the Cloudflare Dashboard and navigate to the Speed Tab. There you’ll find the section for Image Resizing which you can enable with one click.

May I ask if you already have checked the docs page regarding the usage, if any errors shown up?:

Could some cache setup have something to do in between?

Another helpful article in case if needed:

Oh thanks, that’s s very basic setting…!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.