如何在Laravel中生成多个QR码

发布于 2025-02-13 18:24:05 字数 1122 浏览 0 评论 0原文

我正在研究一个项目,必须根据数据库中的成员数量生成QR码。每个QRCode都会有一个唯一的字符串,因此我正在生成一个30个Chracters的随机字符串。到目前为止,我在数据库中有5000名成员,我需要生成5000个QR码。 当我运行应用程序时,它会从数据库中获取记录总数,在foreach循环中,我正在生成QR码,但是服务器在40-50秒后响应503错误,尽管我将php ini设置的限制设置为最大执行时间为3600下面是我的代码。我需要有关如何实现目标的建议,什么是最佳实践以及如何通过独特的字符串批量生成QR码,并避免复制QR码并将QR映像存储在存储文件夹中,以及其在数据库中的文件名,以便以后用来打印它。

$data = \DB::connection('mysql2')->table('basicinfo')->where('is_approved','=','1')->whereIn('classOfMembership',$member_arr)->get(); //this return 4808 records

foreach($data as $d){
 $randomStr = Str::random(33);
 $image = QrCode::format('png')
   ->size(200)->errorCorrection('H')
   ->generate('https://example.com/'.$randomStr);
 $time_now = time();
 $output_file = '/qr-code/election-'.$request->election_year.'/img-' . $time_now . '.png';
 $image_name = 'img-' . $time_now . '.png';
 $insert_arr[] = [
   'election_year' => $request->election_year,
   'position' => $request->post,
   'qr_hash' => $randomStr,
   'qr_code_image' => $image_name
 ];
 Storage::disk('local')->put($output_file, $image); 
}
Ballot::insert($insert_arr);

I am working on a project in which i have to generate QR codes based on number of members in database. Each qrcode will have a unique string so i am generating a random string of 30 chracters. As of now i have 5000 members in database and i need to generate 5000 QR Codes.
When i run the application it fetches total number of records from database and in foreach loop i am generating QR Code, but the server responds to 503 error after 40-50 seconds though i set the limit in php ini setting for max execution time at 3600. below is the my code. I need advice on how can i achieve my goal what is best practice and how i can generate QR code in bulk with unique string and avoid duplicating qr code and store the qr image in storage folder as well its filename in database for later use to print it.

$data = \DB::connection('mysql2')->table('basicinfo')->where('is_approved','=','1')->whereIn('classOfMembership',$member_arr)->get(); //this return 4808 records

foreach($data as $d){
 $randomStr = Str::random(33);
 $image = QrCode::format('png')
   ->size(200)->errorCorrection('H')
   ->generate('https://example.com/'.$randomStr);
 $time_now = time();
 $output_file = '/qr-code/election-'.$request->election_year.'/img-' . $time_now . '.png';
 $image_name = 'img-' . $time_now . '.png';
 $insert_arr[] = [
   'election_year' => $request->election_year,
   'position' => $request->post,
   'qr_hash' => $randomStr,
   'qr_code_image' => $image_name
 ];
 Storage::disk('local')->put($output_file, $image); 
}
Ballot::insert($insert_arr);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文