Skip to content
Snippets Groups Projects
Commit 9f974bd1 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

feat: Integrate Imagick and optimize image handling

- **Dockerfile**: Added conditional installation of Imagick extension, allowing for improved image processing capabilities.
- **ImageTranscodingService.php**: Removed dependency on GdDriver, now exclusively using ImagickDriver for image transcoding.
- **UploadedFilesService.php**: Refactored to utilize Laravel's job dispatching, improving the handling of uploaded picture jobs.
- **UploadedFilesServiceTest.php**: Enhanced test coverage with new test methods and added database and queue fakes for better test isolation.
- **UploadedPicture.php**: Updated to use Imagick driver and added checks for null paths when verifying file existence.
- **UploadedPictureJob.php**: Changed the property visibility of $uploadedPicture to public, aligning with the job's requirements.
parent e45d2c50
Branches
No related tags found
1 merge request!3Ajout interface de création et d'édition de créations (artistiques)
Pipeline #605 passed
......@@ -106,6 +106,18 @@ RUN docker-php-ext-configure gd \
--with-freetype \
&& docker-php-ext-install gd;
###########################################
# Imagick
###########################################
ARG INSTALL_IMAGICK=true
RUN if [ ${INSTALL_IMAGICK} = true ]; then \
apt-get install -y libmagickwand-dev --no-install-recommends \
&& pecl install imagick \
&& docker-php-ext-enable imagick; \
fi
###########################################
# OPcache
###########################################
......
......@@ -13,7 +13,7 @@ class UploadedPictureJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(private readonly UploadedPicture $uploadedPicture) {}
public function __construct(public readonly UploadedPicture $uploadedPicture) {}
public function handle(): void
{
......
......@@ -8,7 +8,7 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Drivers\Imagick\Driver;
class UploadedPicture extends Model
{
......@@ -65,19 +65,19 @@ public function optimize(): void
public function deleteOptimized(): void
{
if (Storage::disk('public')->exists($this->path_thumbnail)) {
if ($this->path_thumbnail && Storage::disk('public')->exists($this->path_thumbnail)) {
Storage::disk('public')->delete($this->path_thumbnail);
}
if (Storage::disk('public')->exists($this->path_medium)) {
if ($this->path_medium && Storage::disk('public')->exists($this->path_medium)) {
Storage::disk('public')->delete($this->path_medium);
}
if (Storage::disk('public')->exists($this->path_big)) {
if ($this->path_big && Storage::disk('public')->exists($this->path_big)) {
Storage::disk('public')->delete($this->path_big);
}
if (Storage::disk('public')->exists($this->path_fullsize)) {
if ($this->path_fullsize && Storage::disk('public')->exists($this->path_fullsize)) {
Storage::disk('public')->delete($this->path_fullsize);
}
}
......
......@@ -2,7 +2,6 @@
namespace App\Services;
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\Encoders\AvifEncoder;
use Intervention\Image\ImageManager;
......@@ -11,7 +10,7 @@ class ImageTranscodingService
{
protected ImageManager $imageManager;
public function __construct(ImagickDriver|GdDriver $driver)
public function __construct(ImagickDriver $driver)
{
$this->imageManager = new ImageManager($driver);
}
......
......@@ -78,8 +78,7 @@ public function storeAndOptimizeUploadedPicture(UploadedFile $uploadedFile): Upl
'path_original' => $folderName.'/'.$fileName,
]);
$uploadedPictureJob = new UploadedPictureJob($uploadedFile);
$uploadedPictureJob->handle();
UploadedPictureJob::dispatch($uploadedFile);
return $uploadedFile;
}
......
......@@ -2,16 +2,22 @@
namespace Tests\Feature\Service;
use App\Jobs\UploadedPictureJob;
use App\Services\UploadedFilesService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class UploadedFilesServiceTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function it_generates_cache_key_correctly()
public function test_it_generates_cache_key_correctly()
{
$service = new UploadedFilesService;
$path = 'images/photos';
......@@ -24,7 +30,7 @@ public function it_generates_cache_key_correctly()
}
#[Test]
public function it_refreshes_file_list_cache_and_returns_file_names()
public function test_it_refreshes_file_list_cache_and_returns_file_names()
{
Storage::fake('public');
$service = new UploadedFilesService;
......@@ -40,7 +46,7 @@ public function it_refreshes_file_list_cache_and_returns_file_names()
}
#[Test]
public function it_clears_existing_cache_when_refreshing_file_list()
public function test_it_clears_existing_cache_when_refreshing_file_list()
{
Storage::fake('public');
$service = new UploadedFilesService;
......@@ -56,4 +62,20 @@ public function it_clears_existing_cache_when_refreshing_file_list()
$this->assertEquals(['file1.jpg'], Cache::get($cacheKey));
}
#[Test]
public function test_it_stores_and_optimizes_an_uploaded_picture()
{
Storage::fake('public');
Queue::fake();
$uploadedFile = UploadedFile::fake()->image('test.jpg');
$service = app(UploadedFilesService::class);
$uploadedPicture = $service->storeAndOptimizeUploadedPicture($uploadedFile);
Storage::disk('public')->assertExists($uploadedPicture->path_original);
Queue::assertPushed(UploadedPictureJob::class);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment