image-resizer icon indicating copy to clipboard operation
image-resizer copied to clipboard

Set file type

Open pimago opened this issue 2 years ago • 5 comments

Maybe it is already possible and I don't find it. But it would be great to have a setting to change the file type after resizing an image. For example to webp.

pimago avatar May 12 '22 12:05 pimago

Hmmm, I'd probably say it's out of scope for this plugin (we're just about resizing, not further manipulation), but anything is possible.

There's lots of complexities to converting images between JPG/PNG/GIF/SVG, and not to mention throwing in newer formats like WEBP/AVIF and even more older/obscure ones like TIFF/BMP. There's lots to consider that's possibly more in the realm of something like ImageOptimize

engram-design avatar May 13 '22 06:05 engram-design

Thanks, I understand. I thought it would make sense because we can also set the quality and I was only thing about webp.

pimago avatar May 13 '22 06:05 pimago

I agree this would be very useful.

It definitely would be expanding the scope of this plugin, but it would fit into the realm of "optimize assets on upload", which is the deeper purpose of this plugin.

Implementation-wise, I wonder if it may be as simple as hooking into the native transform system - basically define an image transform, and simply apply it on image upload.

acalvino4 avatar Aug 02 '23 16:08 acalvino4

Something like this as an outline?

$format = ImageResizer::$plugin->getSettings()->format;
if ($format) {
    $transform = new ImageTransform(['format' => $format]);
    $url = $asset->getUrl($transform);
    # somehow get asset path from $url above
    Craft::$app->assets->replaceAssetFile($asset, <asset-path>, $newfilename);
}

But I'm sure in reality that's probably only 10% of what you'd need to add...

acalvino4 avatar Aug 02 '23 17:08 acalvino4

I'll say that at the moment, "optimize assets on upload" is even out of scope for the plugin (which is why we'd pick ImageOptimize for that), but at some point, one day - we do want to include that functionality to Image Resizer.

It'd be great to have a free option to optimize images, and even change the filetype. All too often, out clients upload PNGs instead of JPEGs for images which can afford to be lossy. That wreaks a bit of havoc on image sizes.

We'll keep it all in mind!

engram-design avatar Aug 03 '23 04:08 engram-design