pdfimagemagickpng

How to use crop from PDF to PNG tiles using ImageMagick


Good day,

I have large issue cropping the PDF to PNG

PDF is about 1,6MB (2500x2500) and one process takes about 7-10min and generates 700MB of temporary files.
e.g.

exec("convert -density 400 'file.pdf' -resize 150% -crop 48x24@ png32:'file_%d.png'");

One PDF must generate PNGs from size 25% to 200%

Here i generate attributes like density, size for resizing in % and grids row and column count

$x = 0; $y = 0;
for ($i = 25; $i <= 200; $i += 25) {
    $x += 8; $y += 4;

    $convert[$i] = ['density' => (($i < 75) ? 200 : ($i < 150) ? 300 : ($i < 200) ? 400 : 500), 'tiles' => implode("x", [$x, $y])];
}

After i launch converter one after one and it's extremely expensive in time.

$file_cropper = function($filename, $additional = '') use ($density, $size, $tiles) {
    $pid = exec("convert -density $density ".escapeshellarg($filename)." -resize $size% -crop $tiles@ ".$additional." png32:".escapeshellarg(str_replace(".pdf", "_%d.png", $filename))." >/dev/null & echo $!");
    do {
        /* some really fast code */
    } while (file_exists("/proc/{$pid}"));
};

If i launch it simultaneously (8 processes) then ImageMagick eats all the space i have (40GB) => ~35GB of temporary files

Where is my problem, what am i doing wrong?

i tried to pass params below to functions $additional var:

"-page 0x0+0+0"
"+repage"
"-page 0x0+0+0 +repage"
"+repage -page 0x0+0+0"

nothing changes

Version: ImageMagick 6.7.7-10 2016-06-01 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
Ubuntu 14.04.4 LTS
2GB / 2CPU

EDITED

After a while managed to replace ImageMagick on GhostScript

gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r240 -sOutputFile=\"file.png\" file.pdf but can't understand how to scale image and crop it.

crop with ImageMagick generates ~35GB temporary files and takes more time than previously.


Solution

  • I managed to resolve my problem that way:

    1. $info = exec("identify -ping %w {$original_pdf_file}"); preg_match('/(\d+x\d+)/', $info, $matches);
    2. "gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r{$r} -g{$dim} -dPDFFitPage -sOutputFile=\"{$png}\" {$filename}"
    3. "convert ".escapeshellarg($png)." -gravity center -background none -extent {$ex}x{$ex} ".escapeshellarg($png)
    4. "convert ".escapeshellarg($png)." -crop {$tiles}x{$tiles}! +repage ".escapeshellarg(str_replace(".png", "_%d.png", $png))

    where:

    Step:

    1. gives me dimension of original file after what i can play with size of png in the future
    2. converts pdf to png with size i need with aspect ratio
    3. converts png to size i wish with aspect ratio 1:1
    4. cropping everything

    this process takes 27.59s on my machine with image resolution 4000x4000 and size of file - only 1,4MB & 0-30MB of temporary files.