QR Image - Simple CMSMS plugin for generating QR code

For one of my last projects i was in need of QR Codes that would be generated from URL of current page.
With help of Google Charts API this is failry easy to achieve but i also wanted to prevent unneeded http requests and wanted a nice way of caching images, so at last i ended up with QR Image plugin.

Well a quick use of API would simply be like this:

<img src="https://chart.googleapis.com/chart?chs=50x50&amp;cht=qr&amp;chl={get_current_url}&amp;chld=L|1&amp;choe=UTF-8" />

This way you would see QR code generated from URL.

So what does this plugin?

As i mentioned i wasn't happy about the fact that i would need extra http requests and images not cached so i ended up with following plugin code. 

function smarty_cms_function_qr_image($params, &$smarty)
{
    $gCms = cmsms();
    
    $qr_image  = '';
    $start_img = '<img src=';
    $close_img = ' />';
    $data_uri  = $params['data_uri'];
    $path      = $params['path'];
    
    // get image from google charts api	
    $image_url_qr = 'https://chart.googleapis.com/chart?chs=' . $params['width'] . 'x' . $params['height'] . '&cht=qr&chl=' . $params['url'] . '&choe=UTF-8';
    // save image in specified folder relative to uploads	
    if (!empty($params['path'])) {
        $image_path_qr = $gCms->config['image_uploads_path'] . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $image_filename_qr;
    } else {
        // save image in configured image uploads folder		
        $image_path_qr = $gCms->config['image_uploads_path'] . DIRECTORY_SEPARATOR . $image_filename_qr;
    }
    $image_filename_qr = md5($params['url']) . '_' . date(Y . m . d) . '' . '.gif';
    if (!file_exists($image_path_qr . $image_filename_qr)) {
        
        $image_to_fetch   = file_get_contents($image_url_qr);
        $local_image_file = fopen($image_path_qr . $image_filename_qr . $extension, 'w');
        chmod($image_path_qr . $image_filename_qr, 0755);
        fwrite($local_image_file, $image_to_fetch);
        fclose($local_image_file);
    }
    // if file existis construct image tag	
    if (file_exists($image_path_qr)) {
        // if param data_uri is set or 0 show base64 image		
        if ($data_uri != 0) {
            if (!empty($params['path'])) {
                $get_image = $gCms->config['image_uploads_url'] . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $image_filename_qr;
                $qr_image  = $start_img .= '"data:image/gif;base64,' . base64_encode(file_get_contents($get_image)) . '"';
            } else {
                $get_image = $gCms->config['image_uploads_url'] . DIRECTORY_SEPARATOR . $image_filename_qr;
                $qr_image  = $start_img .= '"data:image/gif;base64,' . base64_encode(file_get_contents($get_image)) . '"';
            }
            // else show gif image		
        } else {
            if (!empty($params['path'])) {
                $qr_image = $start_img .= '"' . $gCms->config['image_uploads_url'] . '/' . $path . '/' . $image_filename_qr . '"';
            } else {
                $qr_image = $start_img .= '"' . $gCms->config['image_uploads_url'] . '/' . $image_filename_qr . '"';
            }
        }
        // set image width		
        if (!empty($params['width'])) {
            $qr_image .= ' width="' . $params['width'] . '"';
        } else {
            $qr_image .= ' width="100"'; // else set 100px		
        }
        // set image height		
        if (!empty($params['height'])) {
            $qr_image .= ' height="' . $params['height'] . '"';
        } else {
            $qr_image .= ' height="100"'; // else set 100px
        }
        if (!empty($params['alt'])) {
            $alt = $params['alt'];
        } else {
            $alt = $params['url'];
        }
        $qr_image .= ' alt="' . $alt . '"';
        if (!empty($params['title'])) {
            $title = $params['title'];
        } else {
            $title = $params['url'];
        }
        $qr_image .= ' title="' . $title . '"';
        if (!empty($params['class'])) {
            $qr_image .= ' class="' . $params['class'] . '"';
        }
        $qr_image .= $close_img;
    } else {
        $qr_image = '<p style="color:#f00">Oh no. Something is wrong.<br />Image was not found.</p>';
    }
    if (isset($params['assign'])) {
        $smarty->assign(trim($params['assign']), $qr_image);
        return;
    }
    return $qr_image;
}
?>

So what does this code do?

When url parameter is entered, the plugin calls Google Charts API path to QR code image, then it saves .gif image in /uploads/images folder, if path parameter wasn't specified and outputs our saved .gif image.

Example of tag you could insert would look like this (where $entry->url would be in News module) :

{qr_image url=$entry->url path='qr-folder' width='120' height='120' alt=$entry->title title=$entry->title data_uri='1'}

So example above would output a QR code image of 120x120 size in News summary module from News detail link and convert it to base64 image, reducing http requests.

Anyway if you think you could use it somewhere go and grab it here.
There is also another plugin created by Andrea Weichbrodt called QR Code that does similar job so if you prefer it go grab that one.
It's funny as we both had same idea and registered projects on same day :-)

Comments