• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

php二维码生成类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?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);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP变量常量发布时间:2022-07-10
下一篇:
phpueditor上传图片到阿里oss发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap