From: Vincent Vanwaelscappel Date: Thu, 2 Mar 2023 21:29:18 +0000 (+0100) Subject: wip #5769 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=f3e1d3f0d1d86550357308ca1b84935e472e6419;p=cubist_util.git wip #5769 @1 --- diff --git a/composer.json b/composer.json index 1354163..2cc5b85 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "ext-sodium": "*", "laravel/framework": "~5.8|^6.0|^7.0|^8.0", "cubist/net": "dev-master", - "dpb587/microdata-dom": "dev-master" + "dpb587/microdata-dom": "dev-master", + "cubist/pdf": "dev-master" } } diff --git a/composer.lock b/composer.lock index d81c605..278a444 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "93cb873c086aa9bc665faf4289e64fce", + "content-hash": "f353439092103527932eadf9bc1faa09", "packages": [ { "name": "brick/math", @@ -100,6 +100,56 @@ "description": "net cubist composer package", "time": "2023-01-27T14:43:25+00:00" }, + { + "name": "cubist/pdf", + "version": "dev-master", + "source": { + "type": "git", + "url": "git://git.cubedesigners.com/cubist_pdf.git", + "reference": "97b5e96140945c11230ab5dc48219e30d8a6f3c2" + }, + "dist": { + "type": "tar", + "url": "https://composer.cubedesigners.com/dist/cubist/pdf/cubist-pdf-dev-master-426fa1.tar", + "reference": "97b5e96140945c11230ab5dc48219e30d8a6f3c2", + "shasum": "2313cfd88752a2ef63f0a94ef27bf636c488ff1c" + }, + "require": { + "cubist/util": "dev-master", + "ext-dom": "*", + "ext-json": "*", + "ext-libxml": "*", + "laravel/framework": "~5.8|^6.0|^7.0|^8.0", + "php": ">=7.4" + }, + "default-branch": true, + "type": "library", + "extra": { + "laravel": { + "providers": [] + } + }, + "autoload": { + "psr-4": { + "Cubist\\PDF\\": "src" + } + }, + "license": [ + "proprietary" + ], + "authors": [ + { + "name": "Vincent Vanwaelscappel", + "email": "vincent@cubedesigners.com" + } + ], + "description": "PDF", + "keywords": [ + "cubist", + "pdf" + ], + "time": "2023-03-02T16:43:11+00:00" + }, { "name": "dflydev/dot-access-data", "version": "dev-main", @@ -4522,7 +4572,8 @@ "minimum-stability": "dev", "stability-flags": { "cubist/net": 20, - "dpb587/microdata-dom": 20 + "dpb587/microdata-dom": 20, + "cubist/pdf": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/src/Graphics/Image.php b/src/Graphics/Image.php index 2550404..4046557 100644 --- a/src/Graphics/Image.php +++ b/src/Graphics/Image.php @@ -2,6 +2,7 @@ namespace Cubist\Util\Graphics; +use Cubist\PDF\PDFTools; use Cubist\Util\Animations\OAMAnimation; use Cubist\Util\Animations\ZipAnimation; use Cubist\Util\Files\Files; @@ -23,13 +24,16 @@ class Image throw new \Exception('File ' . $path . ' does not exist'); } - $allowedExtensions = ['svg', 'oam', 'zip', 'jpeg', 'jpg', 'gif', 'png', 'webm']; + $allowedExtensions = ['svg', 'oam', 'zip', 'jpeg', 'jpg', 'gif', 'png', 'webm', 'pdf']; $ext = Files::getExtension($path); if (!in_array($ext, $allowedExtensions)) { throw new \Exception('File ' . $path . ' is not a valid image'); } - if ($ext === 'svg') { + if ($ext === 'pdf') { + $i = PDFTools::infos($path); + $res = $i['infos']['size']; + } else if ($ext === 'svg') { $svg = simplexml_load_string(file_get_contents($path)); $attr = $svg->attributes(); if (!isset($attr['width']) || !isset($attr['height'])) { diff --git a/src/Graphics/Resizer.php b/src/Graphics/Resizer.php index 44981bd..e929bbf 100644 --- a/src/Graphics/Resizer.php +++ b/src/Graphics/Resizer.php @@ -178,6 +178,33 @@ class Resizer return $color; } + public static function keepRatio($naturalWidth, $naturalHeight, $forcedWidth = null, $forcedHeight = null, $contains = true) + { + $w = $naturalWidth; + $h = $naturalHeight; + if (!$forcedHeight && !$forcedWidth) { + return [$w, $h]; + } + + $ratio = $naturalWidth / $naturalHeight; + if (!$forcedWidth) { + $forcedWidth = $naturalWidth; + } + if (!$forcedHeight) { + $forcedHeight = $naturalHeight; + } + $s1 = $forcedWidth / $naturalWidth; + $s2 = $forcedHeight / $naturalHeight; + if ($contains) { + $scale = min($s1, $s2); + } else { + $scale = max($s1, $s2); + } + $w = $naturalWidth * $scale; + $h = $naturalHeight * $scale; + return [$w, $h]; + } + }