Skip to content

Commit

Permalink
simplify aspect ratio handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor committed Oct 4, 2024
1 parent 2b93a3e commit e1a3bc5
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 31 deletions.
3 changes: 2 additions & 1 deletion app/Http/Resources/Artists/FullPhotoResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function toArray(Request $request): array
{
return [
'placeholder' => $this->resource->placeholder(),
'aspectRatio' => $this->resource->aspectRatio(),
'width' => $this->resource->width,
'height' => $this->resource->height,
'url' => $this->resource->url(),
];
}
Expand Down
12 changes: 0 additions & 12 deletions app/Images/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Models\Artist;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Intervention\Image\Interfaces\ImageInterface;
use Psl\Type;

final class Photo extends Image
{
Expand Down Expand Up @@ -54,17 +53,6 @@ public function crop(): ArtistPhotoCrop
return $this->crop;
}

public function aspectRatio(): ?float
{
if (! $this->width || ! $this->height) {
return null;
}

$t = Type\float();

return $t->coerce($this->width) / $t->coerce($this->height);
}

public static function shouldSaveDimensions(): bool
{
return true;
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Artists/Show.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

{#if artist.photo}
<div
style:width="{(artist.photo.aspectRatio ?? 1) * 10}rem"
style:aspect-ratio="{artist.photo.width} / {artist.photo.height}"
class="overflow-hidden relative flex-none self-center mt-5 mb-2 h-40 rounded-lg shadow-lg sm:my-0 sm:mr-6 -p-px"
>
<div
Expand Down
3 changes: 2 additions & 1 deletion resources/js/types/artists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export type FacePhotoResource = {

export type FullPhotoResource = {
placeholder: string;
aspectRatio: number | null;
width: number | null;
height: number | null;
url: string;
};

Expand Down
16 changes: 0 additions & 16 deletions tests/Unit/Images/PhotoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,6 @@ public function testCropUpdated(): void
Queue::assertPushed(ProcessImage::class);
}

#[TestDox('it can calculate aspect ratio')]
public function testAspectRatio(): void
{
$this->assertSame(
1.6, (new Photo(['width' => 16, 'height' => 10]))->aspectRatio(),
);
}

#[TestDox('aspect ratio is null when dimensions are not set')]
public function testNullAspectRatio(): void
{
$this->assertNull(
(new Photo(['width' => null, 'height' => null]))->aspectRatio(),
);
}

#[TestDox('it can get its artists')]
public function testArtists(): void
{
Expand Down

0 comments on commit e1a3bc5

Please sign in to comment.