如何在 Laravel 包 SimpleSoftwareIO/simple-qrcode 中插入 QRCode 格式的 PNG 文本?
How to insert some text in QRCode format PNG in Laravel package SimpleSoftwareIO/simple-qrcode?
我正在使用 https://github.com/SimpleSoftwareIO/simple-qrcode 这个包在 laravel 中实现二维码。
我想在二维码下插入一些PNG格式的文字。我怎样才能做到这一点?这个包支持下载PNG格式的二维码吗?
找了半天。
我可以通过这个答案 and i save it in folder by this tutorial https://hdtuto.com/article/how-to-convert-base64-to-image-and-save-it-using-php 从一些文本创建 PNG 图像。
然后我使用合并功能将图像放入 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);
我正在使用 https://github.com/SimpleSoftwareIO/simple-qrcode 这个包在 laravel 中实现二维码。
我想在二维码下插入一些PNG格式的文字。我怎样才能做到这一点?这个包支持下载PNG格式的二维码吗?
找了半天。 我可以通过这个答案 and i save it in folder by this tutorial https://hdtuto.com/article/how-to-convert-base64-to-image-and-save-it-using-php 从一些文本创建 PNG 图像。 然后我使用合并功能将图像放入 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);