ldc-mesbr-app/app/Traits/GenerateBR.php
Thibault Richy 08a829c8a6 test
2019-02-25 11:37:29 +01:00

277 lines
No EOL
12 KiB
PHP

<?php
namespace App\Traits;
use Picqer\Barcode\BarcodeGenerator;
use Picqer\Barcode\BarcodeGeneratorPNG;
use Illuminate\Http\Request;
use App\Coupon;
use App\Site;
trait GenerateBR {
public function generateBase(Request $request, Coupon $coupon)
{
$arial = public_path().'/generation/src/arial.ttf';
$arialbd = public_path().'/generation/src/arialbd.ttf';
$impact = public_path().'/generation/src/impact.ttf';
$hand_picture = 'generation/src/hand.png';
$mask_img = 'generation/src/mask.png';
if(null !== $request->input('site_id')){
$site = Site::where(['id' => $request->input('site_id')])->first();
} else {
$site = $coupon->site()->first();
}
$logo_display = $coupon->coupon_logo_image;
$product_image = $coupon->coupon_decoration_image;
// ---- Masque dans lequel l'image finale sera placée
$mask = imagecreatefrompng($mask_img);
list($maskWidth, $maskHeight) = getimagesize($mask_img);
// ---- Contact distributeur
$text = $coupon->coupon_site_contact;
$ImageFond = imagecreate(155, 27);
$box1 = imagettfbbox(8, 0, $arial, $text);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
imagettftext($ImageFond , 8 , 0, (155/2)-(abs($box1[4])-abs($box1[6]))/2, 23, $black, $arial, $text);
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 748, 365, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
// ----- Mentions légales + RCS
$txt_mentions = $coupon->coupon_legal_mentions." ".$coupon->coupon_rcs;
$ImageMentionFond = imagecreate(415, 90);
$boxMentions = imagettfbbox(7, 0, $arial, $txt_mentions);
$white = imagecolorallocate($ImageMentionFond, 255, 255, 255);
$black = imagecolorallocate($ImageMentionFond, 0, 0, 0);
$lines_mentions = explode("\r\n", $txt_mentions);
$y = 4;
foreach ($lines_mentions as $line) {
$y = $y +13;
$this->imagettftextSp($ImageMentionFond , 7 , 0, (415/2)-(abs($boxMentions[4])-abs($boxMentions[6]))/2, $y, $black, $arial, $line, 0);
}
imagecolortransparent($ImageMentionFond, $white);
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->imagettftext_contour($ImageFond, 95, 0, 5, 110, $grey, $impact, $textMontant, $blue, 3, 3, 3, 3);
$this->imagettftext_contour($ImageFond, 50, 0, 220, 55, $grey, $arial, $textEuro, $blue, 2, 2, 2, 2);
$this->imagettftext_contour($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="";
$textReduc="";
for ($i=0; $i < 11; $i++) {
$line_filigramme .= $txt_filigramme." ";
}
$line_filigramme .= "\r\n";
for ($i=0; $i < 30; $i++) {
$textReduc.= $line_filigramme;
}
$ImageFond = imagecreate(402, 240);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$blackOpac = imagecolorallocatealpha ($ImageFond, 0, 0, 0, 20);
imagettftext($ImageFond , 11 , 25, -100, 11, $blackOpac, $arial, $textReduc);
imagecolortransparent($ImageFond, $white);
imagecopymerge($mask, $ImageFond, 349, 1, 0, 0, imagesx($ImageFond), imagesy($ImageFond), 30);
// ---- Inscription produit valable
$textReduc = $coupon->coupon_validity_text;
$ImageFond = imagecreate(387, 200);
$white = imagecolorallocate($ImageFond, 255, 255, 255);
$black = imagecolorallocate($ImageFond, 0, 0, 0);
imagettftext($ImageFond , 12 , 0, 0, 14, $black, $arialbd, $textReduc);
imagecolortransparent($ImageFond, $white);
imagecopy($mask, $ImageFond, 360, 170, 0, 0, imagesx($ImageFond), imagesy($ImageFond));
// ---- Copie le produit dans le mask
$pdt = imagecreatefromjpeg($product_image);
imagecopy($mask, $pdt, 45, 26, 0, 0, imagesx($pdt), imagesy($pdt));
imagealphablending( $mask, false );
imagesavealpha( $mask, true );
// ---- Copie le logo dans le mask
$logo = imagecreatefromjpeg($logo_display);
imagecopy($mask, $logo, 767, 220, 0, 0, imagesx($logo), imagesy($logo));
imagealphablending( $mask, false );
imagesavealpha( $mask, true );
// ---- Créer le fichier final
imagepng($mask, public_path('generation')."/".$coupon->coupon_code.".png", 9);
// ---- Libère la mémoire associée aux images manipulées
imagedestroy($logo);
imagedestroy($pdt);
imagedestroy($mask);
return "generation/".$coupon->coupon_code.".png";
}
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) {
if ($spacing == 0) {
imagettftext($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) {
imagettftext($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]));
}
}
}
function imagettftext_contour($image, $fontsize, $angle, $x, $y, $color, $font, $text, $color_contour, $contour_haut=1, $contour_bas=1, $contour_gauche=1, $contour_droite=1)
{
$coords_x=array(); $coords_y=array();
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 = array();
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().'/generation/src/arial.ttf';
$arialbd = public_path().'/generation/src/arialbd.ttf';
$impact = public_path().'/generation/src/impact.ttf';
// ---- Masque dans lequel l'image finale sera placée
$mask = imagecreatefrompng($coupon->coupon_image);
list($maskWidth, $maskHeight) = getimagesize($coupon->coupon_image);
// ---- 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){
$textReduc = "Traitement ScanCoupon/HighCo Data Benelux";
}else{
$textReduc = "• Traitement ScanCoupon •";
}
$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){
imagecopy($mask, $ImageFond, 70, 225, 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){
$codeBarre = imagecreatefromjpeg($coupon->coupon_gs1code);
imageresolution($codeBarre, 72);
list($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, 242, 0, 0, imagesx($test), imagesy($test));
}else{
$codeBarre = $coupon->coupon_code.strtoupper($contact->contact_ref);
$codeBarrePrint = $codeBarre;
if( strpos($contact->contact_ref, "@") !== false){
$codeBarre = $coupon->coupon_code.strtolower($contact->contact_ref);
$codeBarrePrint = $coupon->coupon_code.".@.";
}
$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));
}
ob_start();
imagejpeg($mask, null, 100);
$contents = ob_get_contents();
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;
}
}