]> _ Git - cubist_util.git/commitdiff
wait #6094 @5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 4 Sep 2023 15:16:16 +0000 (17:16 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 4 Sep 2023 15:16:16 +0000 (17:16 +0200)
src/CommandLine/Poppler.php

index d76daaaee49803ddecccc51d589870395fa84295..bc553597078a37dbe0e83c1197e898e008e21fdc 100644 (file)
@@ -5,13 +5,18 @@ namespace Cubist\Util\CommandLine;
 use Cubist\PDF\PDFTools;
 use Cubist\Util\CommandLine;
 use Cubist\Util\Files\Files;
+use Cubist\Util\Graphics\Image;
 use Cubist\Util\Math;
+use Illuminate\Support\Facades\Log;
 
 class Poppler
 {
 
     const RESOLUTION_FACTOR = 72;
 
+    /**
+     * @throws \Exception
+     */
     public static function extractArea($file, $page, $rect, $to = null, $options = array(), $cache = null)
     {
         if (null === $to && null === $cache) {
@@ -65,12 +70,13 @@ class Poppler
         }
 
         if ($options['format'] === 'svg') {
-            $pdftocairo = new CommandLine('pdftocairo');
-            $pdftocairo->setArg('-svg');
-            $pdftocairo->setArg('-paperw', round($rect['width']));
-            $pdftocairo->setArg('-paperh', round($rect['height']));
-            self::_crop($pdftocairo, $page, $rect, 1, 1);
-            self::_exec($pdftocairo, $file, $dest . '.' . $extension);
+            $destfile = $dest . '.' . $extension;
+            self::_pdftocairoRect($file, $page, $destfile, $rect);
+            $dim = Image::getimagesize($destfile);
+            // If result image direction is reverted (cf. bug #6094)
+            if (($dim[0] > $dim[1] && $rect['width'] < $rect['height']) || ($dim[1] > $dim[0] && $rect['height'] < $rect['width'])) {
+                self::_pdftocairoRect($file, $page, $destfile, $rect, true);
+            }
             if (!$options['background']) {
                 PDFTools::makeTextSVGFile($dest . '.' . $extension, $dest . '.' . $extension);
             }
@@ -105,6 +111,16 @@ class Poppler
         return $res;
     }
 
+    protected static function _pdftocairoRect($file, $page, $destfile, $rect, $invertPaper = false)
+    {
+        $pdftocairo = new CommandLine('pdftocairo');
+        $pdftocairo->setArg('-svg');
+        $pdftocairo->setArg('-paperw', round($invertPaper ? $rect['height'] : $rect['width']));
+        $pdftocairo->setArg('-paperh', round($invertPaper ? $rect['width'] : $rect['height']));
+        self::_crop($pdftocairo, $page, $rect, 1, 1);
+        self::_exec($pdftocairo, $file, $destfile);
+    }
+
     /**
      * @param $cli CommandLine
      * @param $page int