Rewrite image urls within HTML ending in


I am trying to use Cloudflare’s image resizing and converting images to AVIG/WEBP on dynamically but struggling to get it to work.

Within WordPress I was able to change all my image URLs to include /cdn-cgi/image/f=auto,quality=85/ therefore the final image URL becomes,quality=85/wp-content/uploads/image.jpg

However I quickly noticed this also converted .SVG images which aren’t compatible with Cloudflare image resize and the request fails.

Can anyone help me write a worker script that will add /cdn-cgi/image/f=auto,quality=85/ into the image path as shown above however only do this for .JPG, GIF, PNG. JPEG?

My ideal worker script would:

I would pay for this if someone could please help.