Background
At our company the less work there is to manually do the better. Our marketing lady approached me and asked if there is a way to automatically generate a business card with PHP. I told her it should not be an issue if she could save the template as a SVG file.
So she did. Editing the text is pretty easy as I just find the detail spaces and replace it with the user input.
The Problem
The biggest issue that I’m having at the moment is the QR Code that I get from the Google Charts API. The QR Code contains the business card data for easy saving to the phone. The QR Code is retrieved with this call to the Google Charts API:
$request = "http://chart.apis.google.com/chart?chs=500x500&cht=qr&chld=M&chl=" . urlencode($vcard);
where $vcard is the VCard that I built with PHP. This works, as the VCard can be used when scanning the QR Code.
What I have tried
Since any image in a SVG file is encoded to text, I tried the following methods:
file_get_contents($request); // Seems the encoding is of as there are weird characters in the result
img2php.class // Downloaded from PHP Classes, but only converts images in a directory
Additional Information
What I have realised is that the QR Code is a PNG that is generated. I don’t know if that will affect the answer.
Also, the current QR Code in the business card is encoded as follows:
<image overflow="visible" width="200" height="200"
xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEBZwFnAAD/
7AARRHVja3kAAQAEAAAAHgAA/+4AIUFkb2JlAGTAAAAAAQMA
EAMCAwYAAAReAAALrQAAHjX/2wCEABALCwsMCxAMDBAXDw0PFxsUEBAUGx8XFxcXFx8eFxoaGhoX
Hh4jJSclIx4vLzMzLy9AQEBAQEBAQEBAQEBAQEABEQ8PERMRFRISFRQRFBE
** random gibberish **
saaZZmll6QMgIAGkLbb5qxkhmSE47OSXBN9YXg81d+i5LV36LktWPktmRMsMqSFQrXIRg1v8bjuM
2IBmJtpbymu/Rclq79FyWrv0XJasl5p0mGQqABARbQW4fP8A/s//
2Q==" transform="matrix(0.2003 0 0 0.2003 56.5049 191.0313)">
</image>
Question
Is there a way that I can get the QR Code from Google API and then encode it to string and add it to the SVG template?
And I’ve managed to fix it myself… And by using some more Googling.
First, pull the image from the Google API:
Then just use base64_encode to encode it to a string:
And that works. The QR Code scans correctly. 😀