]> _ Git - cubist_util.git/commitdiff
wip #5799 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 10 Mar 2023 17:25:10 +0000 (18:25 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 10 Mar 2023 17:25:10 +0000 (18:25 +0100)
src/CommandLine/Poppler.php
src/Graphics/PDF.php

index 7698b73e9f6bc5fc636015820ea6c18816f68098..5828dd38a594ed309229a70ad2f89e1ff960bacc 100644 (file)
@@ -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())
index fe51490c5ff3da521720a1787f33d30fd4cf8b2b..94c9394d55767b3048d77d29f29c2215668da60b 100644 (file)
@@ -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