Skip to content

Commit

Permalink
feat: add api route to create thumbnail (glide) given a preset and im…
Browse files Browse the repository at this point in the history
…age asset id
  • Loading branch information
wanze committed Apr 26, 2023
1 parent 0c1a7b7 commit 2452261
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ A Statamic addon providing common functionality and fixes among all Boilerplate

* Provides a ``{{ boilerplate:version }}`` tag to output the version from the root `composer.json`.
* Offers Statamic control panel logins via magic links from our portal.
* Provides a route `/api/thumbnail/{preset}/{imageAssetId}` to output a thumbnail (via Glide)
using the given asset preset and id.
* Multi language extensions
* Always sets the entry of the default site as root of a newly created entry.
* When creating a translation, all data is copied from the originated entry. Note: Only works when *Origin Behaviour* of
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Gridonic\StatamicBoilerplateAddon\Http\Controllers\ThumbnailFromPresetController;
use Gridonic\StatamicBoilerplateAddon\Http\Controllers\MagicLinkController;
use Illuminate\Support\Facades\Route;

if (config('statamic.boilerplate.magic_links_enabled', false)) {
Route::get('/portal-login/{token}', [MagicLinkController::class, 'login']);
}
Route::get('/api/thumbnail/{preset}/{imageAssetId}', [ThumbnailFromPresetController::class, 'thumbnail']);
37 changes: 37 additions & 0 deletions src/Http/Controllers/ThumbnailFromPresetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Gridonic\StatamicBoilerplateAddon\Http\Controllers;

use Illuminate\Routing\Controller;
use Statamic\Facades\Antlers;
use Statamic\Facades\Asset;

class ThumbnailFromPresetController extends Controller
{
public function thumbnail(string $preset, string $imageAssetId) {
if (!$this->isPreset($preset)) {
abort(404);
}

if (!$this->imageExists($imageAssetId)) {
abort(404);
}

$imageManipulationUrl = Antlers::parse(sprintf('{{ glide src="%s" preset="%s" }}', $imageAssetId, $preset));

return redirect($imageManipulationUrl, 301);
}

private function imageExists(string $imageId): bool
{
return Asset::find($imageId) !== null;
}

private function isPreset(string $preset): bool
{
$presets = config('statamic.assets.image_manipulation.presets', []);
$presetNames = array_keys($presets);

return in_array($preset, $presetNames);
}
}

0 comments on commit 2452261

Please sign in to comment.