1

I am trying to rotate multi-page TIFF images using Imagick in PHP. My code is working and produces rotated TIFF images as expected. However, the rotated images outputted have huge file size increases compared to the pre-rotated image.

For example, inputting a 1MB tiff turns into a 33MB tiff output file.

How do I rotate the TIFF file whilst maintaining a file size comparable to the original input file?

$degrees = 90;
$image = new Imagick($pathToInputTiffFile);

for ($i = 0; $i < $image->getNumberImages(); $i++) {
    $image->setIteratorIndex($i);
    $image->rotateImage('#000', $degrees);
}

$image->setFirstIterator();
$image->writeImages($pathToOutputTiffFile, true);
  • Try compressing the tiff after performing rotation - For reference see [this](https://stackoverflow.com/questions/22755833/lzw-compression-method-not-compressing-tiff-image-with-imagick) or similar posts such as [this](https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=13484) – Ken Lee Mar 31 '23 at 03:25

1 Answers1

2

Thanks for the suggestions Ken Lee.

Further to your comment, I discovered that ImageMagick does not compress TIFF images produced by default, even if the input file already has compression. I checked my input TIFF files; they were using CCITT T.6 compression (Group4) with a bit depth of 1.

I found more information about Group4 compression here.

I added these two options and am now able to produce output TIFFs of roughly the same file size as the input TIFF.

$degrees = 90;
$image = new Imagick($pathToInputTiffFile);

for ($i = 0; $i < $image->getNumberImages(); $i++) {
    $image->setIteratorIndex($i);
    $image->rotateImage('#000', $degrees);
}

$image->setFirstIterator();
$image->stripImage();
$image->setImageDepth(1);
$image->setCompression(Imagick::COMPRESSION_GROUP4);
$image->writeImages($pathToOutputTiffFile, true);