From e60d423ff70709e62f18aa0d7bfc1c82ff206d28 Mon Sep 17 00:00:00 2001 From: ncla Date: Thu, 14 Apr 2022 19:44:09 +0300 Subject: [PATCH] Square orientation user avatar generation for CP header (#5731) Co-authored-by: Jason Varga --- routes/cp.php | 2 +- src/Auth/HasAvatar.php | 22 ++++++++++-- .../CP/Assets/ThumbnailController.php | 34 ++++++++++++++++--- tests/Auth/HasAvatarTest.php | 6 ++-- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/routes/cp.php b/routes/cp.php index cb10e4be25..48e1818670 100644 --- a/routes/cp.php +++ b/routes/cp.php @@ -141,7 +141,7 @@ Route::get('assets-fieldtype', 'FieldtypeController@index'); Route::resource('assets', 'AssetsController')->parameters(['assets' => 'encoded_asset']); Route::get('assets/{encoded_asset}/download', 'AssetsController@download')->name('assets.download'); - Route::get('thumbnails/{encoded_asset}/{size?}', 'ThumbnailController@show')->name('assets.thumbnails.show'); + Route::get('thumbnails/{encoded_asset}/{size?}/{orientation?}', 'ThumbnailController@show')->name('assets.thumbnails.show'); Route::get('svgs/{encoded_asset}', 'SvgController@show')->name('assets.svgs.show'); Route::get('pdfs/{encoded_asset}', 'PdfController@show')->name('assets.pdfs.show'); }); diff --git a/src/Auth/HasAvatar.php b/src/Auth/HasAvatar.php index c9b7c1aa6e..51bddd5d74 100644 --- a/src/Auth/HasAvatar.php +++ b/src/Auth/HasAvatar.php @@ -10,11 +10,11 @@ trait HasAvatar /** * Get a user's avatar URL. * - * Could be an asset's URL through a field named avatar, a Gravatar URL, or null. + * Could be an asset thumbnail URL through a field named avatar, a Gravatar URL, or null. */ public function avatar($size = 64) { - if ($this->hasAvatarField() && ($url = $this->avatarFieldUrl())) { + if ($this->hasAvatarField() && ($url = $this->avatarFieldSquareThumbnailUrl())) { return $url; } @@ -50,6 +50,24 @@ public function avatarFieldUrl() return optional($this->avatarFieldValue()->value())->url(); } + /** + * Square thumbnail URL of the avatar from the asset field. + */ + public function avatarFieldSquareThumbnailUrl() + { + $assetId = optional($this->avatarFieldValue()->value())->id(); + + if (! $assetId) { + return null; + } + + return cp_route('assets.thumbnails.show', [ + 'encoded_asset' => base64_encode($assetId), + 'size' => 'small', + 'orientation' => 'square', + ]); + } + /** * The Gravatar URL. */ diff --git a/src/Http/Controllers/CP/Assets/ThumbnailController.php b/src/Http/Controllers/CP/Assets/ThumbnailController.php index bc7f7d2c0c..6657670936 100644 --- a/src/Http/Controllers/CP/Assets/ThumbnailController.php +++ b/src/Http/Controllers/CP/Assets/ThumbnailController.php @@ -18,7 +18,7 @@ class ThumbnailController extends Controller protected $server; /** - * @var Generator + * @var ImageGenerator */ protected $generator; @@ -32,6 +32,11 @@ class ThumbnailController extends Controller */ protected $size; + /** + * @var string + */ + protected $orientation; + /** * @var string */ @@ -52,11 +57,13 @@ public function __construct(Server $server, ImageGenerator $generator) * * @param string $asset * @param string $size + * @param string $orientation * @return \Illuminate\Http\Response */ - public function show($asset, $size = null) + public function show($asset, $size = null, $orientation = null) { $this->size = $size; + $this->orientation = $orientation; $this->asset = $this->asset($asset); if ($placeholder = $this->getPlaceholderResponse()) { @@ -107,9 +114,28 @@ private function generate() return $path; } - public function getPreset() + /** + * Get control panel thumbnail image preset name. + * + * Statamic has few control panel specific image presets + * + * @see \Statamic\Imaging\Manager::cpManipulationPresets + * + * @return string + */ + private function getPreset() + { + return "cp_thumbnail_{$this->size}_{$this->getOrientation()}"; + } + + /** + * Get orientation override from URL path or directly from asset. + * + * @return string|null + */ + private function getOrientation() { - return "cp_thumbnail_{$this->size}_{$this->asset->orientation()}"; + return $this->orientation ?? $this->asset->orientation(); } /** diff --git a/tests/Auth/HasAvatarTest.php b/tests/Auth/HasAvatarTest.php index d75958dd9f..f895dd89b1 100644 --- a/tests/Auth/HasAvatarTest.php +++ b/tests/Auth/HasAvatarTest.php @@ -76,8 +76,10 @@ public function it_gets_the_avatar_if_theres_a_field_defined_in_the_blueprint_an { $user = $this->withAvatarField()->withGravatar()->userWithUploadedAvatar(); - $this->assertEquals('/avatars/john.jpg', $user->avatar()); - $this->assertEquals('/avatars/john.jpg', $user->avatar(64)); + $this->assertEquals('/avatars/john.jpg', $user->avatarFieldUrl()); + $this->assertEquals('/avatars/john.jpg', $user->avatarFieldUrl(64)); + $this->assertEquals('http://localhost/cp/thumbnails/YXZhdGFyczo6am9obi5qcGc=/small/square', $user->avatar()); + $this->assertEquals('http://localhost/cp/thumbnails/YXZhdGFyczo6am9obi5qcGc=/small/square', $user->avatar(64)); $this->assertEquals('https://www.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=64', $user->gravatarUrl()); $this->assertEquals('https://www.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=64', $user->gravatarUrl(64)); $this->assertEquals('https://www.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=128', $user->gravatarUrl(128));