[Drupal 7] How to generate QR code from text in a Drupal site?

| | 2 min read

One of our clients required us to create a Drupal based web application to generate QR code from text in their Drupal site. If you are looking for a way to generate QR code from any text in your Drupal site then read on to find out more .

The QR code which is an abbreviation for Quick Response Code was developed in Japan in the early 90’s as a 2d matrix bar code and it has gained rapid popularity since it could be used on any kind of print media to deliver machine readable encoded information.

Follow the steps below to enable your site to generate QR code.

  • Firstly we need to get hold of the QR code library
  • Copy and paste the uncompressed library file to
     sites/all/libraries/phpqrcode/ 

    in your Drupal site.

  • With that settled we are going to write the code for generating the QR code for any given piece of text.
  • You could place the following code in the node preprocess function in the template.php file of one of the themes.
    
    //defining the base path of your Drupal installation
    global $base_url;
    
    function theme_name_preprocess_node(&$vars) {
      // First we need to create the path for the img which will be generated as a png file. 
      // This will be automatically created using the code below
      $qrpath = variable_get('file_public_path', conf_path() . '/files') . "/images/" . $vars['nid'] . '-C-qr.png'; 
      
      // This is the  data to be added to the QR code.  
      $text = "Some text";
      
      // If the path is available we proceed to generate the form 
      if (!file_exists($qrpath)) {
        $qrpath =  theme_name_qrcode($text , $qrpath);
      }
      
      // This will print the path to the image in a theme
      $vars['qrimgpath'] = $qrpath;
    }
    
    function theme_name_qrcode($text , $qrpath) {
      // Including the qrcode library and generating the code
      include("sites/all/libraries/phpqrcode/qrlib.php");
      QRcode::png($text, $qrpath);
    
      return $qrpath;
    }
  • Hope that helps

    Are you looking for a way to build a similar high quality web application using Drupal? We are Drupal experts and we can provide a wide range of Drupal module development services to get you started with your application. Get in touch with us to know more.