QR Code Generator - with image alt="<?php echo htmlspecialchars($staffRecord['title']); ?>" attribute

4 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: May 16, 2014   (RSS)

By Mikey - May 13, 2014

Does anyone know if it's possible to add an alt="<?php echo htmlspecialchars($staffRecord['title']); ?>" attribute to a QR Code generated by the Add-On "QR Code Generator"? I know it can be done with javascript, but I need to apply the alt="" attribute to the QR Code image using PHP and/or html.

By gregThomas - May 15, 2014

Hi Zicky,

I can have a look to see if it would be possible to easily change the plugin code to allow this feature. But there are currently two different QR code plugins, could you let me know which of the two you are you using?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - May 16, 2014

Hi Zicky,

I've found a solution that will allow you to add alt text to an image.

Firstly, around line 11 of QRCodeGenerator.php, you'll need to add this line:

$GLOBALS['qrCodeGenerator']['defaultSize']   = 200;                 //
$GLOBALS['qrCodeGenerator']['defaultMargin'] = 0;                   //
$GLOBALS['qrCodeGenerator']['defaultAlt']    = 'QR Code';           //
$GLOBALS['qrCodeGenerator']['defaultMode']   = 'img';               // img or url (for image tag or image url)
$GLOBALS['qrCodeGenerator']['pluginName']    = "QR Code Generator"; // displayed in plugin menu

Then around line 38, you need to add this line:

  if (!@$options['size'])   { $options['size']   = $GLOBALS['qrCodeGenerator']['defaultSize']; }
  if (!@$options['margin']) { $options['margin'] = $GLOBALS['qrCodeGenerator']['defaultMargin']; }
  if (!@$options['mode'])   { $options['mode']   = $GLOBALS['qrCodeGenerator']['defaultMode']; }
  if (!@$options['alt'])    { $options['alt']    = $GLOBALS['qrCodeGenerator']['defaultAlt']; }

Then around line 58 of the file you'll need to add this line:

  // build chart url
  $height    = $options['size'];
  $width     = $options['size'];
  $alt       = htmlspecialchars($options['alt']);
  $chartURL  = "http://chart.apis.google.com/chart?"; // docs: http://code.google.com/apis/chart/docs/gallery/qr_codes.html

Then you need to modify lines 86 - 92 so that we we're allowed to use our new alt option:

  // types to options
  $typeOptions = array( 'text'          => array('alt', 'type', 'size', 'margin', 'mode', 'text'),
                        'url'           => array('alt', 'type', 'size', 'margin', 'mode', 'url'),
                        'email'         => array('alt', 'type', 'size', 'margin', 'mode', 'email'),
                        'event'         => array('alt', 'type', 'size', 'margin', 'mode', 'start', 'end', 'title'),
                        'contact'       => array('alt', 'type', 'size', 'margin', 'mode', 'lastName', 'firstName', 'address', 'phone', 'email'),
                        'facebook_like' => array('alt', 'type', 'size', 'margin', 'mode', 'url'));

Finally, you'll need to modify line 74 to look like this:

  if     ($options['mode'] == 'img') { $output = "<img src='$chartURL' alt='$alt' height='$height' width='$width' />"; }

Now when you create an QR code you can add an option called alt, and it's text will appear as the alt text for an image. Here is an example of how to use it:

  <?php
  echo qrCode(array(
    'type'   => 'text',
    'text'   => 'You can set these options for any of the QR codes!',
    'alt'    =>  'This is a test and stuff',
    'size'   => '100', // height & width in pixels
    'margin' => '10',  // margin around the QR code
    'mode'   => 'img', // either img or url.  img for image tag and url for image url only.
  ));
?>

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com