From 5321d2d312e99dd6cb614cde0af202bd7c9b7bad Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 10 Mar 2023 18:25:10 +0100 Subject: [PATCH] wip #5799 @1 --- src/CommandLine/Poppler.php | 49 +++++++++++++++++++++++-------------- src/Graphics/PDF.php | 2 +- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/CommandLine/Poppler.php b/src/CommandLine/Poppler.php index 7698b73..5828dd3 100644 --- a/src/CommandLine/Poppler.php +++ b/src/CommandLine/Poppler.php @@ -9,8 +9,12 @@ use Cubist\Util\Math; class Poppler { - public static function extractArea($file, $page, $rect, $to, $options = array(), $cache = null) + public static function extractArea($file, $page, $rect, $to = null, $options = array(), $cache = null) { + if (null === $to && null === $cache) { + throw new \Exception('You have to define either a destination file or a cache folder'); + } + $defaultOptions = array('resolution' => 300, 'quality' => '95', 'antialias' => true, @@ -26,17 +30,28 @@ class Poppler $extension = $options['format']; } - $res = $to . '.' . $extension; + if (null !== $to) { + $res = $to . '.' . $extension; + } $cacheFile = null; - - if (null !== $cache) { Files::mkdir($cache); - $f = hash_file('sha256', $file) . '||' . '++' . json_encode($rect) . '**' . json_encode($options) . '||' . $page . '!!'; + $f = Files::hashFileAttributes($file) . '||' . '++' . json_encode($rect) . '**' . json_encode($options) . '||' . $page . '!!'; $hash = hash('sha256', $f); - $cacheFile = $cache . '/' . $hash . '.' . $extension; + + $cacheFileWithoutExt = $cache . $hash; + $cacheFile = $cacheFileWithoutExt . '.' . $extension; + if (null === $to) { + $res = $cacheFile; + $dest = $cacheFileWithoutExt; + } else { + $dest = str_replace('.' . $extension, '', $to); + } if (file_exists($cacheFile)) { + if (null === $to) { + return $cacheFile; + } copy($cacheFile, $res); return $res; } @@ -51,8 +66,11 @@ class Poppler $pdftoppm = new CommandLine('pdftoppm'); $pdftoppm->setArg('r', $options['resolution']); - if ($options['format'] === 'png') { - $pdftoppm->setArg('-' . $options['format']); + if ($extension === 'png') { + $pdftoppm->setArg('-png'); + } elseif ($extension === 'jpg') { + $pdftoppm->setArg('-jpeg'); + $pdftoppm->setArg('-jpegopt', '"quality=' . $options['quality'] . '"'); } $aa = $options['antialias'] ? 'yes' : 'no'; $pdftoppm->setArg('-aa', $aa); @@ -67,23 +85,16 @@ class Poppler $pdftoppm->setArg('-W', round($rect['width'] * $options['factor'])); $pdftoppm->setArg('-H', round($rect['height'] * $options['factor'])); $pdftoppm->setArg(null, $file); - $pdftoppm->setArg(null, $to); + $pdftoppm->setArg(null, $dest); $pdftoppm->execute(); - $pdftoppm->debug(); - - if ($options['format'] === 'jpeg') { - $cjpeg = new CommandLine('cjpeg', $res); - $cjpeg->setArg('-quality', $options['quality']); - $cjpeg->setArg(null, $to . '.ppm'); - $cjpeg->execute(); - } if (null !== $cacheFile) { + if (null === $to) { + return $cacheFile; + } copy($res, $cacheFile); } - return $res; - } protected static function _pdftojpeg($file, $to, $nbpages, $options = array()) diff --git a/src/Graphics/PDF.php b/src/Graphics/PDF.php index fe51490..94c9394 100644 --- a/src/Graphics/PDF.php +++ b/src/Graphics/PDF.php @@ -6,7 +6,7 @@ use Cubist\Util\CommandLine\Poppler; class PDF { - public static function extractArea($file, $page, $rect, $to, $options = array(), $cache = null){ + public static function extractArea($file, $page, $rect, $to=null, $options = array(), $cache = null){ return Poppler::extractArea($file,$page,$rect,$to,$options,$cache); } } \ No newline at end of file -- 2.39.5