Skip to content

Commit

Permalink
save cover size to db
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor committed Oct 6, 2024
1 parent 01df480 commit 529a999
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 15 deletions.
14 changes: 14 additions & 0 deletions app/Images/Cover.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@
namespace App\Images;

use App\Models\Tale;
use Exception;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Intervention\Image\Interfaces\ImageInterface;

final class Cover extends Image
{
protected $casts = [
'size' => 'int',
];

public function processVariant(ImageInterface $image, string $variant): ImageInterface
{
$size = min($image->width(), $image->height());

return $image->cover($size, $size);
}

public function saveDimensions(int $width, int $height): void
{
if ($width !== $height) {
throw new Exception('Expected tale cover to be square.');
}

$this->size = $width;
}

protected static function pathPrefix(): string
{
return 'covers';
Expand Down
7 changes: 2 additions & 5 deletions app/Images/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ abstract protected static function pathPrefix(): string;

abstract public function processVariant(ImageInterface $image, string $variant): ImageInterface;

abstract public function saveDimensions(int $width, int $height): void;

/**
* @return list<string>
*/
Expand Down Expand Up @@ -178,9 +180,4 @@ public static function disk(): FilesystemAdapter
{
return Storage::disk(config('filesystems.media'));
}

public static function shouldSaveDimensions(): bool
{
return false;
}
}
7 changes: 2 additions & 5 deletions app/Images/Jobs/ProcessImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ public function processVariant(string $variant): void

$this->image->processVariant($image, $variant);

if ($variant === 'default' && $this->image::shouldSaveDimensions()) {
/** @phpstan-ignore property.notFound */
$this->image->width = $image->width();
/** @phpstan-ignore property.notFound */
$this->image->height = $image->height();
if ($variant === 'default') {
$this->image->saveDimensions($image->width(), $image->height());
}

$destinationPath = $this->image->variantPath($variant);
Expand Down
11 changes: 6 additions & 5 deletions app/Images/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public function processVariant(ImageInterface $image, string $variant): ImageInt
return $this->grayscale ? $image->greyscale() : $image;
}

public function saveDimensions(int $width, int $height): void
{
$this->width = $width;
$this->height = $height;
}

protected static function pathPrefix(): string
{
return 'photos';
Expand All @@ -53,11 +59,6 @@ public function crop(): ArtistPhotoCrop
return $this->crop;
}

public static function shouldSaveDimensions(): bool
{
return true;
}

public function artists(): HasMany
{
return $this->hasMany(Artist::class, 'photo_filename', 'filename');
Expand Down
14 changes: 14 additions & 0 deletions database/migrations/2024_10_06_124745_add_size_to_covers_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('covers', function (Blueprint $table) {
$table->integer('size')->nullable();
});
}
};
2 changes: 2 additions & 0 deletions tests/Unit/Images/CoverProcessingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public function testItWorks(): void
{
Cover::disk()->assertExists("covers/original/{$this->filename}");
Cover::disk()->assertMissing("covers/default/{$this->filename}");
$this->assertNull($this->cover->size);
$this->assertNull($this->cover->placeholder());

ProcessImage::dispatchSync($this->cover);

$this->cover->refresh();
Cover::disk()->assertExists("covers/original/{$this->filename}");
Cover::disk()->assertExists("covers/default/{$this->filename}");
$this->assertSame(427, $this->cover->size);
$this->assertStringStartsWith('data:image/svg+xml;base64,', $this->cover->placeholder());
}
}
6 changes: 6 additions & 0 deletions tests/Unit/Images/CoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function testVariants(): void
$this->assertSame(['default'], Cover::variants());
}

#[TestDox('it casts size to integers')]
public function testSizeCast(): void
{
$this->assertSame(2137, (new Cover(['size' => '2137']))->size);
}

#[TestDox('it stores new cover in correct path and dispatches necessary jobs to process it')]
public function testStore(): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Images/PhotoProcessingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function testItWorks(): void
Photo::disk()->assertExists("photos/original/{$this->filename}");
Photo::disk()->assertMissing("photos/default/{$this->filename}");
Photo::disk()->assertMissing("photos/face/{$this->filename}");
$this->assertNull($this->photo->width);
$this->assertNull($this->photo->height);
$this->assertNull($this->photo->placeholder());
$this->assertNull($this->photo->placeholder('face'));

Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Images/TestCover.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public function processVariant(ImageInterface $image, string $variant): ImageInt
return $image->cover($size, $size);
}

public function saveDimensions(int $width, int $height): void
{
}

protected static function pathPrefix(): string
{
return 'covers';
Expand Down

0 comments on commit 529a999

Please sign in to comment.