]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5421 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 29 Aug 2022 18:10:54 +0000 (20:10 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 29 Aug 2022 18:10:54 +0000 (20:10 +0200)
app/Jobs/FluidbookCompiler.php

index ed2fe184ad962fef62462e3f4471ceed8556cdf1..425ab48948aed9a8ef43aaf4238bf713d979db61 100644 (file)
@@ -4,6 +4,7 @@ namespace App\Jobs;
 
 use App\Fluidbook\PDF;
 use App\Http\Controllers\Admin\Operations\FluidbookPublication\Services\SocialImageOperation;
+use App\Http\Controllers\Admin\Operations\Tools\Favicon;
 use App\Models\Signature;
 use App\Fluidbook\SearchIndex;
 use App\Fluidbook\SEO\Document;
@@ -36,6 +37,7 @@ use Fluidbook\Tools\Compiler\CompilerInterface;
 use Fluidbook\Tools\Links\AnchorLink;
 use Fluidbook\Tools\Links\ContentLink;
 use App\Fluidbook\Link\Link;
+use Fluidbook\Tools\SVG\SVGTools;
 use Illuminate\Console\Command;
 use SimpleXMLElement;
 use SplFileInfo;
@@ -44,6 +46,8 @@ class FluidbookCompiler extends Base implements CompilerInterface
 {
     use FluidbookPlayerBranches;
     use \Fluidbook\Tools\Compiler\FluidbookCompiler;
+    use SocialImageOperation;
+    use Favicon;
 
     protected static $uaPrefixes = array('-moz-', '-webkit-', '-o-', '-ms-', '');
 
@@ -354,8 +358,8 @@ class FluidbookCompiler extends Base implements CompilerInterface
         if ($this->themeSettings->iconSet > 15) {
             $this->svgfiles[] = storage_path('icons/' . $this->themeSettings->iconSet . '.svg');
         }
-        if ($this->themeSettings->symbols !== '') {
-            $this->svgfiles[] = $this->themeAsset($this->themeSettings->symbols);
+        if ($symbols = $this->themeAsset('symbols')) {
+            $this->svgfiles[] = $symbols->getPathname();
         }
 
         if ($this->isMobileFirst()) {
@@ -389,18 +393,24 @@ class FluidbookCompiler extends Base implements CompilerInterface
         $this->fluidbookSettings->set($key, $value);
     }
 
-    public function themeAsset($key, $default = null)
+
+    /**
+     * @param $key
+     * @param $default
+     * @return null|SplFileInfo
+     */
+    public function themeAsset($key)
     {
         $collection = $this->theme->{$key};
+
         if ($collection) {
             $path = $this->theme->getFirstMediaPath($collection);
-            if (!$path) {
-                return $default;
+            if (!$path || !file_exists($path)) {
+                return false;
             }
-            return $path;
-        } else {
-            return $default;
+            return new SplFileInfo($path);
         }
+        return false;
     }
 
     /**
@@ -1695,7 +1705,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
             $socialDescription = htmlspecialchars($this->fluidbookSettings->facebook_description ?: $this->fluidbookSettings->seoDescription, ENT_COMPAT);
 
             $socialImage = 'https://toolbox.fluidbook.com/services/socialimage/' . $this->getFluidbook()->cid;
-            $dim = SocialImageOperation::getSocialImageSize($this->getFluidbook());
+            $dim = self::getSocialImageSize($this->getFluidbook());
 
             $socialImageWidth = $dim[0];
             $socialImageHeight = $dim[1];
@@ -1721,40 +1731,19 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
             $this->log('Got index vars 3');
 
             $favicon = '';
-            $hasIos = false;
-            //if ($iosico = $this->checkThemeImage($this->>$this->themeSettings->iosicon)) {
-            //    $hasIos = true;
-            //    $this->vdir->copy($iosico, 'data/apple-touch-icon.png');
-            //    $favicon .= '<link rel="apple-touch-icon" href="data/apple-touch-icon.png" />' . "\n\t";
-            //}
-            if ($this->themeSettings->favicon != '') {
-
-                $pngFile = $this->checkThemeImage($this->themeSettings->favicon);
-                $icoFile = $this->checkThemeImage('favicon.ico');
-
-                if (!file_exists($icoFile) || filemtime($icoFile) < filemtime($pngFile) || filemtime(__FILE__) > filemtime($icoFile)) {
-                    $tmp = Files::tempnam() . '.png';
-                    $convert = "convert $pngFile -resize 64x64^ -gravity center $tmp";
-                    `$convert`;
-
-                    $icotool = new CommandLine('icotool');
-                    $icotool->setArg('c');
-                    $icotool->setArg('o', $icoFile);
-                    $icotool->setArg(null, $tmp);
-                    $icotool->execute();
-
-                    unlink($tmp);
-                }
+            $faviconAsset = $this->themeAsset('favicon');
+            if ($faviconAsset) {
+                $pngFile = $faviconAsset->getPathname();
+                $faviconDir = self::generateFavicons($pngFile);
 
-                $this->vdir->copy($icoFile, 'data/favicon.ico');
+                $this->vdir->copy($faviconDir . '/favicon.ico', 'data/favicon.ico');
                 $this->vdir->copy($pngFile, 'data/favicon.png');
+                $this->vdir->copy($faviconDir . '/apple-touch-icon.png', 'data/apple-touch-icon.png');
 
                 $datapng = 'data:image/png;base64,' . base64_encode(file_get_contents($pngFile));
 
                 $favicon .= '<link rel="icon" type="image/png" href="' . $datapng . '" />' . "\n\t";
-                if (!$hasIos) {
-                    $favicon .= '<link rel="apple-touch-icon" href="data/favicon.png" />';
-                }
+                $favicon .= '<link rel="apple-touch-icon" href="data/apple-touch-icon.png" />';
             }
 
             $print = $this->writePrint();
@@ -1764,18 +1753,18 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
 
             $splash = '';
             $splashstyles = '';
-            $img = $this->fluidbookSettings->splashImage;
-            if ($img) {
-                $this->vdir->copy($this->wdir . '/' . $img, 'data/images/' . $img);
-                $splashstyles = 'background-image:url(' . 'data/images/' . $img . ');background-size:contain;background-position:50% 50%;';
+            $splashImage = $this->themeAsset('splashImage');
+            if ($splashImage) {
+                $this->vdir->copy($splashImage->getPathname(), 'data/images/' . $splashImage->getFilename());
+                $splashstyles = 'background-image:url(' . 'data/images/' . $splashImage->getFilename() . ');background-size:contain;background-position:50% 50%;';
                 if ($this->fluidbookSettings->splashURL !== '') {
                     $splash = '<a href="' . $this->fluidbookSettings->splashURL . '" target="' . $this->fluidbookSettings->splashTarget . '" style="display:block;position:absolute;top:0;left;0;width:100%;height:100%"></a>';
                 }
-            } else if ($ll = $this->checkThemeImage($this->themeSettings->logoLoader)) {
-                $dim = Image::getimagesize($ll);
+            } else if ($logoLoader = $this->themeAsset('logoLoader')) {
+                $dim = Image::getimagesize($logoLoader->getPathname());
                 if ($dim !== false) {
-                    $this->vdir->copy($ll, 'data/images/' . $this->themeSettings->logoLoader);
-                    $splash .= '<div class="logo"><img src="data/images/' . $this->themeSettings->logoLoader . '" width="' . $dim[0] . '" height="' . $dim[1] . '" alt="" /></div>';
+                    $this->vdir->copy($logoLoader->getPathname(), 'data/images/' . $logoLoader->getFilename());
+                    $splash .= '<div class="logo"><img src="data/images/' . $logoLoader->getFilename() . '" width="' . $dim[0] . '" height="' . $dim[1] . '" alt="" /></div>';
                 }
             }
             $svg = $this->_mergeSVG();
@@ -2099,8 +2088,8 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
 
     protected function writeExtras()
     {
-        if ($as = $this->checkThemeImage($this->themeSettings->afterSearch)) {
-            $this->vdir->copy($as, 'data/images/' . $this->themeSettings->afterSearch);
+        if ($afterSearch = $this->themeAsset('afterSearch')) {
+            $this->vdir->copy($afterSearch->getPathname(), $afterSearch->getFilename());
         }
         if ($this->fluidbookSettings->externalArchives != '') {
             $this->addFilesInfos('archives', $this->wdir . '/' . $this->fluidbookSettings->externalArchives);
@@ -2132,7 +2121,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
 
     protected function addSVGSymbolFromFile($svg, $symbolName)
     {
-        $svg = wsTools::optimizeSVG($svg);
+        $svg = SVGTools::optimizeSVG($svg);
 
         $xml = simplexml_load_string(file_get_contents($svg));
         $viewBox = (string)$xml['viewBox'];
@@ -3111,9 +3100,9 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
         // Header
         $header = 'header{';
         $header .= 'height:' . $this->themeSettings->menuHeight . 'px;';
-        if ($mi = $this->checkThemeImage($this->themeSettings->menuImage)) {
-            $this->vdir->copy($mi, 'data/images/' . $this->themeSettings->menuImage);
-            $header .= 'background-image:url(../images/' . $this->themeSettings->menuImage . ');';
+        if ($mi = $this->themeAsset('menuImage')) {
+            $this->vdir->copy($mi->getPathname(), 'data/images/' . $mi->getFilename());
+            $header .= 'background-image:url(../images/' . $mi->getFilename() . ');';
             $header .= 'background-repeat:no-repeat;';
             $header .= 'background-size:100% ' . $this->themeSettings->menuHeight . 'px;';
         } else {
@@ -3125,10 +3114,10 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
         $this->log('CSS 5');
         // Logo
         $logo = '#logo{';
-        if ($l = $this->checkThemeImage($this->themeSettings->logo)) {
-            $this->vdir->copy($l, 'data/images/' . $this->themeSettings->logo);
-            $dim = Image::getimagesize($l);
-            $logo .= 'background-image:url(../images/' . $this->themeSettings->logo . ');width:' . $dim[0] . 'px;height:' . $dim[1] . 'px;';
+        if ($l = $this->themeAsset('logo')) {
+            $this->vdir->copy($l->getPathname(), 'data/images/' . $l->getFilename());
+            $dim = Image::getimagesize($l->getPathname());
+            $logo .= 'background-image:url(../images/' . $l->getFilename() . ');width:' . $dim[0] . 'px;height:' . $dim[1] . 'px;';
         }
         $logo .= '}';
         $res[] = $logo;
@@ -3251,25 +3240,6 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
         $this->log('Write CSS');
     }
 
-    protected function checkThemeImage($path)
-    {
-        $path = trim($path);
-        $path = trim($path, '/');
-        if (!$path) {
-            return false;
-        }
-        $p = $this->themeRoot . '/' . $path;
-        if (file_exists($p)) {
-            return $p;
-        }
-        $po = str_replace('.svg', '.o.svg', $p);
-        if (file_exists($po)) {
-            copy($po, $p);
-            return $p;
-        }
-        return false;
-    }
-
     protected function _writeLess($variables, $lessContents = '')
     {
         if ($this->widget) {
@@ -3351,14 +3321,14 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
                 break;
         }
 
-        if ($bi = $this->checkThemeImage($this->themeSettings->backgroundImage)) {
+        if ($backgroundImage = $this->themeAsset('backgroundImage')) {
 
-            $dbi = Image::getimagesize($bi);
+            $dbi = Image::getimagesize($backgroundImage->getPathname());
             $this->config->backgroundImageDimensions = array('width' => $dbi[0], 'height' => $dbi[1]);
 
 
-            $this->vdir->copy($bi, 'data/images/' . $this->themeSettings->backgroundImage);
-            $body .= 'background-image:url(../images/' . $this->themeSettings->backgroundImage . ');';
+            $this->vdir->copy($backgroundImage->getPathname(), 'data/images/' . $backgroundImage->getFilename());
+            $body .= 'background-image:url(../images/' . $backgroundImage->getFilename() . ');';
             $body .= 'background-position:';
 
             switch ($this->themeSettings->backgroundVAlign) {