ldc-mesbr-app/app/Traits/GenerateBR.php

611 lines
27 KiB
PHP
Raw Permalink Normal View History

2019-02-13 16:37:14 +01:00
<?php
namespace App\Traits;
use App\Models\Campagne;
2021-04-27 17:29:47 +02:00
use App\Models\Customers;
2019-02-13 16:37:14 +01:00
use Illuminate\Http\Request;
use App\Models\Coupon;
2025-08-14 17:05:09 +02:00
use App\Models\PrintedCoupons;
use App\Models\Site;
use App\Models\Statistique;
2021-04-27 17:29:47 +02:00
trait GenerateBR
{
2019-02-13 16:37:14 +01:00
public function generateBase(Request $request, Coupon $coupon)
2021-04-27 17:29:47 +02:00
{
putenv('GDFONTPATH=' . public_path() . '/generation/src/');
2025-04-02 15:14:41 +00:00
$arial = public_path() . '/build/assets/fonts/coupons/arial.ttf';
$arialbd = public_path() . '/build/assets/fonts/coupons/arialbd.ttf';
$impact = public_path() . '/build/assets/fonts/coupons/impact.ttf';
2025-11-13 10:02:49 +01:00
$hand_picture = public_path() . '/build/assets/images/coupons/hand.png';
$mask_img = public_path() . '/build/assets/images/coupons/mask.png';
2019-02-13 16:37:14 +01:00
if (!is_null($request->site_id)) {
$site = Site::where(['id' => $request->site_id])->first();
2019-02-22 09:33:54 +01:00
} else {
$site = $coupon->site()->first();
}
2021-04-27 17:29:47 +02:00
2025-11-13 10:02:49 +01:00
$logo_display = public_path() . '/' . $coupon->coupon_logo_image;
2019-02-13 16:37:14 +01:00
2025-11-13 10:02:49 +01:00
$product_image = public_path() . '/' . $coupon->coupon_decoration_image;
2019-02-13 16:37:14 +01:00
// ---- Masque dans lequel l'image finale sera placée
2025-04-03 09:30:16 +02:00
$mask = imagecreatefrompng($mask_img);
2019-02-13 16:37:14 +01:00
list($maskWidth, $maskHeight) = getimagesize($mask_img);
2019-02-25 10:23:46 +01:00
// ----- Mentions légales + RCS
2021-04-27 17:29:47 +02:00
if ($coupon->coupon_legal_mentions_splitted == 0) {
2025-04-03 09:30:16 +02:00
$txt_mentions = $coupon->coupon_legal_mentions . " " . $coupon->coupon_rcs;
2019-05-20 14:53:09 +02:00
$ImageMentionFond = imagecreate(415, 100);
2025-04-03 09:30:16 +02:00
$white = imagecolorallocate($ImageMentionFond, 255, 255, 255);
2019-05-20 14:53:09 +02:00
imagefilledrectangle($ImageMentionFond, 0, 0, 414, 99, $white);
2025-04-03 09:30:16 +02:00
$black = imagecolorallocate($ImageMentionFond, 0, 0, 0);
$cleanedString = str_replace(["\r", "\n"], '', $txt_mentions);
$lines_mentions = str_split($cleanedString, 92);
2025-04-03 09:30:16 +02:00
$y = 19;
2019-05-20 14:53:09 +02:00
foreach ($lines_mentions as $line) {
2021-04-27 17:29:47 +02:00
$this->imagettftextSp($ImageMentionFond, 7, 0, 15, $y, $black, $arial, $line, 0.01);
$y = $y + 9;
2019-05-20 14:53:09 +02:00
}
$this->imagettftextSp(
$ImageMentionFond,
7,
0,
15,
$y,
$black,
$arialbd,
sprintf("%s : %s", __('Renseignements'), $coupon->coupon_site_contact),
0.01
);
2019-05-20 14:53:09 +02:00
imagecopy($mask, $ImageMentionFond, 335, 235, 0, 0, imagesx($ImageMentionFond), imagesy($ImageMentionFond));
2019-02-25 11:25:55 +01:00
}
2019-02-13 16:37:14 +01:00
// ---- Copie la main dans le mask
2019-02-25 10:31:11 +01:00
$hand = imagecreatefrompng($hand_picture);
2019-02-13 16:37:14 +01:00
imagecopy($mask, $hand, 577, 2, 0, 0, imagesx($hand), imagesy($hand));
2021-04-27 17:29:47 +02:00
imagealphablending($mask, false);
imagesavealpha($mask, true);
2019-02-13 16:37:14 +01:00
// ---- Inscription du montant du BR
2021-04-27 17:29:47 +02:00
$textMontant = number_format($coupon->coupon_value, 2, ",", " ");
2025-04-03 09:30:16 +02:00
$textEuro = "€";
$libReduc = "DE RÉDUCTION IMMÉDIATE";
$ImageFond = imagecreate(265, 200);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
$blue = imagecolorallocate($ImageFond, 8, 169, 223);
$grey = imagecolorallocate($ImageFond, 219, 219, 221);
$this->imagettftextContour($ImageFond, 95, 0, 5, 110, $grey, $impact, $textMontant, $blue, 3, 3, 3, 3);
$this->imagettftextContour($ImageFond, 50, 0, 220, 55, $grey, $arial, $textEuro, $blue, 2, 2, 2, 2);
$this->imagettftextContour($ImageFond, 16, 0, 0, 150, $grey, $impact, $libReduc, $blue, 1, 1, 1, 1);
2019-02-13 16:37:14 +01:00
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 360, 10, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
// ---- Inscription filigramme
2025-04-03 09:30:16 +02:00
$txt_filigramme = strtoupper($site->site_libelle) . " " . number_format($coupon->coupon_value, 2, ",", " ") . "€";
2021-04-27 17:29:47 +02:00
$line_filigramme = "";
2025-04-03 09:30:16 +02:00
$textFili = "";
2021-04-27 17:29:47 +02:00
for ($i = 0; $i < 11; $i++) {
$line_filigramme .= $txt_filigramme . " ";
2019-02-13 16:37:14 +01:00
}
$line_filigramme .= "\r\n";
2021-04-27 17:29:47 +02:00
for ($i = 0; $i < 30; $i++) {
$textFili .= $line_filigramme;
2019-02-13 16:37:14 +01:00
}
2019-02-25 14:33:36 +01:00
$textFiliLines = explode("\r\n", $textFili);
2025-04-03 09:30:16 +02:00
$ImageFond = imagecreate(402, 240);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$y = 0;
2019-02-25 14:33:36 +01:00
foreach ($textFiliLines as $textFiliLine) {
2021-04-27 17:29:47 +02:00
$y = $y + 20;
$this->imagettftextSp($ImageFond, 11, 25, -100, $y, $black, $arial, $textFiliLine, 0.01);
2019-02-25 14:33:36 +01:00
}
2019-02-13 16:37:14 +01:00
imagecolortransparent($ImageFond, $white);
imagecopymerge($mask, $ImageFond, 349, 1, 0, 0, imagesx($ImageFond), imagesy($ImageFond), 30);
2019-02-25 10:23:46 +01:00
// ---- Inscription produit valable
$textReduc = $coupon->coupon_validity_text;
2021-04-27 17:29:47 +02:00
if (strlen($textReduc) < 100) {
2019-03-12 15:49:26 +01:00
$ImageFond = imagecreate(387, 200);
2025-04-03 09:30:16 +02:00
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
2019-03-12 15:49:26 +01:00
$textReducs = explode("\r\n", $textReduc);
2025-04-03 09:30:16 +02:00
$y = 14;
2019-03-12 15:49:26 +01:00
foreach ($textReducs as $textReduc) {
2021-04-27 17:29:47 +02:00
$this->imagettftextSp($ImageFond, 12, 0, 0, $y, $black, $arialbd, $textReduc, 0.01);
$y = $y + 18;
2019-03-12 15:49:26 +01:00
}
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 360, 170, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
2021-04-27 17:29:47 +02:00
} elseif (strlen($textReduc) > 200) {
$ImageFond = imagecreate(387, 205);
2025-04-03 09:30:16 +02:00
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
$textReducs = explode("\r\n", $textReduc);
2025-04-03 09:30:16 +02:00
$y = 10;
foreach ($textReducs as $textReduc) {
2021-04-27 17:29:47 +02:00
$this->imagettftextSp($ImageFond, 9, 0, 0, $y, $black, $arialbd, $textReduc, 0.01);
$y = $y + 12;
}
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 360, 165, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
2019-03-12 15:49:26 +01:00
} else {
$ImageFond = imagecreate(387, 200);
2025-04-03 09:30:16 +02:00
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
2019-03-12 15:49:26 +01:00
$textReducs = explode("\r\n", $textReduc);
2025-04-03 09:30:16 +02:00
$y = 14;
2019-03-12 15:49:26 +01:00
foreach ($textReducs as $textReduc) {
2021-04-27 17:29:47 +02:00
$this->imagettftextSp($ImageFond, 10, 0, 0, $y, $black, $arialbd, $textReduc, 0.01);
$y = $y + 12;
2019-03-12 15:49:26 +01:00
}
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 360, 170, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
} //end if
2019-02-25 10:41:06 +01:00
// ---- Copie le produit dans le mask
2019-02-26 16:31:42 +01:00
$size = getimagesize($product_image);
switch ($size['mime']) {
case 'image/jpeg':
$pdt = imagecreatefromjpeg($product_image);
break;
case 'image/png':
$pdt = imagecreatefrompng($product_image);
break;
}
2021-04-27 17:29:47 +02:00
imagealphablending($mask, true);
imagesavealpha($mask, true);
2019-03-07 16:14:20 +01:00
imagecopy($mask, $pdt, 40, 10, 0, 0, imagesx($pdt), imagesy($pdt));
2021-04-27 17:29:47 +02:00
2019-02-25 10:41:06 +01:00
// ---- Copie le logo dans le mask
2019-02-26 16:31:42 +01:00
$size_logo = getimagesize($logo_display);
switch ($size_logo['mime']) {
case 'image/jpeg':
$logo = imagecreatefromjpeg($logo_display);
break;
case 'image/png':
$logo = imagecreatefrompng($logo_display);
break;
}
2021-04-27 17:29:47 +02:00
imagealphablending($mask, true);
imagesavealpha($mask, true);
2019-02-26 16:31:42 +01:00
imagecopy($mask, $logo, 767, 220, 0, 0, imagesx($logo), imagesy($logo));
2021-04-27 17:29:47 +02:00
2019-02-13 16:37:14 +01:00
// ---- Créer le fichier final
2025-04-02 15:14:41 +00:00
imagepng($mask, storage_path('app/public/modelfiles/coupons') . "/" . $coupon->coupon_code . ".png", 9);
2019-02-13 16:37:14 +01:00
// ---- Libère la mémoire associée aux images manipulées
2019-02-25 10:41:06 +01:00
imagedestroy($logo);
imagedestroy($pdt);
2019-02-13 16:37:14 +01:00
imagedestroy($mask);
2021-04-27 17:29:47 +02:00
2025-04-02 15:14:41 +00:00
return "storage/modelfiles/coupons/" . $coupon->coupon_code . ".png";
2019-02-13 16:37:14 +01:00
}
2021-04-27 17:29:47 +02:00
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0)
{
2019-02-25 11:43:16 +01:00
if ($spacing == 0) {
2019-02-25 14:33:36 +01:00
imagefttext($image, $size, $angle, $x, $y, $color, $font, $text);
2019-02-25 11:43:16 +01:00
} else {
2019-02-25 11:25:55 +01:00
$temp_x = $x;
2019-02-25 11:43:16 +01:00
$temp_y = $y;
2025-04-03 09:30:16 +02:00
// to avoid special char problems
2021-04-27 17:29:47 +02:00
$char_array = preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY);
foreach ($char_array as $char) {
2019-02-25 14:33:36 +01:00
imagefttext($image, $size, $angle, $temp_x, $temp_y, $color, $font, $char);
2025-04-03 09:30:16 +02:00
$bbox = imagettfbbox($size, 0, $font, $char);
2019-02-25 11:43:16 +01:00
$temp_x += cos(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0]));
$temp_y -= sin(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0]));
2019-02-25 11:25:55 +01:00
}
}
}
2019-02-13 16:37:14 +01:00
function imagettftextContour($image, $fontsize, $angle, $x, $y, $color, $font, $text, $color_contour, $contour_haut = 1, $contour_bas = 1, $contour_gauche = 1, $contour_droite = 1)
2021-04-27 17:29:47 +02:00
{
2025-04-02 15:14:41 +00:00
$coords_x = array();
2021-04-27 17:29:47 +02:00
$coords_y = array();
for ($i_y = -$contour_haut; $i_y <= $contour_bas; $i_y++) {
2025-04-02 15:14:41 +00:00
$y_temp = @array_fill(0, $contour_gauche + $contour_droite + 1, $i_y);
$x_temp = array();
2021-04-27 17:29:47 +02:00
for ($i_x = -$contour_gauche; $i_x <= $contour_droite; $i_x++) {
2025-04-02 15:14:41 +00:00
$x_temp[] = $i_x;
2021-04-27 17:29:47 +02:00
}
2025-04-02 15:14:41 +00:00
$coords_x = array_merge($coords_x, $x_temp);
$coords_y = array_merge($coords_y, $y_temp);
2021-04-27 17:29:47 +02:00
}
2025-04-02 15:14:41 +00:00
$c = count($coords_x);
2021-04-27 17:29:47 +02:00
for ($i = 0; $i < $c; $i++) {
2025-04-02 15:14:41 +00:00
imagettftext($image, $fontsize, $angle, $x + $coords_x[$i], $y + $coords_y[$i], $color_contour, $font, $text);
2021-04-27 17:29:47 +02:00
}
2025-04-02 15:14:41 +00:00
imagettftext($image, $fontsize, $angle, $x, $y, $color, $font, $text);
2019-02-13 16:37:14 +01:00
}
2021-04-27 17:29:47 +02:00
public function printCp($coupon, $contact)
{
2025-04-03 09:30:16 +02:00
$arial = public_path() . '/build/assets/fonts/coupons/arial.ttf';
$arialbd = public_path() . '/build/assets/fonts/coupons/arialbd.ttf';
2021-04-27 17:29:47 +02:00
// ---- Masque dans lequel l'image finale sera placée
$mask = imagecreatefrompng($coupon->coupon_image);
list($maskWidth, $maskHeight) = getimagesize($coupon->coupon_image);
2019-02-13 16:37:14 +01:00
2019-05-20 14:53:09 +02:00
// ----- Mentions légales + RCS dépendant de l'user
if ($coupon->coupon_legal_mentions_splitted == 1) {
$legalMentions = explode('<nl>', $coupon->coupon_legal_mentions);
2021-04-27 17:29:47 +02:00
if (($coupon->coupon_norme == 1 && strlen($contact->contact_cp) == 4) || $coupon->coupon_code == '5425003942648' || $coupon->coupon_code == '5425003941641') {
2025-04-03 09:30:16 +02:00
// BELGE
$txt_mentions = $legalMentions[0] . "\n\r" . $legalMentions[1] . " " . $coupon->coupon_rcs;
2019-05-20 15:11:44 +02:00
$ImageMentionFond = imagecreate(420, 145);
2025-04-03 09:30:16 +02:00
$boxMentions = imagettfbbox(7, 0, $arial, $txt_mentions);
$white = imagecolorallocate($ImageMentionFond, 255, 255, 255);
2019-05-20 14:53:09 +02:00
imagefilledrectangle($ImageMentionFond, 0, 0, 414, 99, $white);
2025-04-03 09:30:16 +02:00
$black = imagecolorallocate($ImageMentionFond, 0, 0, 0);
2019-05-20 14:53:09 +02:00
$lines_mentions = explode("\r\n", $txt_mentions);
2025-04-03 09:30:16 +02:00
$y = 19;
2021-04-27 17:29:47 +02:00
foreach ($lines_mentions as $line) {
$this->imagettftextSp($ImageMentionFond, 7, 0, 15, $y, $black, $arial, $line, 0.01);
$y = $y + 9;
2019-05-20 14:53:09 +02:00
}
2025-04-03 09:30:16 +02:00
// imagecolortransparent($ImageMentionFond, $white);
2019-05-20 15:11:44 +02:00
imagecopy($mask, $ImageMentionFond, 335, 235, 0, 0, imagesx($ImageMentionFond), imagesy($ImageMentionFond));
2019-05-20 14:53:09 +02:00
} else {
2025-04-03 09:30:16 +02:00
// FR
$txt_mentions = $legalMentions[0] . " " . $coupon->coupon_rcs;
2019-05-20 14:53:09 +02:00
$ImageMentionFond = imagecreate(415, 100);
2025-04-03 09:30:16 +02:00
$boxMentions = imagettfbbox(7, 0, $arial, $txt_mentions);
$white = imagecolorallocate($ImageMentionFond, 255, 255, 255);
2019-05-20 14:53:09 +02:00
imagefilledrectangle($ImageMentionFond, 0, 0, 414, 99, $white);
2025-04-03 09:30:16 +02:00
$black = imagecolorallocate($ImageMentionFond, 0, 0, 0);
2019-05-20 14:53:09 +02:00
$lines_mentions = explode("\r\n", $txt_mentions);
2025-04-03 09:30:16 +02:00
$y = 19;
2019-05-20 14:53:09 +02:00
foreach ($lines_mentions as $line) {
2021-04-27 17:29:47 +02:00
$this->imagettftextSp($ImageMentionFond, 7, 0, 15, $y, $black, $arial, $line, 0.01);
$y = $y + 9;
2019-05-20 14:53:09 +02:00
}
2025-04-03 09:30:16 +02:00
// imagecolortransparent($ImageMentionFond, $white);
2019-05-20 14:53:09 +02:00
imagecopy($mask, $ImageMentionFond, 335, 235, 0, 0, imagesx($ImageMentionFond), imagesy($ImageMentionFond));
} //end if
} //end if
2021-04-27 17:29:47 +02:00
// ---- Inscription de l'utilisateur et de la ville
2025-04-03 09:30:16 +02:00
$libelle = "OFFRE RÉSERVÉE À";
$user = $contact->contact_prenom . " " . $contact->contact_nom . "\r\n" . $contact->contact_cp . " " . $contact->contact_ville;
2021-04-27 17:29:47 +02:00
$ImageFond = imagecreate(120, 40);
2025-04-03 09:30:16 +02:00
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
$box1 = imagettfbbox(8, 0, $arial, $user);
$box2 = imagettfbbox(8, 0, $arial, $libelle);
2021-04-27 17:29:47 +02:00
imagettftext($ImageFond, 8, 0, (120 / 2) - (abs($box2[4]) - abs($box2[6])) / 2, 10, $black, $arialbd, $libelle);
imagettftext($ImageFond, 8, 0, (120 / 2) - (abs($box1[4]) - abs($box1[6])) / 2, 25, $black, $arial, $user);
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 770, 50, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
// ---- Inscription de la date de validité
$libelle = "Valable jusqu'au :";
if ($coupon->coupon_validity != 0) {
$dateValidite = date('d/m/Y', strtotime("+" . $coupon->coupon_validity . " day"));
} else {
2025-04-03 09:30:16 +02:00
$dateValidite = date('d/m/Y', strtotime("+30 day"));
// 30 jours par défaut
2021-04-27 17:29:47 +02:00
}
$ImageFond = imagecreate(120, 40);
2025-04-03 09:30:16 +02:00
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
imagettftext($ImageFond, 8, 0, 1, 8, $black, $arialbd, $libelle);
imagettftext($ImageFond, 8, 0, 20, 23, $black, $arial, $dateValidite);
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 780, 142, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
// ---- Création code barre
if (($coupon->coupon_norme == 1 && strlen($contact->contact_cp) == 4) || $coupon->coupon_code == '5425003942648' || $coupon->coupon_code == '5425003941641') {
$textReduc = "Traitement ScanCoupon/HighCo Data Benelux";
} elseif ($coupon->coupon_treatment_center == 0) {
$textReduc = "• Traitement ScanCoupon •";
} else {
$textReduc = "• Traitement Sogec •";
}
2021-04-27 17:29:47 +02:00
$ImageFond = imagecreate(270, 20);
2025-04-03 09:30:16 +02:00
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
imagettftext($ImageFond, 9, 0, 0, 10, $black, $arialbd, $textReduc);
if (($coupon->coupon_norme == 1 && strlen($contact->contact_cp) == 4) || $coupon->coupon_code == '5425003942648' || $coupon->coupon_code == '5425003941641') {
imagecopy($mask, $ImageFond, 70, 230, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
} else {
imagecopy($mask, $ImageFond, 52, 322, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
}
if (($coupon->coupon_norme == 1 && strlen($contact->contact_cp) == 4) || $coupon->coupon_code == '5425003942648' || $coupon->coupon_code == '5425003941641') {
2019-02-13 16:37:14 +01:00
$codeBarre = imagecreatefromjpeg($coupon->coupon_gs1code);
imageresolution($codeBarre, 72);
list($width, $height) = getimagesize($coupon->coupon_gs1code);
2025-04-03 09:30:16 +02:00
$r = $width / $height;
$w = 285;
$h = 150;
2021-04-27 17:29:47 +02:00
if ($w / $h > $r) {
2025-04-03 09:30:16 +02:00
$newwidth = $h * $r;
2019-02-13 16:37:14 +01:00
$newheight = $h;
} else {
2021-04-27 17:29:47 +02:00
$newheight = $w / $r;
2025-04-03 09:30:16 +02:00
$newwidth = $w;
2019-02-13 16:37:14 +01:00
}
$test = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($test, $codeBarre, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
2019-03-07 16:14:20 +01:00
imagecopy($mask, $test, 60, 250, 0, 0, imagesx($test), imagesy($test));
2021-04-27 17:29:47 +02:00
} else {
$codeBarre = $coupon->coupon_code . strval($contact->contact_id);
2021-04-27 17:29:47 +02:00
$codeBarrePrint = $codeBarre;
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$base64 = $generator->getBarcode($codeBarrePrint, $generator::TYPE_CODE_128_A, 2, 46);
$barcodeImage = imagecreatefromstring($base64);
2019-02-13 16:37:14 +01:00
imagecopy($mask, $barcodeImage, 52, 342, 0, 0, imagesx($barcodeImage), imagesy($barcodeImage));
$ImageFond = imagecreate(380, 15);
2025-04-03 09:30:16 +02:00
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
imagettftext($ImageFond, 11, 0, 0, 14, $black, $arial, $codeBarre);
2019-02-13 16:37:14 +01:00
imagecopy($mask, $ImageFond, 52, 390, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
} //end if
2019-02-13 16:37:14 +01:00
2021-04-27 17:29:47 +02:00
ob_start();
2019-12-23 10:22:19 +01:00
imagejpeg($mask, null, 100);
$contents = ob_get_contents();
2021-04-27 17:29:47 +02:00
ob_end_clean();
$dataUri = "data:image/jpeg;base64," . base64_encode($contents);
// ---- Libère la mémoire associée aux images manipulées
imagedestroy($mask);
imagedestroy($ImageFond);
return $dataUri;
}
// VB 22/04/2021
2025-08-14 17:05:09 +02:00
public function printCoupon(Coupon $coupon, Customers $customer, $crmInfos, Campagne $campagne)
2021-04-27 17:29:47 +02:00
{
$arial = public_path() . '/build/assets/fonts/coupons/arial.ttf';
2025-04-03 09:30:16 +02:00
$arialbd = public_path() . '/build/assets/fonts/coupons/arialbd.ttf';
2021-04-27 17:29:47 +02:00
// Load base mask image
$mask = imagecreatefrompng($coupon->coupon_image);
2021-04-27 17:29:47 +02:00
list($maskWidth, $maskHeight) = getimagesize($coupon->coupon_image);
// --- Draw Legal Mentions + RCS depending on the user country
2021-04-27 17:29:47 +02:00
if ($coupon->coupon_legal_mentions_splitted == 1) {
$legalMentions = explode('<nl>', $coupon->coupon_legal_mentions);
2021-04-27 17:29:47 +02:00
if ($customer->country !== 250) {
2025-04-03 09:30:16 +02:00
// BELGIQUE - Luxembourg
$txt_mentions = $legalMentions[0] . "\n\r " . $legalMentions[1] . "\n\r " . $coupon->coupon_rcs;
$imageWidth = 420;
$imageHeight = 145;
2021-04-27 17:29:47 +02:00
} else {
2025-04-03 09:30:16 +02:00
// FR
$txt_mentions = $legalMentions[0] . " " . $coupon->coupon_rcs;
$imageWidth = 415;
$imageHeight = 100;
}
2021-04-27 17:29:47 +02:00
$ImageMentionFond = imagecreate($imageWidth, $imageHeight);
$white = imagecolorallocate($ImageMentionFond, 255, 255, 255);
imagefilledrectangle($ImageMentionFond, 0, 0, $imageWidth - 6, $imageHeight - 46, $white);
$black = imagecolorallocate($ImageMentionFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
$cleanedString = str_replace(["\r", "\n"], '', $txt_mentions);
$lines_mentions = str_split($cleanedString, 92);
$y = 19;
foreach ($lines_mentions as $line) {
$this->imagettftextSp($ImageMentionFond, 7, 0, 15, $y, $black, $arial, $line, 0.01);
$y += 9;
}
imagecopy($mask, $ImageMentionFond, 335, 235, 0, 0, imagesx($ImageMentionFond), imagesy($ImageMentionFond));
imagedestroy($ImageMentionFond);
}
// --- User inscription and city
$libelle = "OFFRE RÉSERVÉE À";
$user = sprintf(
2025-08-14 17:05:09 +02:00
"%s\r\n%s\r\n%s\r\n%s %s\r\n%s",
$crmInfos->firstname,
$crmInfos->lastname,
$crmInfos->address,
$crmInfos->postcode,
$crmInfos->city,
2025-08-14 17:05:09 +02:00
sprintf('id #%s', str_pad($customer->id, 13, "0", STR_PAD_LEFT))
);
2025-08-14 17:05:09 +02:00
$ImageFond = imagecreate(120, 80);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
// Centered libelle text
$boxLibelle = imagettfbbox(8, 0, $arial, $libelle);
$libelleX = (120 / 2) - (abs($boxLibelle[4]) - abs($boxLibelle[6])) / 2;
imagettftext($ImageFond, 8, 0, $libelleX, 10, $black, $arialbd, $libelle);
2025-08-14 17:05:09 +02:00
// User lines
2025-08-14 17:05:09 +02:00
$userLines = explode("\r\n", $user);
$y = 25;
2025-08-14 17:05:09 +02:00
foreach ($userLines as $line) {
$box = imagettfbbox(8, 0, $arial, $line);
$lineX = (120 / 2) - (abs($box[4]) - abs($box[6])) / 2;
$this->imagettftextSp($ImageFond, 8, 0, $lineX, $y, $black, $arial, $line, 0.01);
$y += 10;
2025-08-14 17:05:09 +02:00
}
2021-04-27 17:29:47 +02:00
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 770, 40, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
imagedestroy($ImageFond);
2021-04-27 17:29:47 +02:00
// --- Validity date inscription
2021-04-27 17:29:47 +02:00
$libelle = "Valable jusqu'au :";
$validDays = ($coupon->coupon_validity != 0) ? $coupon->coupon_validity : 30;
$dateValidite = date('d/m/Y', strtotime("+$validDays day"));
2021-04-27 17:29:47 +02:00
$ImageFond = imagecreate(120, 40);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
imagettftext($ImageFond, 8, 0, 1, 8, $black, $arialbd, $libelle);
imagettftext($ImageFond, 8, 0, 20, 23, $black, $arial, $dateValidite);
2021-04-27 17:29:47 +02:00
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 780, 142, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
imagedestroy($ImageFond);
2021-04-27 17:29:47 +02:00
// Save printed coupon
2025-08-14 17:05:09 +02:00
$printedCoupon = $this->savePrintedCoupon($campagne, $customer, $coupon);
// --- Right barcode
2025-08-14 17:05:09 +02:00
$codeBarre = str_pad($printedCoupon->id, 13, "0", STR_PAD_LEFT);
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$base64 = $generator->getBarcode($codeBarre, $generator::TYPE_CODE_128, 2, 30);
$barcodeImage = imagecreatefromstring($base64);
imagecopy($mask, $barcodeImage, 660, 355, 0, 0, imagesx($barcodeImage), imagesy($barcodeImage));
imagedestroy($barcodeImage);
// Text below right barcode
$ImageFond = imagecreate(380, 15);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
imagettftext($ImageFond, 11, 0, 0, 14, $black, $arial, $codeBarre);
imagecopy($mask, $ImageFond, 800, 385, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
imagedestroy($ImageFond);
// --- Barcode treatment text
2021-04-27 17:29:47 +02:00
if ($customer->country !== 250) {
$textReduc = "Traitement ScanCoupon/HighCo Data Benelux";
$copyPosition = [70, 230];
2021-04-27 17:29:47 +02:00
} elseif ($coupon->coupon_treatment_center == 0) {
$textReduc = "• Traitement ScanCoupon •";
$copyPosition = [52, 322];
2021-04-27 17:29:47 +02:00
} else {
$textReduc = "• Traitement Sogec •";
$copyPosition = [52, 322];
2021-04-27 17:29:47 +02:00
}
$ImageFond = imagecreate(270, 20);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
imagettftext($ImageFond, 9, 0, 0, 10, $black, $arialbd, $textReduc);
imagecopy($mask, $ImageFond, $copyPosition[0], $copyPosition[1], 0, 0, imagesx($ImageFond), imagesy($ImageFond));
imagedestroy($ImageFond);
2021-04-27 17:29:47 +02:00
// --- Left barcode
2021-04-27 17:29:47 +02:00
if ($customer->country !== 250) {
$codeBarreImg = imagecreatefromjpeg($coupon->coupon_gs1code);
imageresolution($codeBarreImg, 72);
2021-04-27 17:29:47 +02:00
list($width, $height) = getimagesize($coupon->coupon_gs1code);
$ratio = $width / $height;
$maxWidth = 285;
$maxHeight = 150;
if ($maxWidth / $maxHeight > $ratio) {
$newWidth = $maxHeight * $ratio;
$newHeight = $maxHeight;
2021-04-27 17:29:47 +02:00
} else {
$newHeight = $maxWidth / $ratio;
$newWidth = $maxWidth;
2021-04-27 17:29:47 +02:00
}
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($resizedImage, $codeBarreImg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagecopy($mask, $resizedImage, 60, 250, 0, 0, $newWidth, $newHeight);
imagedestroy($codeBarreImg);
imagedestroy($resizedImage);
2021-04-27 17:29:47 +02:00
} else {
$codeBarre = $coupon->coupon_code;
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$base64 = $generator->getBarcode($codeBarre, $generator::TYPE_CODE_128_A, 2, 46);
2021-04-27 17:29:47 +02:00
$barcodeImage = imagecreatefromstring($base64);
imagecopy($mask, $barcodeImage, 10, 342, 0, 0, imagesx($barcodeImage), imagesy($barcodeImage));
imagedestroy($barcodeImage);
2021-04-27 17:29:47 +02:00
$ImageFond = imagecreate(380, 15);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
2021-04-27 17:29:47 +02:00
imagettftext($ImageFond, 11, 0, 0, 14, $black, $arial, $codeBarre);
imagecopy($mask, $ImageFond, 52, 390, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
imagedestroy($ImageFond);
}
2021-04-27 17:29:47 +02:00
// Final output as base64 encoded JPEG
2021-04-27 17:29:47 +02:00
ob_start();
imagejpeg($mask, null, 100);
$contents = ob_get_contents();
ob_end_clean();
imagedestroy($mask);
2025-11-04 15:29:03 +01:00
// Save stat.
$this->saveStat($campagne, $coupon);
return "data:image/jpeg;base64," . base64_encode($contents);
2021-04-27 17:29:47 +02:00
}
2025-08-14 17:05:09 +02:00
2025-11-04 15:29:03 +01:00
/**
* Save 'printed_coupons' line.
*
* @param Campagne $campagne
* @param Customers $customer
* @param Coupon $coupon
* @return PrintedCoupons
*/
2025-08-14 17:05:09 +02:00
public function savePrintedCoupon(Campagne $campagne, Customers $customer, Coupon $coupon): PrintedCoupons
{
$mur = new PrintedCoupons();
$mur->camp_id = $campagne->camp_id;
$mur->customer_id = $customer->id;
$mur->coupon_id = $coupon->coupon_id;
$mur->save();
return $mur;
}
2025-11-04 15:29:03 +01:00
/**
* Save stats in the 'statistiques' table.
*
* @param Campagne $campagne
* @param Coupon $coupon
* @return Statistique
*/
public function saveStat(Campagne $campagne, Coupon $coupon): Statistique
{
2025-11-04 15:29:03 +01:00
$stat = Statistique::query()->firstOrCreate(
[
'date' => now()->toDateString(),
'stat_campId' => $campagne->camp_id,
'stat_couponId' => $coupon->coupon_id,
],
[
'stat_couponPrinted' => 0,
]
);
$stat->stat_couponPrinted += 1;
$stat->save();
return $stat;
}
2021-04-27 17:29:47 +02:00
}