Skip to content

Commit

Permalink
Square orientation user avatar generation for CP header (#5731)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
ncla and jasonvarga authored Apr 14, 2022
1 parent 10eb74e commit e60d423
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion routes/cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
22 changes: 20 additions & 2 deletions src/Auth/HasAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
*/
Expand Down
34 changes: 30 additions & 4 deletions src/Http/Controllers/CP/Assets/ThumbnailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ThumbnailController extends Controller
protected $server;

/**
* @var Generator
* @var ImageGenerator
*/
protected $generator;

Expand All @@ -32,6 +32,11 @@ class ThumbnailController extends Controller
*/
protected $size;

/**
* @var string
*/
protected $orientation;

/**
* @var string
*/
Expand All @@ -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()) {
Expand Down Expand Up @@ -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();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/Auth/HasAvatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit e60d423

Please sign in to comment.