Run a library, or call a service
A library runs in your own process. You pay in CPU and memory, you control the versions, and nothing leaves your infrastructure. Best when you have a defined set of images to process at build time, or when the content is sensitive.
A hosted service transforms on request, usually driven by URL parameters, and caches the result. You pay per image or per bandwidth, and you get formats you may not want to build for — AVIF in particular. Best for large or user-generated catalogues where pre-generating every variant is impractical.
The failure modes differ, and that is usually what decides it. A library fails by consuming your server's memory under load. A service fails by being down, changing its pricing, or sitting in the critical path of every page you serve.