1

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.

xoxn-- 1'w3k4n
  • 496
  • 1
  • 7
  • 18
  • This question looks relevant http://stackoverflow.com/questions/12562471/conversion-pdf-to-png-or-jpeg-is-very-very-slow-using-imagemagick. You could do the conversion in two stages (1) produce an unscaled PNG as, using `gs`, as suggested in the answer (2) then rescale/crop that, using `convert` to produce each of the scaled PNGs. – dwarring Jul 28 '16 at 20:16
  • Thanks, yesterday realized, that gs way faster and easier to make some first steps. Now searching the way to scale it and crop. Will look at question you provide! – xoxn-- 1'w3k4n Jul 29 '16 at 07:26

1 Answers1

1

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:

  • $filename = file.pdf
  • $png = file.png
  • $r = 120
  • $ex = 4000
  • $dim = $matches[1]

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.

xoxn-- 1'w3k4n
  • 496
  • 1
  • 7
  • 18