QR code generator - API

A QR code is a special 2-dimensional code that is used to transmit short information, e.g. to a mobile phone.

QR codes can be easily generated by simply inserting an image on Google's servers.

For example:

QR code
html
<img src="https://chart.apis.google.com/chart?cht=qr&chs=100x100&choe=UTF-8&chld=H%7C0&chl=https://php.baraja.cz" alt="QR code">

We can set 3 parameters:

  • Size in px (vertical and horizontal)
  • Encoding (I recommend UTF-8)
  • Address (URL to the page, phone number, ...)

TIP: If there is a mobile version of the site, link to that instead.

In most cases, your QR code will be processed by your mobile phone.

It is very easy to write your own function for embedding:

php
function getQrCode(string $url, int $size = 128, string $charset = 'UTF-8'): string
{
$size = $size < 16 ? 16 : ($size > 2048 ? 2048 : $size);
return '<img src="https://chart.apis.google.com/chart?cht=qr&chs=
. $size . 'x' . $size
. '&choe=' . urlencode($charset)
. '&chld=H%7C0&chl=' . urlencode($url)
. '">';
}
echo getQrCode('https://php.baraja.cz');

Newsletter

Nejlepsi tipy a triky o PHP do Vaseho e-mailu. Clanky a novinky nejen ze sveta PHP a programovani.