我正在使用https://github.com/SimpleSoftwareIO/simple-qrcode这个程序包在laravel中实现qrcode。
我想在QRCode下以PNG格式插入一些文本。我怎样才能做到这一点?该软件包是否支持下载PNG格式的QRCode? p>
答案 0 :(得分:0)
经过长时间的搜索。 我可以通过此答案https://stackoverflow.com/a/49704951/8071577从某些文本创建PNG图片,并通过本教程https://hdtuto.com/article/how-to-convert-base64-to-image-and-save-it-using-php将其保存在文件夹中。 然后我使用合并功能将图像转换为QRCode格式PNG。 这是我的代码。
// Create image (base64) from some text
$string = ' Your text here ';
$width = 150;
$height = 150;
$im = @imagecreate ($width, $height);
$text_color = imagecolorallocate ($im, 0, 0, 0); //black text
// white background
// $background_color = imagecolorallocate ($im, 255, 255, 255);
// transparent background
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, true);
imagestring ($im, $font, 40, 130, $string, $text_color);
ob_start();
imagepng($im);
$imstr = base64_encode(ob_get_clean());
imagedestroy($im);
// Save Image in folder from string base64
$img = 'data:image/png;base64,'.$imstr;
$image_parts = explode(";base64,", $img);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$folderPath = \App\UserPet::TEXTQR_DIRECTORY;
$file = $folderPath . '/' . $pet->code_qr . '.jpg';
// MOve to folder
file_put_contents($file, $image_base64);
// Generate QRCode PNG and save put image above with merge funtion
$pathDirectory = storage_path(\App\UserPet::QRCODE_DIRECTORY.'/'.$pet->code_qr.'.png');
QrCode::format('png')->margin(10)->merge(\App\UserPet::TEXTQR_DIRECTORY.'/'.$pet->code_qr.'.jpg', 1, true)->size(200)->generate('hihihi - '.route('pet.detail', ['id' => $pet->code_qr]), $pathDirectory);