Image format conversion in thumb()

Is it possible to convert the image format of uploaded images?

My client has uploaded a lot of .png files, and it would be more bandwidth-friendly to serve them as .jpg instead.

I already use the thumb() method to display most images on the site to control resolution and file size. I imagine something like this:

thumb($img, array('convert' => 'jpg', 'quality' => 70))

Any suggestions?

You can override the default ImageMagick and/or GD driver as described here. Then you can add custom stuff as you like.

I’m doing this with my Focus plugin, too:

You still need to figure out how to convert the images. I found these to links, but didn’t check if it works:

http://www.imagemagick.org/discourse-server/viewtopic.php?t=24048#p103330

Overriding the driver might cause some conflict with other plugins, e.g. the Focus plugin. But you can actually override the filename, as both the GD driver as the ImageMagick driver will chose the image format based on the thumb file’s target file (See filename() method in /kirby/kirby/component/thumb.php). You should be able to override the output format by setting the filename option on a thumb:

$page->image()->thumb(['width' => 300, 'filename' => '{safeName}-{width}x{height}-{options}.jpg']);

Hope, this helps you to achieve the desired result. :slight_smile:

2 Likes