1

I'm trying to convert PNG files to lossless WebP in Perl with Graphics::Magick. Command line for that is:

$ gm convert in.png -define webp:lossless=true out.webp

My Perl code looks something like that:

use Graphics::Magick;

my $image = Graphics::Magick->new();
$image->Read("in.png");
my $image_data = $image->ImageToBlock(magick => "webp");
print $out_fh $image_data;

This code writes lossy WebP files perfectly, but how can I express the "-define" thing in terms of Perl API?

Thanks,

Update: looks like I need to call AddDefiniton API function (http://www.graphicsmagick.org/api/image.html#adddefinition). Looks like it's not exported via Perl API as of now.

squadette
  • 8,177
  • 4
  • 28
  • 39
  • Never tried it but maybe `$image->Set('webp:lossless'=>'true');` – Mark Setchell Nov 14 '17 at 21:40
  • @MarkSetchell, no, it doesn't. `Set(define => 'webp:lossless=true')` doesn't work either :) – squadette Nov 14 '17 at 22:27
  • When installed as a PHP extension, I see there is a method "setimageoption", which expects 3 arguments. From a c file i [stumbled upon](https://github.com/vitoc/gmagick/blob/master/gmagick.c), it seems the arguments are: format, key, value. – rosell.dk Sep 20 '18 at 16:45

1 Answers1

0

I know it is of no help to you, but for those interested in how to do it in PHP, here is how:

$im = new \Gmagick($src);
$im->setimageformat('WEBP');

// Not completely sure if setimageoption() has always been there, so lets check first.
if (method_exists($im, 'setimageoption')) {
    $im->setimageoption('webp', 'lossless', 'true');
}
$imageBlob = $im->getImageBlob();
$success = @file_put_contents($destination, $imageBlob);

For more webp options, check out this code

rosell.dk
  • 2,228
  • 25
  • 15