I have two model : User & Book. I want create a book for each user with a specific QR code
$users = User::get();
$date = ... ;
$finalCount = 0;
$code = 150;
$userCount = count($users);
foreach ($users as $user) {
$book = new Book();
$book->unique_id = uniqid('', true);
$book->user_id = $user->unique_id;
$book->code = "PP-" . strval(mt_rand(100, 999)) . strval($code);
$book->create_date = $date;
$book->status = 'active';
$book->save();
$QRCode = new BaconQrCodeGenerator;
$file = public_path('/images/book/' . $book->code . '.png');
$QRCode->encoding('UTF-8')
->format('png')
->merge('/public/image/logo.png', .15)
->size(1000)
->generate($book->unique_id, $file);
if (File::exists($file))
$finalCount++;
$code++;
if ($finalCount == $userCount)
break;
}
After this function called, i have 20 book for each user. I used an if statement for break the loop ( if ($finalCount == $userCount) ) but it doesn't work.
I can't understand whats going on here and also i have not any error log
Instead of
$file = public_path('/images/book/' . $book->code . '.png');
try
$file = public_path().'/images/book/' . $book->code . '.png';
Related
I am fetching count. My target is to display it in ASCENDING ORDER. i have tried to sort it using sort($match); but i have still failed to achieve my goal. Thank you. Any help will be appreciated
Controller:
function asdasdas(){
$entry= $this->repo->entryfetch();
$data='<table>
<thead><tr><th>NO. </th></tr></thead>';
foreach($entry as $entry){
$match= $this->repo->matchfetch($entry->handlerID);
$data.='<thead><tr><th><hr>'.$entry->handlerID.' '.$entry->entryName.'</th> ';
foreach($match as $match){
$diff = $match->weightM - $match->weightW;
if($match->handlerIDM === $entry->handlerID){
$name=$match->handlertestW;
$count=$match->countM;
}else{
$name=$match->handlerM;
$count=$match->countW;
}
$data.='<tbody><tr><td>'.$count.'</td>';
$data.='<td></td></tr></tbody>';
//
}
}
$data .='<table>';
echo $data;
sort($match); // i have tried to sort it using this but it is not working.
}
You can loop the array from the last index to 0
function asdasdas()
{
$entry = $this->repo->entryfetch();
$data = '<table>
<thead><tr><th>NO. </th></tr></thead>';
for ($j = count($entry) - 1; $j >= 0; $j--) {
$match = $this->repo->matchfetch($entry[$j]->handlerID);
$data .= '<thead><tr><th><hr>' . $entry[$j]->handlerID . ' ' . $entry[$j]->entryName . '</th> ';
for ($i = count($match) - 1; $i >= 0; $i--) {
$diff = $match[$i]->weightM - $match[$i]->weightW;
if ($match[$i]->handlerIDM === $entry[$j]->handlerID) {
$name = $match[$i]->handlertestW;
$count = $match[$i]->countM;
} else {
$name = $match[$i]->handlerM;
$count = $match[$i]->countW;
}
$data .= '<tbody><tr><td>' . $count . '</td>';
$data .= '<td></td></tr></tbody>';
}
}
$data .= '<table>';
echo $data;
}
Context is not much clear. Do you need to sort $match before for loop or while inside the forloop. Can you explain a bit if haven't got the solution.
I wrote these code for upload files through laravel 6.
But I only success for upload one file,even I am sure I got all files while uploading by check from dd($request->files)
in the begining the $index = 0 and in the end it change to 1,first round is ok but the it didn't continue to second run.
I don't know why,please help! thank you~
if($request->hasFile('files')){
$index = 0;
foreach($request->files as $key=>$file){
$originalName = $file[$index]->getClientOriginalName();
$size = $file[$index]->getClientSize();
$ext = $file[$index]->getClientOriginalExtension();
$newName = date('Ymd').mt_rand(100,999).$originalName;
$savePath = '/attachments/'.$newName;
$movePath = base_path().'/public/attachments/'.$newName;
$file[$index]->move(base_path().'/public/attachments',$newName);
$attachment = new attachment();
$attachment->bulletin_id = $bulletin->id; //already got this value before
$attachment->original_name = $originalName;
$attachment->name = $newName;
$attachment->type = $file[$index]->getClientMimeType();
$attachment->path = $savePath;
$attachment->size = ($size/1000);
$attachment->save();
++$index;
}
}
You try this way and it is simple
foreach($request->file('files') as $key=>$file){
$originalName = $file->getClientOriginalName();
$size = $file->getClientSize();
$ext = $file->getClientOriginalExtension();
$newName = date('Ymd').mt_rand(100,999).$originalName;
$savePath = '/attachments/'.$newName;
$movePath = base_path().'/public/attachments/'.$newName;
$file->move(base_path().'/public/attachments',$newName);
$attachment = new attachment();
$attachment->bulletin_id = $bulletin->id; //already got this value before
$attachment->original_name = $originalName;
$attachment->name = $newName;
$attachment->type = $file->getClientMimeType();
$attachment->path = $savePath;
$attachment->size = ($size/1000);
$attachment->save();
}
Is it possible to customize the chunk configuration in Filepond such that the chunk information is provided to the upload server:
as query parameters instead of headers
with custom query parameter names instead of Upload-Length, Upload-Name, and Upload-Offset
I am trying to fit Filepond's chunk implementation to a third party upload endpoint that I don't have control over.
I have found the Advanced configuration where you provide a process function which I've played with a little bit to see what comes through the options param -- however that appears (I think) to make the chunking calculations my responsibility. My original thought was to manipulate the options.chunkServer.url to include the query params I need but I don't believe this processes individual chunks.
In case it makes a difference, this is being done in React using the react-filepond package.
I made and implementation in Laravel 6 using Traits and some "bad practices" (I didn't have time because ... release in prod) to join chunks into a file
Basically:
post to get unique id folder to storage
get chunks and join together
profit!
Here's the full code:
<?php
namespace App\Http\Traits\Upload;
use Closure;
use Faker\Factory as Faker;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
trait Uploadeable
{
public function uploadFileInStorage(Request $request, Closure $closure)
{
// get the nex offset for next chunk send
if (($request->isMethod('options') || $request->isMethod('head')) && $request->has('patch')) {
//get the temp dir
$dir = $request->patch . DIRECTORY_SEPARATOR;
// reead all chunks in directory
$patch = collect(Storage::files($dir))
->sortBy(function ($file) {
return Storage::lastModified($file);
});
// read offsets for calculate
$offsets = array();
$size = 0;
$last_offset = 0;
foreach ($patch as $filename) {
$size = Storage::size($filename);
list($dir, $offset) = explode('file.patch.', $filename, 2);
array_push($offsets, $offset);
if ($offset > 0 && !in_array($offset - $size, $offsets)) {
$last_offset = $offset - $size;
break;
}
// last offset is at least next offset
$last_offset = $offset + $size;
}
// return offset
return response($last_offset, 200)
->header('Upload-Offset', $last_offset);
}
// chunks
if ($request->isMethod('patch') && $request->has('patch')) {
// get the temp dir
$dir = $request->patch . DIRECTORY_SEPARATOR;
// read headers
$offset = $request->header('upload-offset');
$length = $request->header('upload-length');
// should be numeric values, else exit
if (!is_numeric($offset) || !is_numeric($length)) {
return response('', 400);
}
// get the file name
$name = $request->header('Upload-Name');
// sleep server for get a diference between file created to sort
usleep(500000);
// storage the chunk with name + offset
Storage::put($dir . 'file.patch.' . $offset, $request->getContent());
// calculate total size of patches
$size = 0;
$patch = Storage::files($dir);
foreach ($patch as $filename) {
$size += Storage::size($filename);
}
// make the final file
if ($size == $length) {
// read all chunks in directory
$files = collect(Storage::files($dir))
->sortBy(function ($file) {
return Storage::lastModified($file);
});
// create output file
//Log::info(storage_path('app'));
$new_file_name = $final_name = trim(storage_path('app') . DIRECTORY_SEPARATOR . $dir . $name);
$file_handle = fopen($new_file_name, 'w');
// write patches to file
foreach ($files as $filename) {
// get offset from filename
list($dir, $offset) = explode('.patch.', $filename, 2);
// read chunk
$patch_handle = fopen(storage_path('app') . DIRECTORY_SEPARATOR . trim($filename), 'r');
$patch_contents = fread($patch_handle, filesize(storage_path('app') . DIRECTORY_SEPARATOR . trim($filename)));
fclose($patch_handle);
// apply patch
fseek($file_handle, $offset);
fwrite($file_handle, $patch_contents);
}
// done with file
fclose($file_handle);
// file permission (prefered 0755)
chmod($final_name, 0777);
// remove patches
foreach ($patch as $filename) {
$new_file_name = storage_path('app') . DIRECTORY_SEPARATOR . trim($filename);
unlink($new_file_name);
}
// simple class (no time to explain)
$file = new UploadedFile(
$final_name,
basename($final_name),
mime_content_type($final_name),
filesize($final_name),
false
);
$dir = $request->patch . DIRECTORY_SEPARATOR;
$object = new \stdClass();
$object->full_path = (string)$file->getPathname();
$object->directory = (string)($dir);
$object->path = (string)($dir . basename($final_name));
$object->name = (string)$file->getClientOriginalName();
$object->mime_type = (string)$file->getClientMimeType();
$object->extension = (string)$file->getExtension();
$object->size = (string)$this->formatSizeUnits($file->getSize());
// exec closure
$closure($file, (object)$object, $request);
}
// response
return response()->json([
'message' => 'Archivo subido correctamente.',
'filename' => $name
], 200);
}
// get dir unique id folder temp
if ($request->isMethod('post')) {
$faker = Faker::create();
$unique_id = $faker->uuid . '-' . time();
$unique_folder_path = $unique_id;
// create directory
Storage::makeDirectory($unique_folder_path);
// permisos directorio
chmod(storage_path('app') . DIRECTORY_SEPARATOR . $unique_folder_path . DIRECTORY_SEPARATOR, 0777);
// response with folder
return response($unique_id, 200)
->header('Content-Type', 'text/plain');
}
}
private function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
$bytes = $bytes . ' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes . ' byte';
} else {
$bytes = '0 bytes';
}
return $bytes;
}
}
ยดยดยด
I tried hard, just wanna display barcode image in A1 cell. it works well in html. But not in PhpExcel. thanks in advance.
$generator = new BarcodeGeneratorPNG();
$wizard = new PHPExcel_Helper_HTML();
$cellText = '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode('081231723837', $generator::TYPE_CODE_128)) . '">';
$richText = $wizard->toRichTextObject($cellText);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $richText);
Try whith:
https://github.com/davidscotttufts/php-barcode
generating barcode.png
$filepath = 'barcode.png';
$text = '1234567890123';
$size = "80";
$orientation = "horizontal";
$code_type = "code128";
$print = "true";
$sizefactor = "0.8";
barcode( $filepath,$text, $size, $orientation, $code_type, $print, $sizefactor );
Report:
$imgBarcode = imagecreatefrompng('bar_code.png');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setDescription('barcode');
$objDrawing->setImageResource($imgBarcode);
$objDrawing->setHeight(100);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
I have an error while writing the code to refund a transaction from Sagepay:
3047 : Invalid VPSTxId format.
The code I used here is:
if ($this->config->get('sagepay_direct_v3_test') == 'live') {
$url = 'https://live.sagepay.com/gateway/service/refund.vsp';
$payment_data['VPSProtocol'] = '3.00';
} elseif ($this->config->get('sagepay_direct_v3_test') == 'test') {
$url = 'https://test.sagepay.com/gateway/service/refund.vsp';
$payment_data['VPSProtocol'] = '3.00';
}
$this->load->model('checkout/order');
$this->load->model('payment/sagepay_direct_v3');
$order_info = $this->model_checkout_order->getOrder($order_id);
$sagepay_direct_v3_order = $this->getOrder($order_info['advance_order_confirmed_id']);
$params = array();
$params['VPSProtocol'] = urlencode($payment_data['VPSProtocol']);
$params['TxType'] = urlencode('REFUND');
$params['Vendor'] = urlencode($this->config->get('sagepay_direct_v3_vendor'));
$params['VendorTxCode'] = $this->request->get['order_id'] . 'T' . strftime("%Y%m%d%H%M%S") . mt_rand(1, 999);
$params['Amount'] = $this->currency->format($order_info['total'], $order_info['currency_code'], false, false);
$params['Currency'] = $this->currency->getCode();
$params['Description'] = urlencode(substr($this->config->get('config_name'), 0, 100));
$params['RelatedVPSTxId'] = urlencode($sagepay_direct_v3_order['VPSTxId']);
$params['RelatedVendorTxCode'] = urlencode($this->request->get['order_id'] . 'T' . strftime("%Y%m%d%H%M%S") . mt_rand(1, 999));
$params['RelatedSecurityKey'] = urlencode($sagepay_direct_v3_order['SecurityKey']);
$params['RelatedTxAuthNo'] = urlencode($sagepay_direct_v3_order['TxAuthNo']);
$response = $this->model_payment_sagepay_direct_v3->sendCurl($url, $params);
var_dump( $response );
I suspect this is because you are urlencoding the RelatedVPSTxId (and other fields).