php實現帶logo二維碼類

來源:互聯網
上載者:User

本文主要和大家介紹php實現建立二維碼類,支援設定尺寸,加入LOGO,描邊、圓角、透明度,等處理。提供完整代碼,示範執行個體及詳細參數說明,方便大家學習使用。 希望能協助到大家。

實現功能如下:

1.建立二維碼
2.加入logo到二維碼中
3.logo可描邊
4.logo可圓角
5.logo可設透明度
6.logo圖片及輸出圖片類型支援png,jpg,gif格式
7.可設定輸出圖片品質

設定參數說明:

ecc
二維碼品質 L-smallest, M, Q, H-best

size
二維碼尺寸 1-50

dest_file
產生的二維碼圖片路徑

quality
產生的圖片品質

logo
logo路徑,為空白表示不加入logo

logo_size
logo尺寸,null表示按二維碼尺寸比例自動計算

logo_outline_size
logo描邊尺寸,null表示按logo尺寸按比例自動計算

logo_outline_color
logo描邊顏色

logo_opacity
logo不透明度 0-100

logo_radius
logo圓角角度 0-30

代碼如下:

PHPQRCode.class.php

<?phprequire_once dirname(__FILE__)."/qrcode/qrlib.php";/** * PHP建立二維碼類 * Date:    2018-03-18 * Author:  fdipzone * Version: 1.0 * * Description: * PHP實現建立二維碼類,支援設定尺寸,加入LOGO,圓角,透明度,等處理。 * * Func: * public  set_config           設定配置 * public  generate             建立二維碼 * private create_qrcode        建立純二維碼圖片 * private add_logo             合拼純二維碼圖片與logo圖片 * private image_outline        圖片對象進行描邊 * private image_fillet         圖片對象進行圓角處理 * private imagecopymerge_alpha 合拼圖片並保留各自透明度 * private create_dirs          建立目錄 * private hex2rgb              hex顏色轉rgb顏色 * private get_file_ext         擷取圖片類型 */class PHPQRCode{ // class start    /** 預設設定 */    private $_config = array(        'ecc' => 'H',                       // 二維碼品質 L-smallest, M, Q, H-best        'size' => 15,                       // 二維碼尺寸 1-50        'dest_file' => 'qrcode.png',        // 建立的二維碼路徑        'quality' => 100,                    // 圖片品質        'logo' => '',                       // logo路徑,為空白表示沒有logo        'logo_size' => null,                // logo尺寸,null表示按二維碼尺寸比例自動計算        'logo_outline_size' => null,        // logo描邊尺寸,null表示按logo尺寸按比例自動計算        'logo_outline_color' => '#FFFFFF',  // logo描邊顏色        'logo_opacity' => 100,              // logo不透明度 0-100        'logo_radius' => 0,                 // logo圓角角度 0-30    );    /**     * 設定配置     * @param  Array   $config 配置內容     */    public function set_config($config){        // 允許設定的配置        $config_keys = array_keys($this->_config);        // 擷取傳入的配置,寫入設定        foreach($config_keys as $k=>$v){            if(isset($config[$v])){                $this->_config[$v] = $config[$v];            }        }    }    /**     * 建立二維碼     * @param  String $data 二維碼內容     * @return String     */    public function generate($data){        // 建立臨時二維碼圖片        $tmp_qrcode_file = $this->create_qrcode($data);        // 合拼臨時二維碼圖片與logo圖片        $this->add_logo($tmp_qrcode_file);        // 刪除臨時二維碼圖片        if($tmp_qrcode_file!='' && file_exists($tmp_qrcode_file)){            unlink($tmp_qrcode_file);        }        return file_exists($this->_config['dest_file'])? $this->_config['dest_file'] : '';    }    /**     * 建立臨時二維碼圖片     * @param  String $data 二維碼內容     * @return String     */    private function create_qrcode($data){        // 臨時二維碼圖片        $tmp_qrcode_file = dirname(__FILE__).'/tmp_qrcode_'.time().mt_rand(100,999).'.png';        // 建立臨時二維碼        QRcode::png($data, $tmp_qrcode_file, $this->_config['ecc'], $this->_config['size'], 2);        // 返回臨時二維碼路徑        return file_exists($tmp_qrcode_file)? $tmp_qrcode_file : '';    }    /**     * 合拼臨時二維碼圖片與logo圖片     * @param String $tmp_qrcode_file 臨時二維碼圖片     */    private function add_logo($tmp_qrcode_file){        // 建立目標檔案夾        $this->create_dirs(dirname($this->_config['dest_file']));        // 擷取靶心圖表片的類型        $dest_ext = $this->get_file_ext($this->_config['dest_file']);        // 需要加入logo        if(file_exists($this->_config['logo'])){            // 建立臨時二維碼圖片對象            $tmp_qrcode_img = imagecreatefrompng($tmp_qrcode_file);            // 擷取臨時二維碼圖片尺寸            list($qrcode_w, $qrcode_h, $qrcode_type) = getimagesize($tmp_qrcode_file);            // 擷取logo圖片尺寸及類型            list($logo_w, $logo_h, $logo_type) = getimagesize($this->_config['logo']);            // 建立logo圖片對象            switch($logo_type){                  case 1: $logo_img = imagecreatefromgif($this->_config['logo']); break;                  case 2: $logo_img = imagecreatefromjpeg($this->_config['logo']); break;                  case 3: $logo_img = imagecreatefrompng($this->_config['logo']); break;                  default: return '';              }            // 設定logo圖片合拼尺寸,沒有設定則按比例自動計算            $new_logo_w = isset($this->_config['logo_size'])? $this->_config['logo_size'] : (int)($qrcode_w/5);            $new_logo_h = isset($this->_config['logo_size'])? $this->_config['logo_size'] : (int)($qrcode_h/5);            // 按設定尺寸調整logo圖片            $new_logo_img = imagecreatetruecolor($new_logo_w, $new_logo_h);            imagecopyresampled($new_logo_img, $logo_img, 0, 0, 0, 0, $new_logo_w, $new_logo_h, $logo_w, $logo_h);            // 判斷是否需要描邊            if(!isset($this->_config['logo_outline_size']) || $this->_config['logo_outline_size']>0){                list($new_logo_img, $new_logo_w, $new_logo_h) = $this->image_outline($new_logo_img);            }            // 判斷是否需要圓角處理            if($this->_config['logo_radius']>0){                $new_logo_img = $this->image_fillet($new_logo_img);            }            // 合拼logo與臨時二維碼            $pos_x = ($qrcode_w-$new_logo_w)/2;            $pos_y = ($qrcode_h-$new_logo_h)/2;            imagealphablending($tmp_qrcode_img, true);            // 合拼圖片並保留各自透明度            $dest_img = $this->imagecopymerge_alpha($tmp_qrcode_img, $new_logo_img, $pos_x, $pos_y, 0, 0, $new_logo_w, $new_logo_h, $this->_config['logo_opacity']);            // 產生圖片            switch($dest_ext){                case 1: imagegif($dest_img, $this->_config['dest_file'], $this->_config['quality']); break;                case 2: imagejpeg($dest_img, $this->_config['dest_file'], $this->_config['quality']); break;                case 3: imagepng($dest_img, $this->_config['dest_file'], (int)(($this->_config['quality']-1)/10)); break;            }         // 不需要加入logo        }else{            $dest_img = imagecreatefrompng($tmp_qrcode_file);            // 產生圖片            switch($dest_ext){                case 1: imagegif($dest_img, $this->_config['dest_file'], $this->_config['quality']); break;                case 2: imagejpeg($dest_img, $this->_config['dest_file'], $this->_config['quality']); break;                case 3: imagepng($dest_img, $this->_config['dest_file'], (int)(($this->_config['quality']-1)/10)); break;            }        }    }    /**     * 對圖片對象進行描邊     * @param  Obj   $img 圖片對象     * @return Array     */    private function image_outline($img){        // 擷取圖片寬高        $img_w = imagesx($img);        $img_h = imagesy($img);        // 計算描邊尺寸,沒有設定則按比例自動計算        $bg_w = isset($this->_config['logo_outline_size'])? intval($img_w + $this->_config['logo_outline_size']) : $img_w + (int)($img_w/5);        $bg_h = isset($this->_config['logo_outline_size'])? intval($img_h + $this->_config['logo_outline_size']) : $img_h + (int)($img_h/5);        // 建立底圖對象        $bg_img = imagecreatetruecolor($bg_w, $bg_h);        // 設定底圖顏色        $rgb = $this->hex2rgb($this->_config['logo_outline_color']);        $bgcolor = imagecolorallocate($bg_img, $rgb['r'], $rgb['g'], $rgb['b']);        // 填充底圖顏色        imagefill($bg_img, 0, 0, $bgcolor);        // 合拼圖片與底圖,實現描邊效果        imagecopy($bg_img, $img, (int)(($bg_w-$img_w)/2), (int)(($bg_h-$img_h)/2), 0, 0, $img_w, $img_h);        $img = $bg_img;        return array($img, $bg_w, $bg_h);    }    /**     * 對圖片對象進行圓角處理     * @param  Obj $img 圖片對象     * @return Obj     */    private function image_fillet($img){        // 擷取圖片寬高        $img_w = imagesx($img);        $img_h = imagesy($img);        // 建立圓角圖片對象        $new_img = imagecreatetruecolor($img_w, $img_h);        // 儲存透明通道        imagesavealpha($new_img, true);        // 填充圓角圖片        $bg = imagecolorallocatealpha($new_img, 255, 255, 255, 127);        imagefill($new_img, 0, 0, $bg);        // 圓角半徑        $r = $this->_config['logo_radius'];        // 執行圓角處理        for($x=0; $x<$img_w; $x++){            for($y=0; $y<$img_h; $y++){                $rgb = imagecolorat($img, $x, $y);                // 不在圖片四角範圍,直接畫圖                if(($x>=$r && $x<=($img_w-$r)) || ($y>=$r && $y<=($img_h-$r))){                    imagesetpixel($new_img, $x, $y, $rgb);                // 在圖片四角範圍,選擇畫圖                }else{                    // 上左                    $ox = $r; // 圓心x座標                    $oy = $r; // 圓心y座標                    if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){                        imagesetpixel($new_img, $x, $y, $rgb);                    }                    // 上右                    $ox = $img_w-$r; // 圓心x座標                    $oy = $r;        // 圓心y座標                    if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){                        imagesetpixel($new_img, $x, $y, $rgb);                    }                    // 下左                    $ox = $r;        // 圓心x座標                    $oy = $img_h-$r; // 圓心y座標                    if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){                        imagesetpixel($new_img, $x, $y, $rgb);                    }                    // 下右                    $ox = $img_w-$r; // 圓心x座標                    $oy = $img_h-$r; // 圓心y座標                    if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){                        imagesetpixel($new_img, $x, $y, $rgb);                    }                }            }        }        return $new_img;    }    // 合拼圖片並保留各自透明度    private function imagecopymerge_alpha($dest_img, $src_img, $pos_x, $pos_y, $src_x, $src_y, $src_w, $src_h, $opacity){        $w = imagesx($src_img);        $h = imagesy($src_img);        $tmp_img = imagecreatetruecolor($src_w, $src_h);        imagecopy($tmp_img, $dest_img, 0, 0, $pos_x, $pos_y, $src_w, $src_h);        imagecopy($tmp_img, $src_img, 0, 0, $src_x, $src_y, $src_w, $src_h);        imagecopymerge($dest_img, $tmp_img, $pos_x, $pos_y, $src_x, $src_y, $src_w, $src_h, $opacity);        return $dest_img;    }    /**     * 建立目錄     * @param  String  $path     * @return Boolean     */    private function create_dirs($path){        if(!is_dir($path)){            return mkdir($path, 0777, true);        }        return true;    }    /** hex顏色轉rgb顏色     *  @param  String $color hex顏色     *  @return Array     */    private function hex2rgb($hexcolor){        $color = str_replace('#', '', $hexcolor);        if (strlen($color) > 3) {            $rgb = array(                'r' => hexdec(substr($color, 0, 2)),                'g' => hexdec(substr($color, 2, 2)),                'b' => hexdec(substr($color, 4, 2))            );        } else {            $r = substr($color, 0, 1) . substr($color, 0, 1);            $g = substr($color, 1, 1) . substr($color, 1, 1);            $b = substr($color, 2, 1) . substr($color, 2, 1);            $rgb = array(                'r' => hexdec($r),                'g' => hexdec($g),                'b' => hexdec($b)            );        }        return $rgb;    }    /** 擷取圖片類型      * @param  String $file 圖片路徑      * @return int      */      private function get_file_ext($file){        $filename = basename($file);        list($name, $ext)= explode('.', $filename);        $ext_type = 0;        switch(strtolower($ext)){            case 'jpg':            case 'jpeg':                $ext_type = 2;                break;            case 'gif':                $ext_type = 1;                break;            case 'png':                $ext_type = 3;                break;        }        return $ext_type;    }} // class end?>

demo.php

<?phprequire 'PHPQRCode.class.php';$config = array(        'ecc' => 'H',    // L-smallest, M, Q, H-best        'size' => 12,    // 1-50        'dest_file' => 'qrcode.png',        'quality' => 90,        'logo' => 'logo.jpg',        'logo_size' => 100,        'logo_outline_size' => 20,        'logo_outline_color' => '#FFFF00',        'logo_radius' => 15,        'logo_opacity' => 100,);// 二維碼內容$data = 'http://weibo.com/fdipzone';// 建立二維碼類$oPHPQRCode = new PHPQRCode();// 設定配置$oPHPQRCode->set_config($config);// 建立二維碼$qrcode = $oPHPQRCode->generate($data);// 顯示二維碼echo '<img src="'.$qrcode.'?t='.time().'">';?>

相關推薦:

(進階篇)php產生帶logo二維碼方法小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.