简体   繁体   中英

Is there a way with php ImageMagick to count number of pages into TIF image file without loading complete file?

let me describe the scenario.

Environemnt : RedHat linux 7.9 + ImageMagick* + pecl imagick + PHP script.

Scope : Into GUI application user must be allowed to see the single images from a multi-TIF file.

File: multi-tiff file are JPEG compressed.

Problem: I have TIF file that contains N images, total size from few MB to 1/2GB and I need to access to them in two way much faster as possible:

  1. [ KO ] Need to count the total number of images contained into TIF.

    $img = new Imagick($filename); $img->getNumberImages();

    With this way of access, the php class takes quite long time to load a TIF with ~2612 images,about 498M, like 15 seconds.(uncompressed tif is about 1.3G).

  2. [ OK ] Access to the #n image into TIF to allow the user to see each single image.

    $img = new Imagick($filename."[1]");$img->setImageFormat("jpeg"); $thumbnail = $img->getImageBlob();

    It is ' OK ' because the speed is high for the scope (always less than (and far from) 1 second)

** Both point 1/2 was tested with different imagick library version: 6.7.8-9, 6.9.10-68 Q16, 7.1.0 (Q16 HDRI) [pecl imagick 3.4 or 3.7] + GraphicsMagick-1.3.36, but there aren't a meaningful difference.

Is there an alternative to get the number of images into TIF without using time to initialize the full tif with Imagick php class ?

I realize you asked for imagemagick, but php-vips can do this pretty quickly. I tried:

#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

if(count($argv) != 4) {
    echo("usage: ./page.php input-image output-image page-number\n");
    exit(1);
}

// open and check the number of pages
$image = Vips\Image::newFromFile($argv[1]);
$n_pages = $image->get("n-pages");
echo("image $argv[1] has $n_pages pages\n" );

// grab a specific page
$page = Vips\Image::newFromFile($argv[1], ["page" => intval($argv[3])]);
echo("page $argv[3] is {$page->width} by {$page->height} pixels\n" );

// thumbnail the page to 500 pixels across and save as jpg
echo("thumbnailing ...\n");
// this is needed in current libvips to stop thrashing during thumbnail,
// it'll be unnecessary in 8.13+
$page = $page->sequential([
    "tile_height" => 128
]);
$thumb = $page->thumbnail_image(500);
$thumb->writeToFile($argv[2]);
echo("done\n");

I see:

$ ls -l ~/pics/sample/x.tif
-rw-rw-r-- 1 john john 1471953608 Apr 14 09:31 /home/john/pics/sample/x.tif
$ time ./page.php ~/pics/sample/x.tif x.jpg 1234
image /home/john/pics/sample/x.tif has 2600 pages
page 1234 is 2900 by 2048 pixels
thumbnailing ...
done

real    0m0.296s
user    0m0.439s
sys 0m0.063s

So with a 1.4gb, 2600 page TIFF it can fetch and thumbnail a page in about 300ms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM