TIFFs not respecting compression settings when Imagick compiled against ImageMagick > 7.0.7-11
With Imagick on Windows, TIFF compression settings do not seem to work at all.
Take the following:
<?php
declare(strict_types=1);
$bd = __DIR__.DIRECTORY_SEPARATOR;
$inputFn = $bd.'photo.jpg';
$outcompressed = $bd."out_compressed.tiff";
$outuncompressed = $bd."out_uncompressed.tiff";
@unlink($outcompressed);
@unlink($outuncompressed);
// Create uncompressed TIFF
$im1 = new Imagick($inputFn);
$im1->setImageCompression(Imagick::COMPRESSION_UNDEFINED);
$im1->writeImage($outuncompressed);
$im1->clear();
// Create LZW compressed TIFF
$im2 = new Imagick($inputFn);
$im2->setImageCompression(Imagick::COMPRESSION_LZW);
$im2->writeImage($outcompressed);
$im2->clear();
$cfz = filesize($outcompressed);
$ufz = filesize($outuncompressed);
$diff = $ufz - $cfz;
$reduction = $ufz > 0 ? ($diff / $ufz) * 100 : 0;
$digits = log10(max($ufz, $cfz, 1));
$digits = max(1, ceil($digits));
printf(' Uncompressed size: %'.$digits.'d bytes' . PHP_EOL, $ufz);
printf(' Compressed size: %'.$digits.'d bytes' . PHP_EOL, $cfz);
printf(' Difference (php): %'.$digits.'d bytes (%4.1f%% reduction)' . PHP_EOL, $diff, $reduction);
echo phpversion("imagick").PHP_EOL;
echo Imagick::getVersion()['versionString'].PHP_EOL;
I get the following output:
PS C:\imagick-test> php .\test.php
Uncompressed size: 56894836 bytes
Compressed size: 56894836 bytes
Difference (php): 0 bytes ( 0.0% reduction)
3.8.0
ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
Note I have tried all manner of ways of writing the file to no avail - lots of permutations of the following:
// Create LZW compressed TIFF
$im2 = new Imagick($inputFn);
$im2->stripImage();
$im2->setImageFormat('TIFF');
$im2->setOption('tiff:compression', 'lzw');
$im2->setImageCompression(Imagick::COMPRESSION_LZW);
$im2->setImageCompressionQuality(100);
$im2->writeImage("tiff:$outcompressed");
$im2->clear();
and please note that running under WSL, I get the following output:
Uncompressed size: 56878294 bytes
Compressed size: 30187244 bytes
Difference (php): 26691050 bytes (46.9% reduction)
3.8.0
ImageMagick 6.9.11-60 Q16 x86_64 2021-01-25 https://imagemagick.org
I.e. it works as expected (nothing wrong with the code itself).
Is this a bug in imagick? Is there any way around it without resorting to command line arguments or running in WSL on windows?
Thanks!
Please see screenshot of photos:
The photo in use is the following:
https://commons.wikimedia.org/wiki/File:SQM_GE_289A_Boxcab_Quillagua_-_Barriles.jpg
Direct link to file download:
https://upload.wikimedia.org/wikipedia/commons/4/4e/SQM_GE_289A_Boxcab_Quillagua_-_Barriles.jpg
I just quickly checked if it could be related to ImageMagick version but I'm not getting any compression with any version. Also the TIFF images that I got are much smaller. This is from running on 2 different ImageMagick version under Linux (Ubuntu 24.04):
jakub@bukka-desktop:~/prog/php/tests/imagick$ php tiff-compression.php
Uncompressed size: 5025760 bytes
Compressed size: 5025760 bytes
Difference (php): 0 bytes ( 0.0% reduction)
@PACKAGE_VERSION@
ImageMagick 7.1.1-45 Q16-HDRI x86_64 3cbce5696:20250308 https://imagemagick.org
jakub@bukka-desktop:~/prog/php/tests/imagick$ php tiff-compression.php
Uncompressed size: 5021186 bytes
Compressed size: 5021186 bytes
Difference (php): 0 bytes ( 0.0% reduction)
@PACKAGE_VERSION@
ImageMagick 6.9.3-0 Q16 x86_64 2025-03-13 http://www.imagemagick.org
And the files are:
-rw-rw-r-- 1 jakub jakub 5021186 Jul 27 16:02 out_compressed.tiff
-rw-rw-r-- 1 jakub jakub 5021186 Jul 27 16:02 out_uncompressed.tiff
-rw-rw-r-- 1 jakub jakub 4294964 Oct 5 2013 photo.jpg
I will need to investigate more but doesn't look like something Windows specific.
I can confirm this issue has been present for several years, we've just been working around it by running under WSL.
I'll add that this presents an extra issue for large TIFF files as it essentially means that you're hitting the BigTiff limit super early
Also worth noting that ZIP compression does not work on Windows either.
Run the following script:
<?php
/**
* Save Imagick's standard 'rose:' image as TIFF with different compressions
* and report file sizes relative to the uncompressed version.
*/
function human_size(int $bytes): string {
$units = ['B','KB','MB','GB'];
$i = 0;
$val = $bytes;
while ($val >= 1024 && $i < count($units)-1) {
$val /= 1024;
$i++;
}
return sprintf('%0.2f %s', $val, $units[$i]);
}
function save_tiff_variant(string $path, int $compression): void {
$im = new Imagick();
$im->readImage('rose:');
$im->setImageFormat('tiff');
$im->setImageCompression($compression);
// For lossless TIFF, quality is ignored, but set harmlessly:
$im->setImageCompressionQuality(0);
// If you ever need BigTIFF, uncomment:
// $im->setOption('tiff:write-big-tiff', 'true');
if ($im->writeImage($path) === false) {
throw new RuntimeException("Failed to write $path");
}
$im->destroy();
}
$dir = rtrim(__DIR__, DIRECTORY_SEPARATOR);
$paths = [
'none' => $dir . DIRECTORY_SEPARATOR . 'rose_none.tiff',
'lzw' => $dir . DIRECTORY_SEPARATOR . 'rose_lzw.tiff',
'zip' => $dir . DIRECTORY_SEPARATOR . 'rose_zip.tiff',
];
// 3) Save the three variants
save_tiff_variant($paths['none'], Imagick::COMPRESSION_NO);
save_tiff_variant($paths['lzw'], Imagick::COMPRESSION_LZW);
save_tiff_variant($paths['zip'], Imagick::COMPRESSION_ZIP);
// 4) Stat sizes and compare
$sizes = array_map('filesize', $paths);
$base = $sizes['none'];
$staticInfo = Imagick::getVersion()['versionString'] ?? '';
$v = get_imagick_versions_from_phpinfo();
echo 'OS Family: ' . PHP_OS_FAMILY . PHP_EOL;
echo 'PHP Version: ' . PHP_VERSION . PHP_EOL;
echo 'Imagick Version: ' . $staticInfo . PHP_EOL;
echo 'PHPInfo - Compiled With: ' . $v['compiled_raw'] . PHP_EOL;
echo 'PHPInfo - Runtime Version: ' . $v['runtime_raw'] . PHP_EOL;
echo "\nFile sizes:\n";
$rows = [];
foreach ($sizes as $label => $bytes) {
$pct = $base > 0 ? ($bytes / $base) * 100.0 : 0.0;
$change = $base > 0 ? (1.0 - ($bytes / $base)) * 100.0 : 0.0;
$rows[] = [
'Variant' => strtoupper($label),
'Size (bytes)' => number_format($bytes),
'Size (human)' => human_size($bytes),
'% of NONE' => sprintf('%0.1f%%', $pct),
'Savings vs NONE'=> sprintf('%0.1f%%', $change),
];
}
$headers = array_keys($rows[0]);
$widths = array_fill_keys($headers, 0);
foreach ($rows as $r) {
foreach ($r as $k => $v) {
$widths[$k] = max($widths[$k], strlen((string)$v), strlen($k));
}
}
foreach ($headers as $h) {
echo str_pad($h, $widths[$h] + 2);
}
echo "\n";
foreach ($headers as $h) {
echo str_repeat('-', $widths[$h]) . " ";
}
echo "\n";
foreach ($rows as $r) {
foreach ($headers as $h) {
echo str_pad((string)$r[$h], $widths[$h] + 2);
}
echo "\n";
}
/**
* Extracts a version like 7.1.2-0 or 7.1.0-13 from a longer string.
*/
function extract_im_version_number(string $s): ?string {
if (preg_match('/ImageMagick\s+([0-9]+\.[0-9]+\.[0-9]+(?:-[0-9]+)?)/i', $s, $m)) {
return $m[1];
}
return null;
}
/**
* Reads phpinfo(INFO_MODULES) and pulls the exact two lines:
* - "Imagick compiled with ImageMagick version ..."
* - "Imagick using ImageMagick library version ..."
*/
function get_imagick_versions_from_phpinfo(): array {
ob_start();
phpinfo(INFO_MODULES);
$info = ob_get_clean();
// Strip tags for simpler regex across platforms (Windows/Linux)
$text = trim(strip_tags($info));
$compiledRaw = null;
$runtimeRaw = null;
if (preg_match('/Imagick compiled with ImageMagick version\s*(.+)/i', $text, $m1)) {
$compiledRaw = trim(ltrim(trim($m1[1]), '=> '));
}
if (preg_match('/Imagick using ImageMagick library version\s*(.+)/i', $text, $m2)) {
$runtimeRaw = trim(ltrim(trim($m2[1]), '=> '));
}
return [
'compiled_raw' => $compiledRaw,
'runtime_raw' => $runtimeRaw,
'compiled' => $compiledRaw ? extract_im_version_number($compiledRaw) : null,
'runtime' => $runtimeRaw ? extract_im_version_number($runtimeRaw) : null,
];
}
And get these results on Ubuntu:
OS Family: Linux
PHP Version: 8.4.13
Imagick Version: ImageMagick 6.9.11-60 Q16 x86_64 2021-01-25 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 6.9.11-60 Q16 x86_64 2021-01-25 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 6.9.11-60 Q16 x86_64 2021-01-25 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,202 8.99 KB 92.7% 7.3%
ZIP 7,686 7.51 KB 77.4% 22.6%
And this on Windows 11:
OS Family: Windows
PHP Version: 8.4.13
Imagick Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
Running on some older versions, I actually do get this to work:
OS Family: Windows
PHP Version: 7.3.24
Imagick Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,202 8.99 KB 92.7% 7.3%
ZIP 7,686 7.51 KB 77.4% 22.6%
OS Family: Windows
PHP Version: 7.4.12
Imagick Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,202 8.99 KB 92.7% 7.3%
ZIP 7,686 7.51 KB 77.4% 22.6%
However, versions 8+ seems to not work:
OS Family: Windows
PHP Version: 8.3.27
Imagick Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
OS Family: Windows
PHP Version: 8.2.1
Imagick Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
OS Family: Windows
PHP Version: 8.1.11
Imagick Version: ImageMagick 7.1.0-18 Q16 x64 2021-12-14 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.0-18 Q16 x64 2021-12-14 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.0-18 Q16 x64 2021-12-14 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
OS Family: Windows
PHP Version: 8.0.30
Imagick Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
However, it's not just version 7 vs version 8, as my copy of 7.4.33 does not work:
OS Family: Windows
PHP Version: 7.4.33
Imagick Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
Same with 7.4.0:
OS Family: Windows
PHP Version: 7.4.0
Imagick Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
Even a fresh copy of imagick on 7.3.24 doesn't seem to work:
OS Family: Windows
PHP Version: 7.3.24
Imagick Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
Very strange, using two different versions of the extension with the same version of PHP I get different results:
PS C:\Users\harry\web\htdocs\test\tiff testing> php .\tiff_test.php
OS Family: Windows
PHP Version: 7.3.24
Imagick Version: ImageMagick 7.1.0-13 Q16 x64 2021-11-10 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.0-13 Q16 x64 2021-11-10 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.0-13 Q16 x64 2021-11-10 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
PS C:\Users\harry\web\htdocs\test\tiff testing> php .\tiff_test.php
OS Family: Windows
PHP Version: 7.3.24
Imagick Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,202 8.99 KB 92.7% 7.3%
ZIP 7,686 7.51 KB 77.4% 22.6%
Latest PHP version and Imagick version:
PS C:\Users\harry\web\htdocs\test\tiff testing> php -v
PHP 8.4.14 (cli) (built: Oct 22 2025 08:49:43) (ZTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Zend Engine v4.4.14, Copyright (c) Zend Technologies
Result as of today:
PS C:\Users\harry\web\htdocs\test\tiff testing> php .\tiff_test.php
OS Family: Windows
PHP Version: 8.4.14
Imagick Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Compiled With: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
PHPInfo - Runtime Version: ImageMagick 7.1.1-46 Q16 x64 8209e84:20250318 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
After some more digging, this appears to have been introduced in version 3.6.0 of the extension:
OS Family: Windows
PHP Version: 7.3.24
Imagick Module Version: 3.4.4
Imagick::getVersion(): ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
Imagick Compiled With: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
Imagick Runtime Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,202 8.99 KB 92.7% 7.3%
ZIP 7,686 7.51 KB 77.4% 22.6%
OS Family: Windows
PHP Version: 7.3.24
Imagick Module Version: 3.5.0
Imagick::getVersion(): ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
Imagick Compiled With: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
Imagick Runtime Version: ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,202 8.99 KB 92.7% 7.3%
ZIP 7,686 7.51 KB 77.4% 22.6%
OS Family: Windows
PHP Version: 7.3.24
Imagick Module Version: 3.6.0
Imagick::getVersion(): ImageMagick 7.1.0-13 Q16 x64 2021-11-10 https://imagemagick.org
Imagick Compiled With: ImageMagick 7.1.0-13 Q16 x64 2021-11-10 https://imagemagick.org
Imagick Runtime Version: ImageMagick 7.1.0-13 Q16 x64 2021-11-10 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
So the issue is not present in extension version 3.5.1, but is present in extension version 3.6.0.
Looking at the diff between tags 3.5.1 and 3.6.0-RC1 (only tag available for 3.6):
https://github.com/Imagick/imagick/compare/3.5.1...3.6.0RC1
It doesn't look like anything in there could possibly be causing this issue, the only thing I can think is that it's an upstream issue with imagemagick, as there was a version bump:
https://github.com/Imagick/imagick/commit/dcc6b6445b4325025c658d9d1f9d4324ba14de37
From ImageMagick-7.0.7-11 to 7.1.0-13
I'll do some work now and see if there were any changes made in that time that could possibly cause this issue...
As I'm not a C programmer and don't really understand the link between imagick and imagemagick, I've been leaning on good old copilot to look at this...
After going through quite a few diagnostic steps, it seems to think the issue is upstream also. It suggested filing this bug report upstream - would this be sensible @bukka ?
Bug Report for ImageMagick: Title: TIFF compression ignored in ImageMagick 7.1.0-18 Windows build
Description: MagickSetImageCompression and MagickSetOption for tiff:compression are accepted but produce no effect on output file size in Windows ImageMagick 7.1.0-18 Q16 x64. CLI tools work correctly on same system, indicating issue is in MagickWand TIFF coder, not CLI processing.
Evidence: [Include your diagnostic output]
This confirms the issue is a regression in the ImageMagick library itself, specifically in how the Windows build handles TIFF compression when called via the MagickWand API (which PHP uses) versus the CLI tools.
After some more investigation, this is not isolated to Windows.
When compiling imagick against the latest ImageMagick version in ubuntu 24 I get the following:
OS Family: Linux
PHP Version: 8.4.13
Imagick Module Version: @PACKAGE_VERSION@
Imagick::getVersion(): ImageMagick 7.1.2-7 Q16-HDRI x86_64 23405 https://imagemagick.org
Imagick Compiled With: ImageMagick 7.1.2-7 Q16-HDRI x86_64 23405 https://imagemagick.org
Imagick Runtime Version: ImageMagick 7.1.2-7 Q16-HDRI x86_64 23405 https://imagemagick.org
File sizes:
Variant Size (bytes) Size (human) % of NONE Savings vs NONE
------- ------------ ------------ --------- ---------------
NONE 9,924 9.69 KB 100.0% 0.0%
LZW 9,924 9.69 KB 100.0% 0.0%
ZIP 9,924 9.69 KB 100.0% 0.0%
i.e. compression is not being respected.
That means that this issue is not relegated to Windows only, but affects Linux as well.
And just to note, compressing using the exact same version from the CLI nets correct compression:
/mnt/c/users/harry/web/htdocs/test/tiff_testing$ magick -version
Version: ImageMagick 7.1.2-7 Q16-HDRI x86_64 23405 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(4.5)
Delegates (built-in): bzlib djvu fontconfig freetype heic jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png raw tiff webp wmf x xml zlib zstd
Compiler: gcc (13.3)
magick rose: -compress none cli_uncompressed.tiff
magick rose: -compress lzw cli_lzw.tiff
magick rose: -compress zip cli_zip.tiff
yields:
-rwxrwxrwx 9.7K Oct 23 10:46 cli_uncompressed.tif
-rwxrwxrwx 9.0K Oct 23 10:46 cli_lzw.tif
-rwxrwxrwx 7.6K Oct 23 10:47 cli_zip.tif
As expected
After filing an upstream bug report it would appear that the MagickWand api is doing the correct thing:
https://github.com/ImageMagick/ImageMagick/issues/8408
So this must be an issue in the link between PHP and ImageMagick somewhere??
AHA!!! Problem solved!
Over in the imagemagick repo, @snigbo correctly identified that I should use MagickSetCompression instead of MagickSetImageCompression:
I don't use or know PHP or IMagick, and can't comment on those. In your C program, instead of MagickSetImageCompression(), use MagickSetCompression().
This worked for the C++ program calling magickwand, so I thought, would it work in PHP?
Using the same logic, the following DOES NOT WORK:
$im = new Imagick();
$im->readImage('rose:');
$im->setImageCompression(Imagick::COMPRESSION_LZW);
$im->writeImage('out.tiff');
However, the following DOES work:
$im = new Imagick();
$im->readImage('rose:');
$im->setCompression(Imagick::COMPRESSION_LZW);
$im->writeImage('out.tiff');
My question, now, is what is the difference in these two functions?
The C descriptors of the two functions is so similar, with one residing in magick-property.c:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% M a g i c k S e t C o m p r e s s i o n %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% MagickSetCompression() sets the wand compression type.
%
% The format of the MagickSetCompression method is:
%
% MagickBooleanType MagickSetCompression(MagickWand *wand,
% const CompressionType compression)
%
% A description of each parameter follows:
%
% o wand: the magick wand.
%
% o compression: the wand compression.
%
*/
WandExport MagickBooleanType MagickSetCompression(MagickWand *wand,
const CompressionType compression)
{
assert(wand != (MagickWand *) NULL);
assert(wand->signature == MagickWandSignature);
if (wand->debug != MagickFalse)
(void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name);
wand->image_info->compression=compression;
return(MagickTrue);
}
And the other sitting in magick-image.c:
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% M a g i c k S e t I m a g e C o m p r e s s i o n %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% MagickSetImageCompression() sets the image compression.
%
% The format of the MagickSetImageCompression method is:
%
% MagickBooleanType MagickSetImageCompression(MagickWand *wand,
% const CompressionType compression)
%
% A description of each parameter follows:
%
% o wand: the magick wand.
%
% o compression: the image compression type.
%
*/
WandExport MagickBooleanType MagickSetImageCompression(MagickWand *wand,
const CompressionType compression)
{
assert(wand != (MagickWand *) NULL);
assert(wand->signature == MagickWandSignature);
if (wand->debug != MagickFalse)
(void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name);
if (wand->images == (Image *) NULL)
ThrowWandException(WandError,"ContainsNoImages",wand->name);
wand->images->compression=compression;
return(MagickTrue);
}
Is one of these deprecated? How are we supposed to be setting compression?