Skip to content

Commit 479beaa

Browse files
committed
Add laravel/socialite package and enhance image processing in ResizeProductImagesJob
1 parent 83860c7 commit 479beaa

File tree

5 files changed

+236
-40
lines changed

5 files changed

+236
-40
lines changed

app/Http/Controllers/Product/ResizeAllProductImageController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public function resizeAllProductImage(){
1515
foreach ($products as $product) {
1616
dispatch(new ResizeProductImagesJob($product));
1717
}
18+
1819
}
1920
}

app/Jobs/ResizeProductImagesJob.php

Lines changed: 83 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use App\Models\Product;
66
use Illuminate\Bus\Queueable;
7-
use Intervention\Image\ImageManagerStatic as Image;
7+
use Illuminate\Support\Facades\Log;
8+
use Intervention\Image\ImageManager;
9+
use Intervention\Image\Drivers\Gd\Driver;
810
use Illuminate\Queue\SerializesModels;
911
use Illuminate\Queue\InteractsWithQueue;
1012
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -14,75 +16,117 @@ class ResizeProductImagesJob implements ShouldQueue
1416
{
1517
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
1618

17-
/**
18-
* Create a new job instance.
19-
*/
19+
protected Product $product;
20+
protected ImageManager $imageManager;
2021

21-
protected $product;
22-
23-
public function __construct(Product $product)
22+
public function __construct(Product $product)
2423
{
2524
$this->product = $product;
25+
// On force Imagick comme driver
26+
$this->imageManager = new ImageManager(new Driver());
2627
}
2728

28-
/**
29-
* Execute the job.
30-
*/
3129
public function handle(): void
3230
{
3331
$this->resizeProductProfile();
3432
$this->resizeVariationImages();
35-
$this->resizeAdditionalImages();
33+
//$this->resizeAdditionalImages();
3634
}
3735

3836
protected function resizeProductProfile()
3937
{
4038
if (!$this->product->product_profile) return;
4139

42-
$originalPath = storage_path('app/public/' . $this->product->product_profile);
43-
$newPath = storage_path('app/public/resized/' . basename($originalPath));
40+
try {
41+
$originalPath = public_path('storage/'.$this->product->product_profile);
42+
43+
if (!is_readable($originalPath)) {
44+
Log::warning("Fichier illisible (permissions): " . $originalPath);
45+
return;
46+
}
47+
if (!file_exists($originalPath)) {
48+
Log::warning("Image profil du produit introuvable : {$originalPath}");
49+
return;
50+
}
4451

45-
// Création dossier si pas existant
46-
if (!file_exists(dirname($newPath))) mkdir(dirname($newPath), 0755, true);
52+
4753

48-
$img = Image::make($originalPath)
49-
->resize(800, 600, fn($c) => $c->aspectRatio()->upsize())
50-
->resizeCanvas(800, 600, 'center', false, '#f8f8f8')
51-
->save($newPath);
54+
$newPath = public_path('storage/resized/products/'.basename($originalPath));
55+
if (!file_exists(dirname($newPath))) mkdir(dirname($newPath), 0755, true);
56+
57+
$image = $this->imageManager->read($originalPath)
58+
->scaleDown(800, 800);
59+
60+
$canvas = $this->imageManager->create(800, 800, '#f8f8f8');
61+
$canvas->place($image, 'center')->save($newPath);
62+
63+
5264

53-
// Mettre à jour le champ product_profile
54-
$this->product->update(['product_profile' => 'resized/' . basename($originalPath)]);
65+
//$this->product->update(['product_profile' => 'resized/products/'.basename($originalPath)]);
66+
} catch (\Throwable $e) {
67+
68+
$imgInfo = @getimagesize($originalPath);
69+
70+
if (!$imgInfo) {
71+
Log::warning("Image illisible : {$originalPath}");
72+
}
73+
Log::warning("Erreur redimensionnement image variation du produit {$this->product->id} : "
74+
. $e->getMessage()
75+
. " | Image : " . ($originalPath ?? 'inconnue'));
76+
}
5577
}
5678

5779
protected function resizeVariationImages()
5880
{
5981
foreach ($this->product->variations as $variation) {
6082
foreach ($variation->images as $imgModel) {
61-
$originalPath = storage_path('app/public/' . $imgModel->image_path);
62-
$newPath = storage_path('app/public/resized/' . basename($originalPath));
63-
64-
$img = Image::make($originalPath)
65-
->resize(800, 600, fn($c) => $c->aspectRatio()->upsize())
66-
->resizeCanvas(800, 600, 'center', false, '#f8f8f8')
67-
->save($newPath);
68-
69-
$imgModel->update(['image_path' => 'resized/' . basename($originalPath)]);
83+
try {
84+
$originalPath = public_path('storage/'.$imgModel->image_path);
85+
if (!file_exists($originalPath)) {
86+
Log::warning("Image variation introuvable : {$originalPath}");
87+
continue;
88+
}
89+
90+
$newPath = public_path('storage/resized/variations/'.basename($originalPath));
91+
if (!file_exists(dirname($newPath))) mkdir(dirname($newPath), 0755, true);
92+
93+
$image = $this->imageManager->read($originalPath)
94+
->scaleDown(800, 800);
95+
96+
$canvas = $this->imageManager->create(800, 800, '#f8f8f8');
97+
$canvas->place($image, 'center')->save($newPath);
98+
99+
//$imgModel->update(['image_path' => 'resized/variations/'.basename($originalPath)]);
100+
} catch (\Throwable $e) {
101+
Log::warning("Erreur redimensionnement image variation du produit {$this->product->id} : " . $e->getMessage());
102+
}
70103
}
71104
}
72105
}
73106

74107
protected function resizeAdditionalImages()
75108
{
76109
foreach ($this->product->images as $imgModel) {
77-
$originalPath = storage_path('app/public/' . $imgModel->image_path);
78-
$newPath = storage_path('app/public/resized/' . basename($originalPath));
79-
80-
$img = Image::make($originalPath)
81-
->resize(800, 600, fn($c) => $c->aspectRatio()->upsize())
82-
->resizeCanvas(800, 600, 'center', false, '#f8f8f8')
83-
->save($newPath);
84-
85-
$imgModel->update(['image_path' => 'resized/' . basename($originalPath)]);
110+
try {
111+
$originalPath = public_path('storage/'.$imgModel->image_path);
112+
if (!file_exists($originalPath)) {
113+
Log::warning("Image additionnelle introuvable : {$originalPath}");
114+
continue;
115+
}
116+
117+
$newPath = public_path('storage/resized/images/'.basename($originalPath));
118+
if (!file_exists(dirname($newPath))) mkdir(dirname($newPath), 0755, true);
119+
120+
$image = $this->imageManager->read($originalPath)
121+
->scaleDown(800, 800);
122+
123+
$canvas = $this->imageManager->create(800, 800, '#f8f8f8');
124+
$canvas->place($image, 'center')->save($newPath);
125+
126+
//$imgModel->update(['image_path' => 'resized/images/'.basename($originalPath)]);
127+
} catch (\Throwable $e) {
128+
Log::warning("Erreur redimensionnement image additionnelle du produit {$this->product->id} : " . $e->getMessage());
129+
}
86130
}
87131
}
88132
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"laravel/framework": "^10.10",
1313
"laravel/passport": "^12.3",
1414
"laravel/sanctum": "^3.3",
15+
"laravel/socialite": "*",
1516
"laravel/tinker": "^2.8",
1617
"notchpay/notchpay-php": "^1.6",
1718
"predis/predis": "^2.3",

composer.lock

Lines changed: 149 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
/*
164164
* Application Service Providers...
165165
*/
166+
Intervention\Image\ImageServiceProvider::class,
166167
App\Providers\AppServiceProvider::class,
167168
App\Providers\AuthServiceProvider::class,
168169
App\Providers\BroadcastServiceProvider::class,
@@ -182,6 +183,7 @@
182183
*/
183184

184185
'aliases' => Facade::defaultAliases()->merge([
186+
'Image' => Intervention\Image\Facades\Image::class
185187
// 'Example' => App\Facades\Example::class,
186188
])->toArray(),
187189

0 commit comments

Comments
 (0)