--- /dev/null
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\FluidbookPublication;
+use Cubist\Backpack\Console\Commands\CubistCommand;
+use Cubist\Util\ArrayUtil;
+use Cubist\Util\PHP;
+
+class FluidbookSocialImage extends CubistCommand
+{
+ protected $signature = 'fluidbook:socialimage {id} {--force}';
+ protected $description = 'Generate image for social networks';
+
+ /**
+ * @throws \Exception
+ */
+ public function handle()
+ {
+ PHP::neverStop(true);
+
+ $ids = ArrayUtil::parseRange($this->argument('id'));
+ $fluidbooks = FluidbookPublication::withoutGlobalScopes()->where('created_ok', '1')->whereIn('id', $ids)->get();
+ foreach ($fluidbooks as $fluidbook) {
+ $this->line('Generate social image for ' . $fluidbook->id);
+ \App\Jobs\FluidbookSocialImage::dispatchSync($fluidbook, $this->option('force'));
+ }
+ }
+}
use Cubist\Util\Files\Files;
use Cubist\Util\Graphics\Image;
-class SocialImage{
+class SocialImage
+{
/**
* @param $fluidbook FluidbookPublication
* @return string
*/
- public static function createSocialImage($fluidbook)
+ public static function createSocialImage($fluidbook, $force = false)
{
$id = $fluidbook->id;
$wid = $fluidbook->getAssetDir() . '/';
$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) {
+ if ($force || 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']);
+ $force = $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', 'path' => 'index.html']);
$w = 1200;
$h = 628;
$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('delay', 2);
$cl->setArg('scale', 0.5);
$cl->setArg('dest', $res);
- $cl->setArg('url', '"'.$url.'"');
+ $cl->setArg('url', '"' . $url . '"');
$cl->execute();
unlink($lock);
+
+ self::getSocialImageSize($fluidbook);
}
}
}
*/
public static function getSocialImageSize($fluidbook)
{
- $socialImage = self::createSocialImage($fluidbook);
- if(!file_exists($socialImage)){
+ $socialImage = self::socialImagePath($fluidbook->id);
+ if (!file_exists($socialImage)) {
return null;
}
$sizeFile = self::socialImagePath($fluidbook->id, 'size');
--- /dev/null
+<?php
+
+namespace App\Jobs;
+
+use App\Fluidbook\SocialImage;
+use App\Models\FluidbookPublication;
+
+class FluidbookSocialImage extends Base
+{
+
+
+ /**
+ * @var FluidbookPublication
+ */
+ protected $fluidbook;
+
+ /**
+ * @var bool
+ */
+ protected $force;
+
+ /**
+ * @param $id int|FluidbookPublication
+ * @param $force bool
+ * @throws \Exception
+ */
+
+ public function __construct($id, $force = false)
+ {
+ if ($id instanceof FluidbookPublication) {
+ $this->fluidbook = $id;
+ $id = $this->fluidbook->id;
+ } else {
+ $this->fluidbook = FluidbookPublication::withoutGlobalScopes()->find($id);
+ }
+ if (!$this->fluidbook) {
+ throw new \Exception('Unable to generate social image for fluidbook ' . $id . ' : fluidbook not found');
+ }
+ $this->force = $force;
+ }
+
+ public function uniqueId()
+ {
+ return $this->fluidbook->id;
+ }
+
+ public function handle()
+ {
+ SocialImage::createSocialImage($this->fluidbook, $this->force);
+ }
+
+}
use App\Http\Controllers\Admin\Operations\FluidbookPublication\Services\SocialImageOperation;
use App\Http\Controllers\Admin\Operations\FluidbookPublication\StatsOperation;
use App\Jobs\FluidbookImagesPreprocess;
+use App\Jobs\FluidbookSocialImage;
use App\Models\Base\ToolboxSettingsModel;
use App\Models\Traits\CheckHash;
use App\Models\Traits\PublicationSettings;
if ($this->_compositionUpdated) {
FluidbookImagesPreprocess::dispatch($this->id);
}
+ FluidbookSocialImage::dispatch($this->id);
return parent::onSaved();
}
const optionDefinitions = [
{name: 'url', type: String},
{name: 'dest', type: String},
- {name: 'delay', type: Number, defaultOption: 10},
+ {name: 'delay', type: Number, defaultOption: 2},
{name: 'scale', type: Number, defaultOption: 1},
{name: 'width', type: Number, defaultOption: 1920},
{name: 'height', type: Number, defaultOption: 1080}
deviceScaleFactor: options.scale,
});
await page.setDefaultNavigationTimeout(0);
- await page.goto(options.url);
+ await page.goto(options.url, {waitUntil: 'networkidle2'});
await new Promise(r => setTimeout(r, 1000 * options.delay));
await page.screenshot({path: options.dest, type: 'jpeg', quality: 95});
await browser.close();