diff --git a/app/Http/Controllers/Bo/ProductsController.php b/app/Http/Controllers/Bo/ProductsController.php index 6a4c0b1..81e430d 100644 --- a/app/Http/Controllers/Bo/ProductsController.php +++ b/app/Http/Controllers/Bo/ProductsController.php @@ -15,6 +15,7 @@ use App\Models\TaxRate; use App\Models\XmlInco; use Illuminate\Http\Request; use Illuminate\Support\Str; +use Illuminate\Support\Facades\File; class ProductsController extends Controller { @@ -190,7 +191,7 @@ class ProductsController extends Controller */ public function edit($id) { - $product = Product::findOrFail($id); + $product = Product::findOrFail($id); if(!empty($product->product_type)) { $product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type); //above lines equivalent to : @@ -218,7 +219,6 @@ class ProductsController extends Controller /** * update an existing product - * @todo : enregistrer unité de vente, poids et unité de poids */ public function update(Request $request, $id) { @@ -429,6 +429,152 @@ class ProductsController extends Controller } } + /** + * prepare Inco Images + */ + public function prepareIncoImages(Request $request) { + $picture = $request->picture; + + $preparedImages = array(); + + foreach ($picture as $incoImage) { + //charge + if(File::exists(storage_path($incoImage))) { + $imagePath = storage_path($incoImage); + //redimensionne + //create mask + //Resize original image into mask + $this->resizeImg( + $imagePath, + config('theme.product_image_large_maxwidth'), + config('theme.product_image_large_maxheight'), + File::name($incoImage) . "_large.png" + ); + + $this->resizeImg( + $imagePath, + config('theme.product_image_maxwidth'), + config('theme.product_image_maxheight'), + File::name($incoImage) . "_small.png" + ); + + + $preparedImages[] = "inco/images/" . File::name($incoImage) . "_large.png"; + } + } + //return array("data"=> $preparedImages); + return view('bo.products.inco-images', compact('preparedImages')); + } + + /** + * redimensionne une image dans un masque + */ + private function resizeImg($imagePath, $maskWidth, $maskHeight, $resizedName) + { + // ---- création du masque + $mask = imagecreatetruecolor($maskWidth, $maskHeight); + imagesavealpha($mask, true); + $color = imagecolorallocatealpha($mask, 0, 0, 0, 127); + imagefill($mask, 0, 0, $color); + + // ---- Récupère le mime type de l'image + $info = getimagesize($imagePath); + $mime = $info['mime']; + + // ---- Défini les fonctions à appeler selon le type de fichier + switch ($mime) { + case 'image/jpeg': + $image_create_func = 'imagecreatefromjpeg'; + break; + + case 'image/png': + $image_create_func = 'imagecreatefrompng'; + break; + default: + return false; + } + + // ---- Chargement de l'image source + $img = $image_create_func($imagePath); + list($imgWidth, $imgHeight) = getimagesize($imagePath); + + // ---- Calcul les nouvelles dimensions de l'image + if($imgWidth > $imgHeight){ + $finalWidth = $maskWidth; + $finalHeight = ($finalWidth*$imgHeight)/$imgWidth; + }else{ + $finalHeight = $maskHeight; + $finalWidth = ($finalHeight*$imgWidth)/$imgHeight; + } + + // ---- Redimensionne l'image source + $resizeImg = imagecreatetruecolor($finalWidth, $finalHeight); + imagealphablending( $resizeImg, false ); + imagesavealpha( $resizeImg, true ); + imagecopyresampled($resizeImg, $img, 0, 0, 0, 0, $finalWidth, $finalHeight, $imgWidth, $imgHeight); + + // ---- Copie l'image redimensionnée dans le mask + imagecopy($mask, $resizeImg, ($maskWidth-$finalWidth)/2, ($maskHeight-$finalHeight)/2, 0, 0, imagesx($resizeImg), imagesy($resizeImg)); + imagealphablending( $mask, false ); + imagesavealpha( $mask, true ); + + //écrit dans storage/app/public + // ---- Créer le fichier final + imagepng($mask, storage_path('app/public/inco/images/' . $resizedName), 5); + + // ---- Libère la mémoire associée aux images manipulées + imagedestroy($img); + imagedestroy($resizeImg); + imagedestroy($mask); + } + + /** + * set une image préparée comme image du produit + */ + public function setIncoImage(Request $request) + { + $imagePath = $request->picture; + + $imagePath = storage_path("app/public/" . $imagePath); + + $product_id = $request->product; + $product = Product::findOrFail($product_id); + + $str_random = Str::random(5); + //default is large image + $finalImageName = $product->id . '_' . $str_random . '_large.' . File::extension($imagePath); + + $productimagespath = public_path('images/produits'); + + if(!File::isDirectory($productimagespath)) { + File::makeDirectory($productimagespath, 0644, true); + } + + File::copy($imagePath, $productimagespath . "/" . $finalImageName); + + //suppression de l'ancien fichier + if ($product->picture_large !== null && file_exists(public_path('images/produits/') . $product->picture_large)) { + unlink(public_path('images/produits/') . $product->picture_large); + } + + $product->picture_large = $finalImageName; + + + //now set small image + $imagePath = str_replace("_large", "_small", $imagePath); + $finalImageName = $product->id . '_' . $str_random . '.' . File::extension($imagePath); + File::copy($imagePath, $productimagespath . "/" . $finalImageName); + + //suppression de l'ancien fichier + if ($product->picture !== null && file_exists(public_path('images/produits/') . $product->picture)) { + unlink(public_path('images/produits/') . $product->picture); + } + + $product->picture = $finalImageName; + + $product->save(); + } + /** * helper method to update additionnal product info * foodProduct diff --git a/resources/views/bo/products/edit.blade.php b/resources/views/bo/products/edit.blade.php index 8f38e5f..749ac37 100644 --- a/resources/views/bo/products/edit.blade.php +++ b/resources/views/bo/products/edit.blade.php @@ -63,29 +63,15 @@ {{$product->name}} @endif - {{-- WIP : use Inco Assets + {{-- variante préparation d'images --}} @if ($product->product_type == "foodProduct") - @if (!empty($product->foodProduct->xmlInco)) - - + @if (!empty($product->foodProduct->xmlInco->picture)) +
- @foreach ($product->foodProduct->xmlInco->medias as $media) -
- -
- - {{$media['filename']}} - - -
-
- - @endforeach +
@endif - @endif --}} + @endif

@@ -243,9 +229,24 @@ @section('scripts') @endsection diff --git a/resources/views/bo/products/inco-images.blade.php b/resources/views/bo/products/inco-images.blade.php new file mode 100644 index 0000000..bfda009 --- /dev/null +++ b/resources/views/bo/products/inco-images.blade.php @@ -0,0 +1,12 @@ +@foreach ($preparedImages as $media) +
+ +
+ + visuel Inco + +
+
+@endforeach diff --git a/routes/web.php b/routes/web.php index aa49ef1..79aff7b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -88,6 +88,8 @@ Route::namespace('App\Http\Controllers\Bo') Route::post('/ajax/delete-creneaux-exception', [\App\Http\Controllers\Bo\MagasinsController::class, 'destroyExceptions']); //recherche d'inco Route::post('/ajax/check-inco-data', [\App\Http\Controllers\Bo\ProductsController::class, 'checkIncoData']); + Route::post('/ajax/get-inco-images', [\App\Http\Controllers\Bo\ProductsController::class, 'prepareIncoImages']); + Route::post('/ajax/set-inco-images', [\App\Http\Controllers\Bo\ProductsController::class, 'setIncoImage']); // Produits //route ajoutée avant la déclaration de route resource sinon la forme /products/{product} l'emporte lors du routage