php 二维码生成类
<?php /** * BarcodeQR - Code QR Barcode Image Generator (PNG) * @package BarcodeQR * @category BarcodeQR * @name BarcodeQR * @version 1.0 * @author Shay Anderson 05.11 * @link http://www.shayanderson.com/php/php-qr-code-generator-class.htm * @license http://www.gnu.org/licenses/gpl.html GPL License * This is free software and is distributed WITHOUT ANY WARRANTY */ final class BarcodeQR { /** * Google Chart API URL */ const API_CHART_URL = "http://chart.apis.google.com/chart"; /** * Code data * @var string $_data */ private $_data; /** * Bookmark code * @param string $title * @param string $url */ public function bookmark($title = null, $url = null) { $this->_data = "MEBKM:TITLE:{$title};URL:{$url};;"; } /** * MECARD code * @param string $name * @param string $address * @param string $phone * @param string $email */ public function contact($name = null, $address = null, $phone = null, $email = null) { $this->_data = "MECARD:N:{$name};ADR:{$address};TEL:{$phone};EMAIL:{$email};;"; } /** * Create code with GIF, JPG, etc. * @param string $type * @param string $size * @param string $content */ public function content($type = null, $size = null, $content = null) { $this->_data = "CNTS:TYPE:{$type};LNG:{$size};BODY:{$content};;"; } /** * Generate QR code image * @param int $size * @param string $filename * @return bool */ public function draw($size = 150, $filename = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, self::API_CHART_URL); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, "chs={$size}x{$size}&cht=qr&chl=" . urlencode($this->_data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $img = curl_exec($ch); curl_close($ch); if ($img) { if ($filename) { if (!preg_match("#\.png$#i", $filename)) { $filename .= ".png"; } return file_put_contents($filename, $img); } else { header("Content-type: image/png"); print $img; return true; } } return false; } /** * Email address code * @param string $email * @param string $subject * @param string $message */ public function email($email = null, $subject = null, $message = null) { $this->_data = "MATMSG:TO:{$email};SUB:{$subject};BODY:{$message};;"; } /** * Geo location code * @param string $lat * @param string $lon * @param string $height */ public function geo($lat = null, $lon = null, $height = null) { $this->_data = "GEO:{$lat},{$lon},{$height}"; } /** * Telephone number code * @param string $phone */ public function phone($phone = null) { $this->_data = "TEL:{$phone}"; } /** * SMS code * @param string $phone * @param string $text */ public function sms($phone = null, $text = null) { $this->_data = "SMSTO:{$phone}:{$text}"; } /** * Text code * @param string $text */ public function text($text = null) { $this->_data = $text; } /** * URL code * @param string $url */ public function url($url = null) { $this->_data = preg_match("#^https?\:\/\/#", $url) ? $url : "http://{$url}"; } /** * Wifi code * @param string $type * @param string $ssid * @param string $password */ public function wifi($type = null, $ssid = null, $password = null) { $this->_data = "WIFI:T:{$type};S{$ssid};{$password};;"; } } // 生成二维码 Demo $qrimage_path = ‘/home/www‘; $qr = new BarcodeQR(); $qr_text = ‘[User ID] Content‘; $uri_hash = md5($user_id); $qrimg_file = $qrimage_path . DIRECTORY_SEPARATOR . $uri_hash . ‘.png‘; $qr->text($qr_text); return $qr->draw(154, $qrimg_file);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。