]> _ Git - fluidbook-toolbox.git/commitdiff
wait #5971 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 31 May 2023 08:33:57 +0000 (10:33 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 31 May 2023 08:33:57 +0000 (10:33 +0200)
app/Fluidbook/Compiler/Compiler.php
app/Fluidbook/SocialImage.php [new file with mode: 0644]
app/Http/Controllers/Admin/Operations/FluidbookPublication/Services/SocialImageOperation.php

index 2857ab12dbbf583c156b395af70652d5e9e77dca..ffaad09da130fb65f45c8aae870642d9bdbd30b3 100644 (file)
@@ -7,7 +7,7 @@ use App\Fluidbook\PDF;
 use App\Fluidbook\SearchIndex;
 use App\Fluidbook\SEO\Document;
 use App\Fluidbook\SEO\Page;
-use App\Http\Controllers\Admin\Operations\FluidbookPublication\Services\SocialImageOperation;
+use App\Fluidbook\SocialImage;
 use App\Http\Controllers\Admin\Operations\Tools\Favicon;
 use App\Jobs\Base;
 use App\Jobs\FluidbookImagesPreprocess;
@@ -46,7 +46,6 @@ class Compiler extends Base implements CompilerInterface
 {
     use FluidbookPlayerBranches;
     use \Fluidbook\Tools\Compiler\FluidbookCompiler;
-    use SocialImageOperation;
     use Favicon;
 
     protected static $uaPrefixes = array('-moz-', '-webkit-', '-o-', '-ms-', '');
@@ -1088,29 +1087,30 @@ class Compiler extends Base implements CompilerInterface
             $socialTitle = htmlspecialchars($this->fluidbookSettings->facebook_title ?: $titre, ENT_COMPAT);
             $socialDescription = htmlspecialchars($this->fluidbookSettings->facebook_description ?: $this->fluidbookSettings->seoDescription, ENT_COMPAT);
 
-            $socialImage = backpack_url('services/socialimage/' . $this->getFluidbook()->cid);
-            $dim = self::getSocialImageSize($this->getFluidbook());
-
-            $socialImageWidth = $dim[0];
-            $socialImageHeight = $dim[1];
 
             $this->log('Got index vars 2.5');
 
 
             $titre = $this->fluidbookSettings->title;
-
             $description = '<meta name="description" content="' . $this->seo->pages[1]->description . '">';
-
             $twittercard = '<meta name="twitter:title" content="' . $socialTitle . '">
        <meta name="twitter:description" content="' . $socialDescription . '">
-       <meta name="twitter:image" content="' . $socialImage . '">
        <meta name="twitter:site" content="@Fluidbook">
        <meta name="twitter:card" content="summary_large_image">';
             $opengraph = '<meta property="og:title" content="' . $socialTitle . '"/>
-       <meta property="og:description" content="' . $socialDescription . '"/>
-       <meta property="og:image" content="' . $socialImage . '"/>
+       <meta property="og:description" content="' . $socialDescription . '"/>';
+
+            $dim = SocialImage::getSocialImageSize($this->getFluidbook());
+            if ($dim) {
+                $socialImage = backpack_url('services/socialimage/' . $this->getFluidbook()->cid);
+                $socialImageWidth = $dim[0];
+                $socialImageHeight = $dim[1];
+
+                $opengraph .= '        <meta property="og:image" content="' . $socialImage . '"/>
        <meta property="og:image:width" content="' . $socialImageWidth . '"/>
        <meta property="og:image:height" content="' . $socialImageHeight . '"/>';
+                $twittercard .= '      <meta name="twitter:image" content="' . $socialImage . '">';
+            }
 
             $this->log('Got index vars 3');
 
diff --git a/app/Fluidbook/SocialImage.php b/app/Fluidbook/SocialImage.php
new file mode 100644 (file)
index 0000000..50db969
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+
+namespace App\Fluidbook;
+
+use App\Models\FluidbookPublication;
+use Cubist\Util\CommandLine;
+use Cubist\Util\Files\Files;
+use Cubist\Util\Graphics\Image;
+
+class SocialImage{
+
+    /**
+     * @param $fluidbook FluidbookPublication
+     * @return string
+     */
+    public static function createSocialImage($fluidbook)
+    {
+        $id = $fluidbook->id;
+        $wid = $fluidbook->getAssetDir() . '/';
+
+        if ($fluidbook->facebook_image != '') {
+            $c = $wid . $fluidbook->facebook_image;
+            if (file_exists($c)) {
+                $res = $c;
+            }
+        }
+
+        if (!isset($res)) {
+            $res = self::socialImagePath($id);
+            $limit = max(time() - (3600 * 24 * 30), $fluidbook->updated_at->getTimestamp());
+            $minsize = 20 * 1024;
+
+            if (isset($_GET['force']) || !file_exists($res) || filemtime($res) < $limit || filesize($res) < $minsize) {
+                $lock = self::socialImagePath($id, 'lock');
+                if (!file_exists($lock) || filemtime($lock) < time() - 3600) {
+                    touch($lock);
+
+                    $force = (file_exists($res) && filemtime($res) < $limit) || isset($_GET['forcecompile']);
+                    $url = route('fluidbook_preview_with_time', ['version' => 'online', 'id' => $id, 'hash' => $fluidbook->hash, 'time' => time(), 'nointerface' => 1, 'force' => $force ? '1' : '0', 'puppeteer' => '1']);
+
+                    $w = 1200;
+                    $h = 628;
+
+                    $cl = new CommandLine('node');
+                    $cl->setArg(null, resource_path('fluidbookpublication/social_screenshot/social_screenshot.js'));
+                    $cl->setArg('width', $w);
+                    $cl->setArg('height', $h);
+                    $cl->setArg('delay', 10);
+                    $cl->setArg('scale', 0.5);
+                    $cl->setArg('dest', $res);
+                    $cl->setArg('url', '"'.$url.'"');
+                    $cl->execute();
+                    unlink($lock);
+                }
+            }
+        }
+        return $res;
+    }
+
+    /**
+     * @param $fluidbook
+     * @return array
+     * @throws \Exception
+     */
+    public static function getSocialImageSize($fluidbook)
+    {
+        $socialImage = self::createSocialImage($fluidbook);
+        if(!file_exists($socialImage)){
+            return null;
+        }
+        $sizeFile = self::socialImagePath($fluidbook->id, 'size');
+        if (!file_exists($sizeFile) || filemtime($sizeFile) < filemtime($socialImage)) {
+            $res = Image::getimagesize($socialImage);
+            file_put_contents($sizeFile, json_encode($res));
+            return $res;
+        }
+        return json_decode(file_get_contents($sizeFile), true);
+    }
+
+    public static function socialImagePath($id, $ext = 'jpg')
+    {
+        return Files::mkdir(protected_path('fluidbookpublication/socialimage')) . $id . '.' . $ext;
+    }
+}
index f032468422bd146419328396133ebb7c6971b6da..403c4bf79331afce66f935458f7809c42b1f2609 100644 (file)
@@ -2,18 +2,17 @@
 
 namespace App\Http\Controllers\Admin\Operations\FluidbookPublication\Services;
 
+use App\Fluidbook\SocialImage;
 use App\Http\Middleware\CheckIfAdmin;
 use App\Models\FluidbookPublication;
 use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
-use Cubist\Util\CommandLine;
-use Cubist\Util\Files\Files;
-use Cubist\Util\Graphics\Image;
 use Cubist\Util\PHP;
 use Illuminate\Support\Facades\Route;
-use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\F;
+
 
 trait SocialImageOperation
 {
+
     protected function setupSocialimageRoutes($segment, $routeName, $controller)
     {
         foreach (['services', 's'] as $segment) {
@@ -45,11 +44,6 @@ trait SocialImageOperation
         return $this->_socialImage($publication);
     }
 
-    public static function socialImagePath($id, $ext = 'jpg')
-    {
-        return Files::mkdir(protected_path('fluidbookpublication/socialimage')) . $id . '.' . $ext;
-    }
-
     /**
      * @param $fluidbook FluidbookPublication
      * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|object
@@ -59,73 +53,8 @@ trait SocialImageOperation
     {
         PHP::neverStop(true);
 
-        $social = self::createSocialImage($fluidbook);
+        $social = SocialImage::createSocialImage($fluidbook);
         return XSendFileController::sendfile($social);
     }
 
-    /**
-     * @param $fluidbook FluidbookPublication
-     * @return string
-     */
-    public static function createSocialImage($fluidbook)
-    {
-        $id = $fluidbook->id;
-        $wid = $fluidbook->getAssetDir() . '/';
-
-        if ($fluidbook->facebook_image != '') {
-            $c = $wid . $fluidbook->facebook_image;
-            if (file_exists($c)) {
-                $res = $c;
-            }
-        }
-
-        if (!isset($res)) {
-            $res = self::socialImagePath($id);
-            $limit = max(time() - (3600 * 24 * 30), $fluidbook->updated_at->getTimestamp());
-            $minsize = 20 * 1024;
-
-            if (isset($_GET['force']) || !file_exists($res) || filemtime($res) < $limit || filesize($res) < $minsize) {
-                $lock = self::socialImagePath($id, 'lock');
-                if (!file_exists($lock) || filemtime($lock) < time() - 3600) {
-                    touch($lock);
-
-                    $force = (file_exists($res) && filemtime($res) < $limit) || isset($_GET['forcecompile']);
-                    $url = route('fluidbook_preview_with_time', ['version' => 'online', 'id' => $id, 'hash' => $fluidbook->hash, 'time' => time(), 'nointerface' => 1, 'force' => $force ? '1' : '0', 'puppeteer' => '1']);
-
-                    $w = 1200;
-                    $h = 628;
-
-                    $cl = new CommandLine('node');
-                    $cl->setArg(null, resource_path('fluidbookpublication/social_screenshot/social_screenshot.js'));
-                    $cl->setArg('width', $w);
-                    $cl->setArg('height', $h);
-                    $cl->setArg('delay', 10);
-                    $cl->setArg('scale', 0.5);
-                    $cl->setArg('dest', $res);
-                    $cl->setArg('url', '"'.$url.'"');
-                    $cl->execute();
-                    $cl->debug();
-                    unlink($lock);
-                }
-            }
-        }
-        return $res;
-    }
-
-    /**
-     * @param $fluidbook
-     * @return array
-     * @throws \Exception
-     */
-    public static function getSocialImageSize($fluidbook)
-    {
-        $socialImage = self::createSocialImage($fluidbook);
-        $sizeFile = self::socialImagePath($fluidbook->id, 'size');
-        if (!file_exists($sizeFile) || filemtime($sizeFile) < filemtime($socialImage)) {
-            $res = Image::getimagesize($socialImage);
-            file_put_contents($sizeFile, json_encode($res));
-            return $res;
-        }
-        return json_decode(file_get_contents($sizeFile), true);
-    }
 }