Image conversion with Imagemagick

Hey - I have several images in png format that I want to convert into jpg.
They are integrated with kirbytext. Can I manipulate the thumb driver in some way?

I found this on the internet: https://imagemagick.org/script/convert.php
How can I use these snippets into Kirby’s thumb generator?

The thumb driver only comes into effect if you actually create thumbs, but if you integrate images with the image KirbyTag, no thumbs are created, unless you have modified the tag.

You could, however, modify the image kirbytag and add an action that converts the image to jpg and at the same time then renders this newly created image instead of the original one.

But if its just a few images, I’d probably rather replace the images manually and do a search and replace.

I use a modified tag for now. It just resizes all images, but what’s the best way to convert png’s to jpg’s?

…add an action that converts the image to jpg and at the same time then renders this newly created image instead of the original one.

I think this is what I want. But as I said, I don’t really know how to use the Imagemagick scripts. Has someone ever modified the driver?

My tag: $tag->src = $tag->file->thumb()->url();
Config:

'thumbs' => [
  'presets' => [
     'driver' => 'im',
     'default' => [
       'width' => 400, 
       'convert' => 'jpeg']
  ]
]

I want to say that this is for my Chrome Audit’s report. He is setting the score down because there are png’s - I need a certain ranking, like 80.

Converting file type is not an option available through the thumbs driver. You could either create a custom driver or do the type conversion before you call the thumb method.

But you would have to check which ImageMagick option does this conversion (I found format but looks like you need the mogrify utility for that).

Another option would be to use the SimpleImage class to do that. Here’s a very simple example using SimpleImage:

This code is far from perfect but should give you an idea.

<?php 

try {
  // Create a new SimpleImage object
  $image = new \claviska\SimpleImage();

  // find a png image
  $file = $page->images()->filterBy('extension', 'png')->first();
  // convert to jpg
  $image
    ->fromFile($file->root())
    ->toFile($page->root() . '/' . $file->name() .'.jpg', 'image/jpeg');

} catch(Exception $err) {
  // Handle errors
  echo $err->getMessage();
}

$newFile = $page->image($file->name() . '.jpg');
echo $newFile;