Skip to content

Commit

Permalink
Updates GenerateFavicons Listener so it uses Storage (#6)
Browse files Browse the repository at this point in the history
for loading and saving the favicons
  • Loading branch information
SanderGo authored Jul 3, 2023
1 parent 6ecff67 commit 9c57926
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Listeners/GenerateFavicons.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\Facades\Artisan;
use Statamic\Events\GlobalSetSaved;
use Statamic\Globals\GlobalSet;
use Illuminate\Support\Facades\Storage;
use Statamic\Facades\URL;

class GenerateFavicons
{
Expand Down Expand Up @@ -38,17 +40,17 @@ public function handle(GlobalSetSaved $event)
$svg = $globals->inDefaultSite()->get('svg');
$background = $globals->inDefaultSite()->get('background');

$this->createThumbnail(public_path('favicons/') . $svg, public_path('favicons/icon-180.png'), 180, 180, $background, 15);
$this->createThumbnail(public_path('favicons/') . $svg, public_path('favicons/icon-512.png'), 512, 512, $background, 15);
$this->createThumbnail(public_path('favicons/') . $svg, public_path('favicons/favicon-16x16.png'), 16, 16, 'transparent', false);
$this->createThumbnail(public_path('favicons/') . $svg, public_path('favicons/favicon-32x32.png'), 32, 32, 'transparent', false);
$this->createThumbnail($svg, 'icon-180.png', 180, 180, $background, 15);
$this->createThumbnail($svg, 'icon-512.png', 512, 512, $background, 15);
$this->createThumbnail($svg, 'favicon-16x16.png', 16, 16, 'transparent', false);
$this->createThumbnail($svg, 'favicon-32x32.png', 32, 32, 'transparent', false);

Artisan::call('cache:clear');
}

private function createThumbnail($import, $export, $width, $height, $background, $border)
{
$svg = file_get_contents($import);
$svg = file_get_contents(URL::makeAbsolute(Storage::disk('favicons')->url($import)));
$svgObj = simplexml_load_string($svg);
$viewBox = explode(' ', $svgObj['viewBox']);
$viewBoxWidth = $viewBox[2];
Expand Down Expand Up @@ -81,7 +83,7 @@ private function createThumbnail($import, $export, $width, $height, $background,
}

$im->setImageFormat('png32');
file_put_contents($export, $im->getImageBlob());
Storage::disk('favicons')->put($export, $im->getImageBlob());
$im->clear();
$im->destroy();
}
Expand Down

0 comments on commit 9c57926

Please sign in to comment.