I am using imagick php extension to create .webp and .jp2 files from .jpg and compress the image during the process. I'm getting good results for .webp images, but having some issues with .jp2.
This is my code to create webp:
<?php
$img = new Imagick("test.jpg");
$img->setImageFormat("webp");
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(50);
$img->writeImage("test.webp");
?>
And this works fine, my webp is almost 90% decreased in file size compared to jpg. But using the same technique for JPEG2000 (jp2), I have an increase in file size of around 30%.
This is the code I'm using for jp2:
<?php
$img = new Imagick("test.jpg");
$img->setImageFormat("jp2");
$img->setImageCompression(Imagick::COMPRESSION_JPEG2000);
$img->setImageCompressionQuality(50);
$img->writeImage("test.jp2");
?>
I tried many different images as a sourcefile (compressed and uncompressed), all had the same result. Any insights to what I am doing wrong? Thanks.
I'm not sufficiently familiar with JPEG2000 to offer any useful explanation, and would prefer if someone else can explain it better, so please don't rush to "accept" this answer.
For the moment though, note that quality works differently in JPEG2000, see the defines starting jp2:XXX here.
In PHP Imagick, you could implement the equivalent defines
with:
$imagick->setOption('jp2:quality', 40);