How to Cache Google QR Image using PHP?


In this post, we know the easy way to dynamically obtain a QR image for any given text or URL. So in this article, we will explore the method to save this QR image locally. The advantage of doing so is that all images are saved to your server, which make sure that the images can be loaded without problem. For example, the google servers are sometimes not accessible i.e. blocked in China, therefore, it is useful to save the QR images to your server (the Google QR URL is visited from your server).

Save the following PHP code on your public folder, e.g. https://justyy.com/qr/index.php. Create a cache folder to keep all the QR images.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  $chs = "250x250";
  $chl = "";
  
  if (isset($_GET['chs'])) {
    $chs = trim($_GET['chs']);
  }
  
  if (isset($_GET['chl'])) {
    $chl = trim($_GET['chl']);
  }
    
  if (!$chl) {
    die();
  }
  
  $file = 'cache/' . md5($chl . $chs) . '.png';
  
  header('Content-Type: image/png');
  if (is_file($file)) { // check the cache file
    $im = imagecreatefrompng($file);
    imagepng($im);
    imagedestroy($im);    
  }
  
  // read QR image from Google server
  $google = "https://chart.googleapis.com/chart?chs=$chs&cht=qr&chl=$chl";
  $png = file_get_contents($google);
  
  if ($png) { // save it locally to your server
    file_put_contents($file, $png);
    $im = imagecreatefrompng($file);
    imagepng($im);
    imagedestroy($im);        
  }  
  
  die();
  $chs = "250x250";
  $chl = "";
  
  if (isset($_GET['chs'])) {
    $chs = trim($_GET['chs']);
  }
  
  if (isset($_GET['chl'])) {
    $chl = trim($_GET['chl']);
  }
    
  if (!$chl) {
    die();
  }
  
  $file = 'cache/' . md5($chl . $chs) . '.png';
  
  header('Content-Type: image/png');
  if (is_file($file)) { // check the cache file
    $im = imagecreatefrompng($file);
    imagepng($im);
    imagedestroy($im);    
  }
  
  // read QR image from Google server
  $google = "https://chart.googleapis.com/chart?chs=$chs&cht=qr&chl=$chl";
  $png = file_get_contents($google);
  
  if ($png) { // save it locally to your server
    file_put_contents($file, $png);
    $im = imagecreatefrompng($file);
    imagepng($im);
    imagedestroy($im);        
  }  
  
  die();

As such, the QR images will be saved to the cache folder with the file name equals to the MD5 hash of the text and the QR image size. To get a QR code, you can then visit the URL e.g:

https://justyy.com/qr?chl=https://helloacm.com

The chl parameter is required and the default size chs is 250×250. Note that the first time may be a little bit slower because the VPS server will wait until it fetches the image from the Google server. To reduce the overload of the VPS server, the CloudFlare Cache-Everything rule is set up:

*justyy.com/qr*
Browser Cache TTL: a year
cache Level: Cache Everything
Edge Cache TTL: an hour

Now, the QR image should be served by the CloudFlare CDN, so the speed is improved.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
458 words
Last Post: Can a Win32 C++ Process Kill Itself?
Next Post: C++ Coding Exercise: How to Check if a Large Integer is divisible by 11?

The Permanent URL is: How to Cache Google QR Image using PHP?

Leave a Reply