Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Created September 9, 2014 16:58
Show Gist options
  • Save khoand0000/75abf2d8ad49c02468c6 to your computer and use it in GitHub Desktop.
Save khoand0000/75abf2d8ad49c02468c6 to your computer and use it in GitHub Desktop.
1 <?php
2 function qr_code($data, $type = "TXT", $size ='150', $ec='L', $margin='0')
3 {
4 $types = array("URL" => "http://", "TEL" => "TEL:", "TXT"=>"", "EMAIL" => "MAILTO:");
5 if(!in_array($type,array("URL", "TEL", "TXT", "EMAIL")))
6 {
7 $type = "TXT";
8 }
9 if (!preg_match('/^'.$types[$type].'/', $data))
10 {
11 $data = str_replace("\\", "", $types[$type]).$data;
12 }
13 $ch = curl_init();
14 $data = urlencode($data);
15 curl_setopt($ch, CURLOPT_URL, 'http://chart.apis.google.com/chart');
16 curl_setopt($ch, CURLOPT_POST, true);
17 curl_setopt($ch, CURLOPT_POSTFIELDS, 'chs='.$size.'x'.$size.'&cht=qr&chld='.$ec.'|'.$margin.'&chl='.$data);
18 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
19 curl_setopt($ch, CURLOPT_HEADER, false);
20 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
21
22 $response = curl_exec($ch);
23
24 curl_close($ch);
25 return $response;
26 }
27
28 header("Content-type: image/png");
29 echo qr_code("http://emoticode.net", "URL");
30
31 ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment