VashGH
VashGH

Reputation: 263

Small file to QR Code

I'm basically trying to use Google's QR Code API to create a QR code from an array of bytes. I've tried passing Google's web backend an array of bytes for the 'chl' variable (data for the QR code), but it never seems to like it. I've used Google's PHP example as the basis for my code, but if anyone knows of an alternative way to simply convert an array of bytes into a QR code using Google's API, that's the only goal.

<?php
// Create some random text-encoded data for a QR code.
//header('content-type: image/png');    
$url = 'https://chart.googleapis.com/chart?chid=' . md5(uniqid(rand(), true));
$chd = file($_FILES["file"]["tmp_name"]);

// Add image type, image size, and data to params.
$qrcode = array(
'cht' => 'qr',
'chs' => '300x300',
'choe' => 'ISO-8859-1',
'chl' => $chd);


// Send the request, and print out the returned bytes.
$context = stream_context_create(
array('http' => array(
  'method' => 'POST',
  'content' => http_build_query($qrcode))));
fpassthru(fopen($url, 'r', false, $context));
?>

There's the PHP, handling an uploaded file and sending to Google's API via a byte array.

Upvotes: 2

Views: 930

Answers (2)

Sean Owen
Sean Owen

Reputation: 66886

You can't encode a byte array using the Google Chart Server -- it operates on strings. I think these are valiant attempts at hacking around that but won't work in the end for the reason you've found.

You should just use the encoder in the zxing project locally. It also operates on strings, but, you can actually easily change or inject a call that specifies a byte array directly that way.

Upvotes: 0

goat
goat

Reputation: 31813

The file() function gives an array of strings, split on new lines. I think you just want a string. see file_get_contents()

make sure to

echo http_build_query($qrcode);

to help you debug.

Upvotes: 1

Related Questions