mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 11:14:06 +00:00
146 - Optimizing the Image Size
This commit is contained in:
@@ -24,8 +24,11 @@ class ProcessPosterImage implements ShouldQueue
|
||||
public function handle()
|
||||
{
|
||||
$imageContents = Storage::disk('public')->get($this->concert->poster_image_path);
|
||||
$image = Image::make($imageContents);
|
||||
$image->resize(600)->encode();
|
||||
|
||||
$image = Image::make($imageContents)->resize(600, null, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
})->limitColors(255)->encode();
|
||||
|
||||
Storage::disk('public')->put($this->concert->poster_image_path, (string) $image);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,35 @@ class ProcessPosterImageTest extends TestCase
|
||||
ProcessPosterImage::dispatch($concert);
|
||||
|
||||
$resizedImage = Storage::disk('public')->get('posters/example-poster.png');
|
||||
list($width) = getimagesizefromstring($resizedImage);
|
||||
list($width, $height) = getimagesizefromstring($resizedImage);
|
||||
$this->assertEquals(600, $width);
|
||||
$this->assertEquals(776, $height);
|
||||
|
||||
$resizedImageContents = Storage::disk('public')->get('posters/example-poster.png');
|
||||
$controlImageContents = file_get_contents(base_path('tests/__fixtures__/optimized-poster.png'));
|
||||
$this->assertEquals($controlImageContents, $resizedImageContents);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function it_optimizes_the_poster_image()
|
||||
{
|
||||
Storage::fake('public');
|
||||
Storage::disk('public')->put(
|
||||
'posters/example-poster.png',
|
||||
file_get_contents(base_path('tests/__fixtures__/small-unoptimized-poster.png'))
|
||||
);
|
||||
$concert = \ConcertFactory::createUnpublished([
|
||||
'poster_image_path' => 'posters/example-poster.png',
|
||||
]);
|
||||
|
||||
ProcessPosterImage::dispatch($concert);
|
||||
|
||||
$optimizedImageSize = Storage::disk('public')->size('posters/example-poster.png');
|
||||
$originalSize = filesize(base_path('tests/__fixtures__/small-unoptimized-poster.png'));
|
||||
$this->assertLessThan($originalSize, $optimizedImageSize);
|
||||
|
||||
$optimizedImageContents = Storage::disk('public')->get('posters/example-poster.png');
|
||||
$controlImageContents = file_get_contents(base_path('tests/__fixtures__/optimized-poster.png'));
|
||||
$this->assertEquals($controlImageContents, $optimizedImageContents);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
tests/__fixtures__/optimized-poster.png
Normal file
BIN
tests/__fixtures__/optimized-poster.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 134 KiB |
BIN
tests/__fixtures__/small-unoptimized-poster.png
Normal file
BIN
tests/__fixtures__/small-unoptimized-poster.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 567 KiB |
Reference in New Issue
Block a user