crawl bot
Infoscript - Desenvolvimento web
infoscript_logo
PHP / MySQL
Gerador QRCode
Publicado por antlou em 28/05/2017

Vou mostrar e desenvolver uma class muito simples em linguagem PHP que gera imagem de QrCode link e utilizando o serviço Google Chart API.

Este objecto envia uma solicitação HTTP para o servidor Web da API do Google Chart a solicitar imagem PNG que represente o código QR.

A imagem de código QR gerada pode ser retornada como uma string, servida para download ou retornada como uma string de URL.

Passemos então ao codigo.
QRcode_class.php
<?php  
 
/*
    ********************************************
    * Lou_QRcode.class                        
    * Generator QR codes               
    * Author:antlou                            
    * Version:1.0                              
    * Date:2017/05/27                          
    ********************************************
    */ 
class qrcode  
{  
    private 
$data;  
      
    
//creating code with link mtadata  
    
public function link($url){  
        if (
preg_match('/^http:\/\//'$url
    || 
preg_match('/^https:\/\//'$url))   
        {  
            
$this->data $url;  
        }  
        else  
        {  
            
$this->data "http://".$url;  
        }  
    }   
    
//creating text qr code  
    
public function text($text){  
        
$this->data $text;  
    }    
    
//creating code with phone   
    
public function phone_number($phone){  
        
$this->data "TEL:".$phone;  
    }   
    
//creating code with mecard metadata  
    
public function contact_info
    
($name$address$phone$email){  
        
$this->data "MECARD:N:".$name.";ADR:".$address."
    ;TEL:"
.$phone.";EMAIL:".$email.";;";  
    }    
    
//creating code with geo location metadata  
    
public function geo($lat$lon$height){  
        
$this->data "GEO:".$lat.",".$lon.",".$height;  
    }    
    
//getting image  
    
public function get_image
    
($size 150$EC_level 'L'$margin '0'){  
        
$ch curl_init();  
        
$this->data urlencode($this->data);   
        
curl_setopt($chCURLOPT_URL,
     
'http://chart.apis.google.com/chart');  
        
curl_setopt($chCURLOPT_POSTtrue);  
        
curl_setopt($chCURLOPT_POSTFIELDS
    
'chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'
    |'
.$margin.'&chl='.$this->data);  
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);  
        
curl_setopt($chCURLOPT_HEADERfalse);  
        
curl_setopt($chCURLOPT_TIMEOUT30);  

        
$response curl_exec($ch);  
        
curl_close($ch);  
        return 
$response;  
    }       
    
//getting link for image  
    
public function get_link
    
($size 150$EC_level 'L'$margin '0'){  
        
$this->data urlencode($this->data);   
        return 
'http://chart.apis.google.com/chart?
    chs='
.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'
    |'
.$margin.'&chl='.$this->data;  
    }       
    
//forcing image download 
    
public function download_image($file){ 
        
header('Content-Disposition: attachment; filename=QRcode.png'); 
        
header('Content-Type: image/png'); 
        echo 
$file
    }     
    
//save image to server
    
public function save_image
    
($file$path "./QRcode.png"){ 
        
file_put_contents($path$file);
    } 
}  
?>

QRcode.php
<?php
    
/*
    ****************************************
     *QRcode.class                        
    * Generator QR codes               
    * Author:antlou                            
    * Version:1.0                              
    * Date:2017/05/27   
    ****************************************
    */

    
require_once("QRcode_class.php");

$qr = new qrcode();

//link
$qr->link("http://www.infoscript.pt");
echo 
"<p>Link : http://www.infoscript.pt </p>";
echo 
"<p><img src='".$qr->get_link()."' border='0'/></p>";

//text
$qr->text("Hello World!");
echo 
'<p>UTF8 text : "Hello World!"</p>';
echo 
'<p><img src="'.$qr->get_link().'" border="0"/></p>';

//phone number
$qr->phone_number("12345678");
echo 
'<p>Telephone number : "12345678"</p>';
echo 
'<p><img src="'.$qr->get_link().'" border="0"/></p>';

//geo location works on smartphones
//First param - latitude
//Second param - longitude
//Third param - height above earth in meters
$qr->geo("40.902517""-8.491024""100");
echo 
'<p>Geographical location :
 40.902517", "-8.491024", "100"</p>'
;
echo 
"<p><img src='".$qr->get_link()."' border='0'/></p>";

//here is the way to output image
header("Content-type: image/png");
echo 
$qr->get_image();

//and here is the way to force image download
$file $qr->get_image();
$qr->download_image($file)

?>

Resultado:

Link : www.infoscript.pt


Download Código
Espero ter sido útil. Indique grau de satisfação.
1 votos - 5 pontos - 23 downloads
Média : 5,000

Infoscript - Desenvolvimento de websites, e-commerce e programação

Infoscript tem soluções para desenvolvimento web e programação para aproximar os seus clientes às necessidades atuais do mercado.
O design de produto, a criação de websites e lojas online entre outros, são os principais serviços que temos para lhe oferecer.
Contate-nos através do endereço geral@infoscript.pt
Nós teremos uma solução para si à medida.