From c5e0b1696a33c676352af344909cf96555ef63f9 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 29 Aug 2019 12:06:37 +0200 Subject: [PATCH] wip #2965 @0.5 --- composer.json | 3 ++- .../Http/Controllers/CubistPWAController.php | 27 ++++++++++++++++--- src/app/Magic/Models/Settings.php | 2 ++ src/app/Magic/PageData.php | 7 +++++ src/resources/views/favicon.blade.php | 2 +- src/routes/cubist/backpack/pwa.php | 1 + 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 80e8636..3c49936 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "barryvdh/laravel-debugbar": "^3.2", "league/commonmark-ext-autolink": "^1.0", "cviebrock/laravel-elasticsearch": "^3.6", - "spatie/laravel-honeypot": "^1.3" + "spatie/laravel-honeypot": "^1.3", + "chrisjean/php-ico": "^1.0" }, "require-dev": { diff --git a/src/app/Http/Controllers/CubistPWAController.php b/src/app/Http/Controllers/CubistPWAController.php index e943f4b..fd46f64 100644 --- a/src/app/Http/Controllers/CubistPWAController.php +++ b/src/app/Http/Controllers/CubistPWAController.php @@ -4,28 +4,49 @@ namespace Cubist\Backpack\app\Http\Controllers; use Illuminate\Http\Request; +use PHP_ICO; class CubistPWAController extends CubistFrontController { public static $androidSizes = [36, 48, 72, 96, 144, 192, 256, 512, 1024]; public static $iosSizes = [57, 72, 76, 114, 120, 144, 152, 167, 182]; - public static $faviconSizes = [16, 32,48,96,144, 192,256,384,512]; + public static $faviconSizes = [16, 32, 48, 96, 144, 192, 256, 384, 512]; public function manifest(Request $request) { $icons = []; + foreach (self::$androidSizes as $size) { + $icons[] = [ + 'sizes' => $size . 'x' . $size, + 'type' => 'image/png', + 'src' => $this->data['global']->getImageUrl('favicon_android', 'favicon_android_' . $size), + 'density' => round($size / 48, 3), + ]; + } $res = ['name' => $this->data['global']->pwa_name, 'short_name' => $this->data['global']->pwa_short_name, 'description' => '', 'display' => 'standalone', 'orientation' => 'any', - 'background-color' => $this->data['global']->pwa_theme_color, - 'theme-color' => $this->data['global']->pwa_theme_color, + 'background_color' => $this->data['global']->pwa_theme_color, + 'theme_color' => $this->data['global']->pwa_theme_color, 'start_url' => '/', 'scope' => '/', 'icons' => $icons]; return response(json_encode($res))->header('Content-Type', 'application/manifest+json'); } + + public function favicon() + { + $source = $this->data['global']->getImageFile('favicon_android', 'favicon_144'); + $dest = storage_path('favicon.ico'); + dd($dest); + if (!file_exists($dest) || filemtime($source) > filemtime($dest)) { + $ico_lib = new PHP_ICO($source, array(array(16, 16), array(32, 32), array(64, 64), array(128, 128))); + $ico_lib->save_ico($dest); + } + return response(file_get_contents($dest))->header('Content-Type','text/html'); + } } diff --git a/src/app/Magic/Models/Settings.php b/src/app/Magic/Models/Settings.php index 83cd986..9e5e446 100644 --- a/src/app/Magic/Models/Settings.php +++ b/src/app/Magic/Models/Settings.php @@ -17,6 +17,8 @@ class Settings extends CubistMagicModel public function registerMediaConversions(Media $media = null) { + parent::registerMediaConversions($media); + foreach (CubistPWAController::$androidSizes as $size) { $this->addMediaConversion('favicon_android_' . $size) ->width($size) diff --git a/src/app/Magic/PageData.php b/src/app/Magic/PageData.php index f148a6e..8d53434 100644 --- a/src/app/Magic/PageData.php +++ b/src/app/Magic/PageData.php @@ -240,6 +240,13 @@ class PageData implements \ArrayAccess return count($this->getMedia($offset)) > 0; } + public function getImageFile($offset, $conversionName = '') + { + /** @var Media $media */ + $media = $this->getMedia($offset)->get(0); + return $media->getPath($conversionName); + } + public function getMediaURL($offset, $default = null) { return $this->getImageURL($offset, '', $default); diff --git a/src/resources/views/favicon.blade.php b/src/resources/views/favicon.blade.php index e14cdaf..6440f38 100644 --- a/src/resources/views/favicon.blade.php +++ b/src/resources/views/favicon.blade.php @@ -2,5 +2,5 @@ @endforeach @foreach(Cubist\Backpack\app\Http\Controllers\CubistPWAController::$iosSizes as $size) - + @endforeach diff --git a/src/routes/cubist/backpack/pwa.php b/src/routes/cubist/backpack/pwa.php index b0228fe..4265a0c 100644 --- a/src/routes/cubist/backpack/pwa.php +++ b/src/routes/cubist/backpack/pwa.php @@ -1,2 +1,3 @@