How to use crop from PDF to PNG tiles using ImageMagick -


good day,

i have large issue cropping pdf png

pdf 1,6mb (2500x2500) , 1 process takes 7-10min , 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 size 25% 200%

here generate attributes density, size resizing in % , grids row , column count

$x = 0; $y = 0; ($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 launch converter 1 after 1 , 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 $!");     {         /* fast code */     } while (file_exists("/proc/{$pid}")); }; 

if launch simultaneously (8 processes) imagemagick eats space have (40gb) => ~35gb of temporary files

where problem, doing wrong?

i tried pass params below 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 while managed replace imagemagick on ghostscript

gs -dnopause -dbatch -sdevice=pngalpha -r240 -soutputfile=\"file.png\" file.pdf can't understand how scale image , crop it.

crop imagemagick generates ~35gb temporary files , takes more time previously.

i managed resolve problem 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 can play size of png in future
  2. converts pdf png size need aspect ratio
  3. converts png size wish aspect ratio 1:1
  4. cropping everything

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


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -