php-QRcode generation failed - php
I'm using PHP QR Code library to generate QR codes.
I included the library and after fetching user information from database, I am trying to create qrcode. And then return the path of the generated qrcode to the front end so that I can pass it to an image tag for showing it to users.
I am fetching name, id,email and user image path from database. I want to include user image to the qrcode, so I get the contents and encode it as string.
I'm not getting errors. I checked the folder, qrcode is not being saved.
require_once 'externalLibraries/qrcode/qrlib.php';
// how to build raw content - QRCode with Business Card (VCard) + photo
$tempDir = QRCODE_PATH; //saves temporary directory path
// we building raw data
$codeContents = 'BEGIN:VCARD'."\n";
$codeContents .= 'FN:'.$name."\n";
$codeContents .= 'ID:'.$id."\n";
$codeContents .= 'EMAIL:'.$email."\n";
$codeContents .= 'PHOTO;JPEG;ENCODING=BASE64:'.base64_encode(file_get_contents('../'.$userAvatar))."\n";
$codeContents .= 'END:VCARD';
// generating
QRcode::png($codeContents, $tempDir.$clientid.'.png', 4, 3);
// displaying
return QRCODE_PATH.$clientid.'.png';
Is this the way to generate qrcodes?
Your code is working for me. The image is saved at the pointed location. I used placeholder for your variables though. To display the image you can use:
$imgpath = QRCODE_PATH.$clientid.'.png';
$src = 'data: '.mime_content_type($imgpath).';base64,'.base64_encode(file_get_contents($imgpath));
echo '<img src="'.$src.'">';
Update:
As mentioned by RST in the comments and stated in these answer a QR-Code can only have a limited size. The image you are using might simply be too large. Try using your generation without the image and see if it works. To answer your question in the comment, you can either reseize the image to be smaller, but no other method will help you hence the QR-Code size is limited. Maybe you think about putting a link to the image into the QR-Code.
Related
How to store QR code as image file in to Mysql Database
I`m implementing a simple application using Laravel. just wondering, when I send qr code in email text, does qr code need to be stored in database first to Specify file pass for the image?? If that answer is yes, is there any way that I`m able to store qr code without using form tag?
I don't think you need to store the actual QR code. A QR code is merely a way of representing a string of characters. Often people will put a URL into the QR code. You can probably just store the source data into your db, and generate the QR from the data. If the data is a URL, the device consuming the QR should be able to link to the url which will bring it back to your application. You could put parameters on the end of the URL to allow your app to retrieve the data from your db for that user. You could even use a signed URL so that the end user cannot change it. Here is an article that I found that may help. It's not laravel specific, but will help with the QR code understanding. https://www.kerneldev.com/2018/09/07/qr-codes-in-laravel-complete-guide/
You can do it converting image to base64 and then store it as text. for more information how to encode visit http://php.net/manual/en/function.base64-encode.php and for decode http://php.net/manual/en/function.base64-decode.php example encode: $file_encoded = base64_encode(file_get_contents($file)); //this is stringed data. save this in database. example decode: $file_encoded = base64_decode ($file_encoded); //this will be file.
You could also store the image as a BLOB, which has less overhead as a base64 encoded image and would not be indexed as a searchable string. Even better might be to just store links to binaries in your database as opposed to the data itself.
$data = new ModelName(); $path = '/img/'; if(!\File::exists(public_path($path))) { \File::makeDirectory(public_path($path)); } $file_path = $path . time() . '.png'; $image = \QrCode::format('png') ->merge('img/t.jpg', 0.1, true) ->size(200)->errorCorrection('H') ->generate('A simple example of QR code!', $file_path) $data->file = $file_path; $data->save(); I hope this will help you, it works fine for me.
Whether there generate QR code in my project directory will get mix when multi people using it?
I have a question about the application generate QR code image. I have an application when clients click a button there will generate a QR code image, my way is store in the project library, then print <img> with the url to the screen. then clients can see it. But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix? my code is bellow: function generate_qrcode($url){ $filename = 'hante_qrcode.png'; $errorCorrectionLevel = 'L'; $matrixPointSize = 4; //generate QR code image $o = QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2); echo "<pre>"; print_r($o); print_r('<img src="hante_qrcode.png">'); } if there get mix, how to solve this problem?
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix? yes how to solve this problem? there are two ways to solve this problem you can provide unique name for every files like using timestamp using time() function or with user ID. cause as per you are passing parameters while generating qr code you need to store the file. without saving file also possible but in that case you can't configure pixel size and frame size. you can refer this for PHP QR code-Examples don't store image on server and find some js to generate qr code directly from client side. having a one demo for that check if you can use it var qrcode = new QRCode("qrcode"); qrcode.makeCode('https://stackoverflow.com'); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script> <div id="qrcode"></div>
Of course it will be overwritten. Solution 1 Create unique filename for every image. This way you can save your images for use later. Another benefit of this, you don't have to create image again for same url. $filename = md5($url) . ".png"; if(!file_exists($filename)){ $o = QRcode::png($url, $filename, ...); } echo '<img src="'.$filename.'">'; Solution 2 If you don't want to save images for disk space reasons you can serve image directly. In your code, user sends request to index.php and fetch image address as response. After then browser makes another request to get image. You can return image rather than returning html. // image.php // Still we want to give uniqe filename because we can get another request while one request is processing $filename = md5(microtime) . "_qr.png"; $o = QRcode::png($url, $filename, ...); $image = file_get_contents($filename); // remove the file after stored in a variable unlink($filename); header('Content-Type: image/jpeg'); header('Content-Length: ' . filesize($image)); echo $image; // index.html <img src="image.php?url=someurl">
PNG image required to be converted from JSON and email
So I have used a signature pad from another source and the pad works fine. When I submit the form, it saves the image as JSON code and this is useful as I need to store it within a database as well. However upon submission, I need to convert the code to png and email the image. This is the code I use to convert the code to image, $output2 is the JSON code from the signature capture, signature-to-image.php is the file actually converting the image: require_once 'signature-to-image.php'; $img = sigJsonToImage($output2); // Save to file imagepng($img); // Destroy the image in memory when complete - I destroy the image at the very end of the PHP file This code converts the code fine as I have tested it opening a header with just $img in it and the image appears fine. However when I try to add the png image to the body of the email, it just shows up as Resource id#7. I use the line of code below to send the image: $email_message .= "Signature: ".clean_string($img)."\n"; I am assuming I need to either embed the image within the body of the email (which I am unfamiliar with) or I need to send the image as an attachment (which I am also unfamiliar with). Am I correct in thinking this? which option is the better choice or is there a better alternative? So I write in the signature pad, it captures the signature and saves the image in JSON (as far as I am aware, I have zero experience using JSON) This is an example of the output I would receive: [{"lx":46,"ly":49,"mx":46,"my":48},{"lx":46,"ly":48,"mx":46,"my":49},{"lx":46,"ly":51,"mx":46,"my":48},{"lx":46,"ly":55,"mx":46,"my":51},{"lx":46,"ly":62,"mx":46,"my":55},{"lx":47,"ly":69,"mx":46,"my":62},{"lx":52,"ly":94,"mx":47,"my":69},{"lx":54,"ly":105,"mx":52,"my":94},{"lx":56,"ly":114,"mx":54,"my":105},{"lx":58,"ly":126,"mx":56,"my":114},{"lx":59,"ly":133,"mx":58,"my":126},{"lx":60,"ly":136,"mx":59,"my":133},{"lx":60,"ly":140,"mx":60,"my":136},{"lx":88,"ly":25,"mx":88,"my":24},{"lx":88,"ly":24,"mx":88,"my":25},{"lx":88,"ly":28,"mx":88,"my":24},{"lx":89,"ly":33,"mx":88,"my":28},{"lx":90,"ly":39,"mx":89,"my":33},{"lx":91,"ly":43,"mx":90,"my":39},{"lx":93,"ly":60,"mx":91,"my":43},{"lx":94,"ly":69,"mx":93,"my":60},{"lx":96,"ly":79,"mx":94,"my":69},{"lx":98,"ly":94,"mx":96,"my":79},{"lx":99,"ly":102,"mx":98,"my":94},{"lx":100,"ly":114,"mx":99,"my":102},{"lx":100,"ly":117,"mx":100,"my":114},{"lx":100,"ly":118,"mx":100,"my":117},{"lx":100,"ly":120,"mx":100,"my":118},{"lx":28,"ly":116,"mx":28,"my":115},{"lx":28,"ly":115,"mx":28,"my":116},{"lx":28,"ly":114,"mx":28,"my":115},{"lx":30,"ly":113,"mx":28,"my":114},{"lx":32,"ly":112,"mx":30,"my":113},{"lx":36,"ly":110,"mx":32,"my":112},{"lx":57,"ly":104,"mx":36,"my":110},{"lx":67,"ly":100,"mx":57,"my":104},{"lx":91,"ly":92,"mx":67,"my":100},{"lx":121,"ly":75,"mx":91,"my":92},{"lx":127,"ly":72,"mx":121,"my":75},{"lx":129,"ly":72,"mx":127,"my":72},{"lx":130,"ly":72,"mx":129,"my":72},{"lx":130,"ly":73,"mx":130,"my":72},{"lx":196,"ly":48,"mx":196,"my":47},{"lx":196,"ly":47,"mx":196,"my":48},{"lx":198,"ly":45,"mx":196,"my":47},{"lx":200,"ly":43,"mx":198,"my":45},{"lx":204,"ly":42,"mx":200,"my":43},{"lx":209,"ly":41,"mx":204,"my":42},{"lx":219,"ly":40,"mx":209,"my":41},{"lx":232,"ly":39,"mx":219,"my":40},{"lx":238,"ly":39,"mx":232,"my":39},{"lx":244,"ly":39,"mx":238,"my":39},{"lx":257,"ly":38,"mx":244,"my":39},{"lx":261,"ly":38,"mx":257,"my":38},{"lx":263,"ly":38,"mx":261,"my":38},{"lx":223,"ly":42,"mx":223,"my":41},{"lx":223,"ly":41,"mx":223,"my":42},{"lx":223,"ly":44,"mx":223,"my":41},{"lx":223,"ly":45,"mx":223,"my":44},{"lx":223,"ly":50,"mx":223,"my":45},{"lx":223,"ly":52,"mx":223,"my":50},{"lx":223,"ly":56,"mx":223,"my":52},{"lx":223,"ly":75,"mx":223,"my":56},{"lx":223,"ly":84,"mx":223,"my":75},{"lx":223,"ly":91,"mx":223,"my":84},{"lx":223,"ly":110,"mx":223,"my":91},{"lx":223,"ly":119,"mx":223,"my":110},{"lx":223,"ly":123,"mx":223,"my":119},{"lx":223,"ly":127,"mx":223,"my":123},{"lx":222,"ly":129,"mx":223,"my":127},{"lx":221,"ly":129,"mx":222,"my":129},{"lx":219,"ly":130,"mx":221,"my":129},{"lx":217,"ly":130,"mx":219,"my":130},{"lx":211,"ly":131,"mx":217,"my":130},{"lx":201,"ly":131,"mx":211,"my":131},{"lx":192,"ly":132,"mx":201,"my":131},{"lx":181,"ly":132,"mx":192,"my":132},{"lx":177,"ly":132,"mx":181,"my":132},{"lx":173,"ly":132,"mx":177,"my":132},{"lx":163,"ly":132,"mx":173,"my":132},{"lx":161,"ly":132,"mx":163,"my":132},{"lx":159,"ly":132,"mx":161,"my":132},{"lx":161,"ly":132,"mx":159,"my":132},{"lx":166,"ly":131,"mx":161,"my":132},{"lx":172,"ly":130,"mx":166,"my":131},{"lx":176,"ly":130,"mx":172,"my":130},{"lx":192,"ly":130,"mx":176,"my":130},{"lx":203,"ly":130,"mx":192,"my":130},{"lx":211,"ly":130,"mx":203,"my":130},{"lx":219,"ly":130,"mx":211,"my":130},{"lx":222,"ly":130,"mx":219,"my":130},{"lx":228,"ly":130,"mx":222,"my":130},{"lx":232,"ly":130,"mx":228,"my":130},{"lx":235,"ly":130,"mx":232,"my":130},{"lx":240,"ly":130,"mx":235,"my":130},{"lx":241,"ly":130,"mx":240,"my":130},{"lx":242,"ly":130,"mx":241,"my":130},{"lx":243,"ly":130,"mx":242,"my":130},{"lx":246,"ly":130,"mx":243,"my":130},{"lx":250,"ly":129,"mx":246,"my":130},{"lx":261,"ly":128,"mx":250,"my":129},{"lx":265,"ly":128,"mx":261,"my":128},{"lx":267,"ly":127,"mx":265,"my":128},{"lx":269,"ly":127,"mx":267,"my":127}]
how to retrieve blob file to image in php?
I am using plupload to upload file in my php based website, with large file uploading the file becomes a file named 'blob' without any suffix. I know this is a binary file that contains the raw data, question is how to retrieve the data and save it back as an image file, say .png/.jpg or etc? I tried: $imageString = file_get_contents($blogPath); $image = imagecreatefromstring($imageString); But it gives me some 'Data is not in recognized format...' error, any thoughts? Thanks in advance.
Your call to imagecreatefromstring() should work just fine if your file_get_contents() is working. Use var_dump($imageString) to verify. Did you mean to name your variable $blobPath instead of $blogPath? You don't need to load this image though. Just rename the file. rename($blobPath, 'new/path/here.jpg'); http://php.net/manual/en/function.rename.php
I am storing the uploaded image files for late use, like attaching them to posts or products(my site is e-commerce CMS). I figured that my image file didn't get fully uploaded to the server, the image before upload is 6mb, but the blob file is just 192kb, so my best guess is that what get uploaded is just a chunk instead of the whole package, and yet that brought up another question: how should I take all the pieces and assemble them as one complete image file? As mentioned earlier, I am using plupload for js plugin and php as backend, the backend php code to handle uploading goes like this: move_uploaded_file($_FILES["file"]["tmp_name"], $uploadFolder . $_FILES["file"]["name"]);
Instead of doing that you should do this to display image to the browser <img src="data:image/jpeg;base64,'.base64_encode( $row['blob_image'] ).'"/> I'm not sure what imagecreatefromsting does or how it encodes the image. I looked at the documentation for that function; you're missing: $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl' . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr' . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r' . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg=='; $data = base64_decode($data); <--- this operation
Generating a QR
Hi i need to generate a QR, (QR ISO/IEC 18004:2000) Using some data re= rr= tt= id= Here is an example of a chain. $data = "?re=AAA010101AAA&rr=CAJR820905IP1&tt=116.00&id= 556bef95-8322-4897-ae65-3b5e9de593f8"; I have found this link: http://phpqrcode.sourceforge.net/examples/index.php?example=001 I have not idea at all, of how to build a QR using my data and that library, can anyone point at my stupidity pls.. thanks EDIT Here is the real answer that i get thanks to the advice wich point me in the rigth direction. (thats why i mark it as correct answer). Anyway, someone may need this. To genereate a QR (CBB in other countries) you can use the library http://phpqrcode.sourceforge.net/ Follow this. Create a file to generate the QR, in my case (/common/qrcode.php) Inside this file you must insert the code to generate QR, depending on what you need. This example03 its pretty good for this. include('phpqrcode/qrlib.php'); $param = $_GET['id']; // remember to sanitize that - it is user input! // we need to be sure ours script does not output anything!!! // otherwise it will break up PNG binary! ob_start("callback"); // here DB request or some processing $codeText = 'DEMO - '.$param; // end of processing here $debugLog = ob_get_contents(); ob_end_clean(); // outputs image directly into browser, as PNG stream QRcode::png($codeText); Then in your view or page where you want to display the QR, you can do this, $ourParamId = 1234; echo '<img src="/common/qrcode.php?id='.$ourParamId.'" />'; Hopes this helps someone.
Check out : example 3. A QR Code simply represents a text string. // here DB request or some processing $codeText = 'DEMO - '.$param; $codeText = 'YOUR TEXT GOES HERE';
Well, the link that you have mentioned in the question is the answer: <?php include('../lib/full/qrlib.php'); // outputs image directly into browser, as PNG stream QRcode::png('PHP QR Code :)'); So you can have a page which accepts the data and displays the QR code as an image and you can have a script that reads the image and save it to DB or filesystem. Hope this helps!