Skip to content

Commit 83860c7

Browse files
committed
Add ResizeAllProductImageController and enhance ResizeProductImagesJob for bulk image resizing
This commit introduces the ResizeAllProductImageController, which allows for the bulk resizing of product images by dispatching jobs for each product. Additionally, the ResizeProductImagesJob is enhanced to include methods for resizing product profiles, variation images, and additional images, improving the overall image processing capabilities within the application.
1 parent 8262674 commit 83860c7

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Product;
4+
5+
use App\Models\Product;
6+
use Illuminate\Http\Request;
7+
use App\Http\Controllers\Controller;
8+
use App\Jobs\ResizeProductImagesJob;
9+
10+
class ResizeAllProductImageController extends Controller
11+
{
12+
public function resizeAllProductImage(){
13+
$products = Product::all();
14+
15+
foreach ($products as $product) {
16+
dispatch(new ResizeProductImagesJob($product));
17+
}
18+
}
19+
}

app/Jobs/ResizeProductImagesJob.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace App\Jobs;
44

5+
use App\Models\Product;
56
use Illuminate\Bus\Queueable;
7+
use Intervention\Image\ImageManagerStatic as Image;
8+
use Illuminate\Queue\SerializesModels;
9+
use Illuminate\Queue\InteractsWithQueue;
610
use Illuminate\Contracts\Queue\ShouldQueue;
711
use Illuminate\Foundation\Bus\Dispatchable;
8-
use Illuminate\Queue\InteractsWithQueue;
9-
use Illuminate\Queue\SerializesModels;
1012

1113
class ResizeProductImagesJob implements ShouldQueue
1214
{
@@ -15,16 +17,72 @@ class ResizeProductImagesJob implements ShouldQueue
1517
/**
1618
* Create a new job instance.
1719
*/
18-
public function __construct()
20+
21+
protected $product;
22+
23+
public function __construct(Product $product)
1924
{
20-
//
25+
$this->product = $product;
2126
}
2227

2328
/**
2429
* Execute the job.
2530
*/
2631
public function handle(): void
2732
{
28-
//
33+
$this->resizeProductProfile();
34+
$this->resizeVariationImages();
35+
$this->resizeAdditionalImages();
36+
}
37+
38+
protected function resizeProductProfile()
39+
{
40+
if (!$this->product->product_profile) return;
41+
42+
$originalPath = storage_path('app/public/' . $this->product->product_profile);
43+
$newPath = storage_path('app/public/resized/' . basename($originalPath));
44+
45+
// Création dossier si pas existant
46+
if (!file_exists(dirname($newPath))) mkdir(dirname($newPath), 0755, true);
47+
48+
$img = Image::make($originalPath)
49+
->resize(800, 600, fn($c) => $c->aspectRatio()->upsize())
50+
->resizeCanvas(800, 600, 'center', false, '#f8f8f8')
51+
->save($newPath);
52+
53+
// Mettre à jour le champ product_profile
54+
$this->product->update(['product_profile' => 'resized/' . basename($originalPath)]);
55+
}
56+
57+
protected function resizeVariationImages()
58+
{
59+
foreach ($this->product->variations as $variation) {
60+
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)]);
70+
}
71+
}
72+
}
73+
74+
protected function resizeAdditionalImages()
75+
{
76+
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)]);
86+
}
2987
}
3088
}

routes/api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
use App\Http\Controllers\Admin\Stat\ActiveDeliveryStatController;
8484
use App\Http\Controllers\Payment\Coolpay\Product\PayinController;
8585
use App\Http\Controllers\Product\Payment\SucessPaymentController;
86+
use App\Http\Controllers\Product\ResizeAllProductImageController;
8687
use App\Http\Controllers\User\ControlPaymentStatusByRefController;
8788
use App\Http\Controllers\Admin\Seller\ConfirmStatusSellerController;
8889
use App\Http\Controllers\Payment\Coolpay\Product\CheckPayinController;
@@ -115,6 +116,8 @@
115116

116117
Route::get("catalogue/{shop_key}",[CatalogueController::class,'index']);
117118

119+
Route::get("resize/products",[ResizeAllProductImageController::class,"resizeAllProductImage"]);
120+
118121
Route::get('/filter/products/{arrayId}',[CategoryFilterController::class,'filter']);
119122
Route::get('/filter/categories/{arraySubCategoryId}',[CategoryFilterController::class,'getCategoryBySubCategory']);
120123
//Status for notchpay

0 commit comments

Comments
 (0)