]> _ Git - cubist_util.git/commitdiff
wip #6076 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 26 Jun 2023 07:50:59 +0000 (09:50 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 26 Jun 2023 07:50:59 +0000 (09:50 +0200)
src/CommandLine/Poppler.php
src/Graphics/Image.php

index 9b88f3391636685e47fb3f6dd69eb0ebf7b9fd91..d76daaaee49803ddecccc51d589870395fa84295 100644 (file)
@@ -22,7 +22,6 @@ class Poppler
             'quality' => '95',
             'antialias' => true,
             'format' => 'jpeg',
-            'internalformat' => 'png',
             'texts' => true,
             'background' => true,
         );
@@ -30,9 +29,6 @@ class Poppler
 
         $options = array_merge($defaultOptions, $options);
         $options['factor'] = $options['resolution'] / self::RESOLUTION_FACTOR;
-        if ($options['format'] === 'svg') {
-            $options['internalformat'] = 'svg';
-        }
         $extension = self::_format2ext($options['format']);
 
         if (null !== $to) {
@@ -71,8 +67,8 @@ class Poppler
         if ($options['format'] === 'svg') {
             $pdftocairo = new CommandLine('pdftocairo');
             $pdftocairo->setArg('-svg');
-            $pdftocairo->setArg('-paperw',$rect['width']);
-            $pdftocairo->setArg('-paperh',$rect['height']);
+            $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);
             if (!$options['background']) {
@@ -145,91 +141,6 @@ class Poppler
         }
     }
 
-    protected static function _pdftojpeg($file, $to, $nbpages, $options = array())
-    {
-        /*
-         *               pdftoppm version 0.48.0
-Copyright 2005-2016 The Poppler Developers - http://poppler.freedesktop.org
-Copyright 1996-2011 Glyph & Cog, LLC
-Usage: pdftoppm [options] [PDF-file [PPM-file-prefix]]
-  -f <int>                 : first page to print
-  -l <int>                 : last page to print
-  -o                       : print only odd pages
-  -e                       : print only even pages
-  -singlefile              : write only the first page and do not add digits
-  -r <fp>                  : resolution, in DPI (default is 150)
-  -rx <fp>                 : X resolution, in DPI (default is 150)
-  -ry <fp>                 : Y resolution, in DPI (default is 150)
-  -scale-to <int>          : scales each page to fit within scale-to*scale-to pixel box
-  -scale-to-x <int>        : scales each page horizontally to fit in scale-to-x pixels
-  -scale-to-y <int>        : scales each page vertically to fit in scale-to-y pixels
-  -x <int>                 : x-coordinate of the crop area top left corner
-  -y <int>                 : y-coordinate of the crop area top left corner
-  -W <int>                 : width of crop area in pixels (default is 0)
-  -H <int>                 : height of crop area in pixels (default is 0)
-  -sz <int>                : size of crop square in pixels (sets W and H)
-  -cropbox                 : use the crop box rather than media box
-  -mono                    : generate a monochrome PBM file
-  -gray                    : generate a grayscale PGM file
-  -png                     : generate a PNG file
-  -jpeg                    : generate a JPEG file
-  -tiff                    : generate a TIFF file
-  -tiffcompression <string>: set TIFF compression: none, packbits, jpeg, lzw, deflate
-  -freetype <string>       : enable FreeType font rasterizer: yes, no
-  -thinlinemode <string>   : set thin line mode: none, solid, shape. Default: none
-  -aa <string>             : enable font anti-aliasing: yes, no
-  -aaVector <string>       : enable vector anti-aliasing: yes, no
-  -opw <string>            : owner password (for encrypted files)
-  -upw <string>            : user password (for encrypted files)
-  -q                       : don't print any messages or errors
-  -v                       : print copyright and version info
-  -h                       : print usage information
-  -help                    : print usage information
-  --help                   : print usage information
-  -?                       : print usage information
-
-         */
-
-        $defaultOptions = array('resolution' => 150,
-            'quality' => '80',
-            'antialias' => true,
-            'format' => 'jpeg',
-            'internalformat' => 'png');
-
-        $options = array_merge($defaultOptions, $options);
-
-        $pdftoppm = new CommandLine('pdftoppm');
-        $pdftoppm->setArg('r', $options['resolution']);
-        $pdftoppm->setArg('-' . $options['internalformat']);
-        $aa = $options['antialias'] ? 'yes' : 'no';
-        $pdftoppm->setArg('-aa', $aa);
-        $pdftoppm->setArg('-aaVector', $aa);
-        $pdftoppm->setArg('-cropbox');
-        $pdftoppm->setArg('-freetype', (!isset($options['texts']) || $options['texts']) ? 'yes' : 'no');
-        $pdftoppm->setArg(null, $file);
-        $pdftoppm->setArg(null, $to . '/p');
-        $pdftoppm->execute();
-        //$pdftoppm->debug();
-
-
-        $chars = strlen($nbpages);
-        for ($i = 1; $i <= $nbpages; $i++) {
-            $f = $to . '/p-' . Math::fill($i, $chars, '0') . '.' . self::_format2ext($options['internalformat']);
-            $d = $to . '/p' . $i . '.' . self::_format2ext($options['format']);
-
-            if ($options['internalformat'] != $options['format']) {
-                $convert = new Imagemagick();
-                $convert->setSrc($f);
-                $convert->setDest($d);
-                $convert->execute();
-                //$convert->debug();
-            } else {
-                rename($f, $d);
-            }
-            chmod($d, 0777);
-        }
-    }
-
     protected static function _format2ext($format)
     {
         if ($format === 'jpeg') {
index 09c70536ca5a64a5a2bac31ffcd69809939c37ec..7a2c57836d3575af10345c94732cf2b85ab75aaf 100644 (file)
@@ -6,6 +6,7 @@ use Cubist\PDF\PDFTools;
 use Cubist\Util\Animations\OAMAnimation;
 use Cubist\Util\Animations\ZipAnimation;
 use Cubist\Util\Files\Files;
+use Illuminate\Support\Facades\Log;
 
 class Image
 {
@@ -119,10 +120,14 @@ class Image
         if (file_exists($fixed) && filemtime($fixed) >= filemtime($source)) {
             return $fixed;
         }
-        if(file_exists($fixed) && is_link($fixed)){
+        if (file_exists($fixed) && is_link($fixed)) {
             unlink($fixed);
         }
-        $svg = simplexml_load_string(file_get_contents($source));
+        try {
+            $svg = simplexml_load_string(file_get_contents($source), \SimpleXMLElement::class, LIBXML_PARSEHUGE);
+        } catch (\Exception $e) {
+            throw new \Exception('Unable to fix SVG dimensions : Error while loading SVG ' . $source);
+        }
         if (!$svg) {
             copy($source, $fixed);
             return $fixed;