]> _ Git - cubist_util.git/commitdiff
wip #4697 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 22 Sep 2021 08:11:14 +0000 (10:11 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 22 Sep 2021 08:11:14 +0000 (10:11 +0200)
src/Animations/OAMAnimation.php [new file with mode: 0644]
src/Animations/ZipAnimation.php [new file with mode: 0644]
src/Graphics/Image.php [new file with mode: 0644]

diff --git a/src/Animations/OAMAnimation.php b/src/Animations/OAMAnimation.php
new file mode 100644 (file)
index 0000000..0f0b4cc
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+
+namespace Cubist\Util\Animations;
+
+use Cubist\Util\Files\Files;
+
+class OAMAnimation
+{
+    public static function unzipOAM($src, $dest = null, $type = 'oam')
+    {
+        if (null === $dest) {
+            $dest = Files::tmpdir();
+        }
+
+        if ($type === 'oam' && file_exists($dest . '/config.xml')) {
+            return $dest;
+        }
+
+        Files::mkdir($dest);
+        $cmd = "unzip $src -d $dest";
+        `$cmd`;
+        return $dest;
+    }
+
+    public static function getOAMData($src)
+    {
+        $unz = self::unzipOAM($src);
+        $config = $unz . 'config.xml';
+        if (!file_exists($config)) {
+            return null;
+        }
+
+        $x = simplexml_load_string(file_get_contents($config));
+        $config = (string)$x->oamfile['src'];
+        $config = $unz . '/' . $config;
+        $x = simplexml_load_string(file_get_contents($config), 'SimpleXMLElement', LIBXML_NOCDATA);
+        $c = Xml::toObject($x);
+
+        $props = array('default-width' => 'width', 'default-height' => 'height', 'html-page' => 'html');
+
+        $res = [];
+        foreach ($c->properties->property as $p) {
+            if (isset($props[$p->_name])) {
+                $res[$props[$p->_name]] = $p->_defaultValue;
+            }
+        }
+        preg_match('|/oam/([0-9a-z]+)/$|', $unz, $m);
+        $res['hash'] = $m[1];
+        $res['url'] = '/files/oam/' . $res['hash'] . '/Assets/' . $res['html'];
+        return $res;
+    }
+}
\ No newline at end of file
diff --git a/src/Animations/ZipAnimation.php b/src/Animations/ZipAnimation.php
new file mode 100644 (file)
index 0000000..2f50773
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+namespace Cubist\Util\Animations;
+
+class ZipAnimation extends OAMAnimation
+{
+    public static function getZIPData($src)
+    {
+        $unz = self::unzipOAM($src,null, 'zip');
+        $index = $unz . '/index.html';
+        if (file_exists($index)) {
+            $c = file_get_contents($index);
+            if (stripos($c, '<div id="lottie"></div>') !== false) {
+                preg_match('/var animationData = (\{.*\})/', $c, $m);
+                $ad = json_decode(trim($m[1]), true);
+                $res['width'] = $ad['w'];
+                $res['height'] = $ad['h'];
+
+            }
+            preg_match('|/zip/([0-9a-z]+)/$|', $unz, $m);
+            $res['hash'] = $m[1];
+            $res['url'] = '/files/zip/' . $res['hash'] . '/index.html';
+            return $res;
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/Graphics/Image.php b/src/Graphics/Image.php
new file mode 100644 (file)
index 0000000..a14ef34
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+namespace Cubist\Util\Graphics;
+
+use Cubist\Util\Animations\OAMAnimation;
+use Cubist\Util\Animations\ZipAnimation;
+use Cubist\Util\Files\Files;
+
+class Image
+{
+    protected static $_imagesizeCache = [];
+
+    public static function getimagesize($path)
+    {
+        if (isset(self::$_imagesizeCache[$path])) {
+            return self::$_imagesizeCache[$path];
+        }
+
+        $ext = Files::getExtension($path);
+        if ($ext === 'svg') {
+            $svg = simplexml_load_string(file_get_contents($path));
+            $attr = $svg->attributes();
+            if (!isset($attr['width']) || !isset($attr['height'])) {
+                $viewbox = trim($attr['viewBox']);
+                $e = explode(' ', $viewbox);
+                $width = (float)$e[2];
+                $height = (float)$e[3];
+            } else {
+                $width = (float)$attr['width'];
+                $height = (float)$attr['height'];
+            }
+            $res = array(
+                $width,
+                $height
+            );
+        } else if ($ext === 'oam') {
+            $oam = OAMAnimation::getOAMData($path);
+            $res = [$oam['width'], $oam['height']];
+        } else if ($ext === 'zip') {
+            $zip = ZipAnimation::getZIPData($path);
+            $res = [$zip['width'], $zip['height']];
+        } else {
+            $res = getimagesize($path);
+        }
+        if (!is_array($res)) {
+            $res = false;
+        } else {
+            $res = array_slice($res, 0, 2, true);
+        }
+        self::$_imagesizeCache[$path] = $res;
+        return $res;
+    }
+}
\ No newline at end of file