PHP Requires Chilkat v11.7.0+
Chilkat2 PHP
Generate a QR Code PNG File
See more QR Code Examples
Demonstrates the simplest way to create a QR code: the text to encode goes in payload.text, and output.path names the PNG file to write. QrCode chooses the smallest QR version that holds the text at error-correction level M, and the response reports what it chose and the size of the image. The request and the response are both JSON objects, and they must be two different objects.Chilkat PHP Downloads
<?php
use Chilkat2\Formats;
use Chilkat2\JsonObject;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
$json = new JsonObject();
$json->UpdateString('payload.text', 'https://www.chilkatsoft.com/');
// Each module (one of the small squares) is drawn 8 pixels wide. The default is 4.
$json->UpdateInt('render.moduleSize', 8);
// Write the image to a file. An existing file is replaced only when output.overwrite is true.
// Otherwise the call fails and the existing file is left as it was.
$json->UpdateString('output.path', 'qa_output/qr_chilkat.png');
$json->UpdateBool('output.overwrite', true);
// The image is also returned in the response as base64 unless output.return is "none".
$json->UpdateString('output.return', 'none');
$fmt = new Formats();
$jsonOut = new JsonObject();
try {
$fmt->QrCode('generate', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'file: ', $jsonOut->StringOf('outputs[0].path'), "\n";
echo 'image: ', $jsonOut->IntOf('outputs[0].width'), ' x ', $jsonOut->IntOf('outputs[0].height'), ' pixels, ', $jsonOut->IntOf('outputs[0].byteCount'), " bytes\n";
echo 'QR version: ', $jsonOut->IntOf('symbols[0].selectedEncoding.version'), "\n";
echo 'error correction: ', $jsonOut->StringOf('symbols[0].selectedEncoding.errorCorrection'), "\n";
echo 'modules: ', $jsonOut->IntOf('symbols[0].dimensions.modulesWide'), ' x ', $jsonOut->IntOf('symbols[0].dimensions.modulesHigh'), "\n";
// The image written to qa_output/qr_chilkat.png:
//
// Sample output:
// file: qa_output/qr_chilkat.png
// image: 296 x 296 pixels, 1251 bytes
// QR version: 3
// error correction: M
// modules: 29 x 29