Rapport-Laravel/app/Console/Commands/GenerateAssets.php

144 lines
5.4 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\LambdaRequest;
use Illuminate\Support\Facades\Http;
use Log;
class GenerateAssets extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'assets:generate';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$job = DB::table("export_jobs")->where('status', 'created')->where('type', 'tentcard')->first();
if(!is_null($job)) {
//$user = DB::table("users")->where('user_id', $job->user_id)->first();
//$response = LambdaRequest::getrequest('ums', 'contact/get_all', ['search' => json_encode([]), 'keyword' => "", 'start' => 0, 'length' => 9999, 'fields' => '["msl_code","doctor_image","user_info"]', 'belowme' => true, 'source' => 'dynamodb' ], $user->access_token );
$conts = DB::table("contacts")->select('id', 'name', 'contact_id', 'user_id', 'employee_code', 'rep_name', 'position_name', 'msl_code')->whereBetween('created_date', [date("Y-m-d H:i:s", strtotime($job->start_date) + 19800 ), date("Y-m-d H:i:s", strtotime($job->end_date) + 19800 ) ])->get();
$files = [];
$contacts = [];
foreach($conts as $cont) {
$contacts[$cont->msl_code] = $cont;
}
foreach($contacts as $contact) {
//$contact->name = str_replace(" ", "_", $contact->name);
print_r($contact);
$file = $this->generate($contact, $job);
$files[] = $file;
}
print_r(count($contacts));
// $folder = storage_path('app/public/'.$job->type);
// $fileName = $contact->user_info->employee_code.'-'.$user->name.'-'.date("Y-m-d", strtotime($job->start_date)).'-'.date("Y-m-d", strtotime($job->start_date)).'.zip';
// exec('cd '.$folder.'; zip "'.$fileName.'" ' . implode(' ', $files));
// $zip = new \ZipArchive();
// $folder = storage_path('app/public/'.$job->type);
// $fileName = $contact->user_info->employee_code.'('.$user->name.')-'.$job->start_date.'-'.$job->start_date.'.zip';
// if ($zip->open($folder, \ZipArchive::CREATE)== TRUE)
// {
// foreach ($files as $value){
// $zip->addFile($folder.'/'.$value, $value);
// }
// $zip->close();
// }
//print_r($fileName);
}
//print_r($job);
}
protected function generate($contact, $job) {
$folder = storage_path('app/public/'.$job->type);
$templatename = 'template.png';
switch($job->type) {
case 'tentcard':
$pdfname = str_replace(' ', '_', $contact->employee_code).'-'.str_replace(' ', '_', $contact->name).'-Tent-Standee.pdf';
if(!file_exists($folder.'/'.$pdfname)) {
$outputImage = $this->retimage($folder.'/'.$templatename);
$font = storage_path('app').'/fonts/Poppins-Medium.ttf';
$white = imagecolorallocatealpha($outputImage, 255, 255, 255, 0);
imagettftext($outputImage, 28, 0, 120, 1290, $white, $font, 'Dr. ' . str_replace('Dr', '', str_replace('.', '', $contact->name) ));
exec('qrencode -o '.$folder.'/qrcode-'.$contact->id.'.png -s 5 -m 2 https://breathefree.cttr.in/tracking/'.$contact->contact_id.'/'.$contact->user_id);
//file_put_contents($folder.'/'.'qrcode-'.$contact->id.'.png', file_get_contents( 'https://api.qrserver.com/v1/create-qr-code/?size=200&data=https://breathefree.cttr.in/tracking/'.$contact->id.'/'.$contact->user_id ));
$qrimg = $this->retimage($folder.'/'.'qrcode-'.$contact->id.'.png');
imagecopyresized($outputImage,$qrimg,645,905,0,0,220,220,225,225);
$filename = 'tentcard-'.$contact->id.'.png';
imagepng($outputImage, $folder.'/'.$filename);
imagedestroy($outputImage);
//imagedestroy($templatePDF);
exec('convert ' .$folder.'/'.$filename. ' '.$folder.'/'.str_replace(' ', '_', $contact->employee_code).'-'.str_replace(' ', '_', $contact->name).'-Tent-Standee.pdf');
exec('rm '.$folder.'/'.$filename);
exec('rm '.$folder.'/'.'qrcode-'.$contact->id.'.png');
}
return $pdfname;
break;
}
}
protected function retimage($image) {
$size = getimagesize($image);
switch ($size['mime']) {
case 'image/jpeg':
$photoimg = imagecreatefromjpeg($image);
break;
case 'image/png':
$photoimg = imagecreatefrompng($image);
break;
case 'image/jpeg':
$photoimg = imagecreatefrompng($image);
break;
default:
# code...
break;
}
return $photoimg;
}
}