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 @@
@endif
- {{-- WIP : use Inco Assets
+ {{-- variante préparation d'images --}}
@if ($product->product_type == "foodProduct")
- @if (!empty($product->foodProduct->xmlInco))
-
- Donnée inco
+ @if (!empty($product->foodProduct->xmlInco->picture))
+ Images inco