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