QR Barcode Implementation in magento
QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode). For more detail about QR code please refer wiki. We can implement this QR code into magento product. So that each product has a got unique barcode.We can easily find out the product description based on this code.
Add below code in your helper class:
Example path: app\code\local\Jute\MYMODULE\Helper\Data.php
/* QR-Code generation – start */
public function callQR_Code($img_url,$img_Sku) {
//first check if the image is fetchable
$img_url = “http://chart.apis.google.com/chart?chs=150×150&cht=qr&chld=L|4&choe=UTF-8&chl=”.$img_url;
$img_info = @getimagesize($img_url);
if(false == $img_info || !isset($img_info[2]) || !($img_info[2] == IMAGETYPE_JPEG || $img_info[2] == IMAGETYPE_PNG || $img_info[2] == IMAGETYPE_JPEG2000 || $img_info[2] == IMAGETYPE_GIF)) {
if(true == $debug)
echo ‘The image doesn\’t seem to exist in the remote server’;
return ”; //return empty string
}
$img_type = $img_info[‘mime’];
$img_type = explode(‘/’,$img_type);
$img_type = $img_type[1];
//calculate the destination image path
$i_dest = ‘QRCODE/’.$img_Sku.’.’.$img_type;
//now try to create the image
if($img_type == ‘jpg’) {
$m_img = @imagecreatefromjpeg($img_url);
} else if($img_type == ‘png’) {
$m_img = @imagecreatefrompng($img_url);
@imagealphablending($m_img, false);
@imagesavealpha($m_img, true);
} else if($img_type == ‘gif’) {
$m_img = @imagecreatefromgif($img_url);
} else {
$m_img = FALSE;
}
//was the attempt successful?
if(FALSE === $m_img) {
if(true == $debug)
echo ‘Can not create image from the URL’;
return ”;
}
//now attempt to save the file on local server
if($img_type == ‘jpg’) {
if(imagejpeg($m_img, $i_dest, 100))
return $i_dest;
else
return ”;
} else if($img_type == ‘png’) {
if(imagepng($m_img, $i_dest, 0))
return $i_dest;
else
return ”;
} else if($img_type == ‘gif’) {
if(imagegif($m_img, $i_dest))
return $i_dest;
else
return ”;
}
return ”;
}
/* QR-Code generation – start */
Call above method in your action (wherever you want you can call)
Example path : app\code\local\Jute\MYMODULE\controllers\Adminhtml\MyController.php
Sample Code:
/* QR-Code Generation – start */
$prSKU = $product->getSku();
$filename = ‘QRCODE/’.$prSKU.”.png”;
if (file_exists($filename)) {
}else{
$purl =$product->getProductUrl();
//$this->callQR_Code($purl,$prSKU);
Mage::helper(‘mymodule’)->callQR_Code($purl,$prSKU);
}
/* QR-Code Generation – start */
Description:
We can call this QR-Code image in PDF also.
Once above implementation is done, while calling on this action, the QR_Code will generate as a “SKU.PNG” format based on the product sku. Generated PNG file will store a specific folder which one created by the customer. For Example: am creating a folder called QRCODE which was placed in root of the folder (Root/QRCODE). If same name already exist (same sku) in the folder then it won’t create new one also it won’t replace. It will insert when there is no sku.png already exist.