From: Vincent Vanwaelscappel Date: Mon, 4 Sep 2023 15:16:16 +0000 (+0200) Subject: wait #6094 @5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=8687dd873682a0dd603ebaf5fb75ec351b864535;p=cubist_util.git wait #6094 @5 --- diff --git a/src/CommandLine/Poppler.php b/src/CommandLine/Poppler.php index d76daaa..bc55359 100644 --- a/src/CommandLine/Poppler.php +++ b/src/CommandLine/Poppler.php @@ -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