44
55use App \Models \Product ;
66use 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 ;
810use Illuminate \Queue \SerializesModels ;
911use Illuminate \Queue \InteractsWithQueue ;
1012use 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}
0 commit comments