[PHP] phpqrcode 生成带LOGO二维码,并打包下载

[PHP] phpqrcode 生成带LOGO二维码,并打包下载

基于 phpqrcode 生成二维码,带LOGO
只需要修改输入参数与图片地址,整体可以直接拿来用
官网下载完整的 phpqrcode 库文件,下载地址

生成二维码

 	/**
     * 导出二维码 -- 格式为所有二维码图片
     * @param $ercode_list 二维码链接数组,我的数组包含两个参数,url 和 unique_id
     * 			url 二维码链接
     * 			unique_id 主要为生成独一无二的二维码文件名称
     * @return string
     */
    public function exportErcode($ercode_list)
    {
        require_once APPPATH . "libraries/phpqrcode/phpqrcode.php";
        $img_arr = array();
        foreach ($ercode_list as $ercode) {

            $url = $ercode['url'];
            //容错级别
            $errorCorrectionLevel = 'L';
            $matrixPointSize = 10;//生成图片大小

            $img_name = 'uploads/ercode/' . $ercode['unique_id'] . '_qrcode.png';

			//生成二维码 -- 具体参数见下面说明
            QRcode::png($url, $img_name, $errorCorrectionLevel, $matrixPointSize, 2);
            $logo = 'static/images/defaultLogo.png';//准备好的logo图片
            $QR = $img_name;//已经生成的原始二维码图

			//需要logo的话,就添加以下代码
            if ($logo !== FALSE) {
                $QR = imagecreatefromstring(file_get_contents($QR));
                $logo = imagecreatefromstring(file_get_contents($logo));
                $QR_width = imagesx($QR);//二维码图片宽度
                $QR_height = imagesy($QR);//二维码图片高度
                $logo_width = imagesx($logo);//logo图片宽度
                $logo_height = imagesy($logo);//logo图片高度
                $logo_qr_width = $QR_width / 5;
                $scale = $logo_width / $logo_qr_width;
                $logo_qr_height = $logo_height / $scale;
                $from_width = ($QR_width - $logo_qr_width) / 2;
                //重新组合图片并调整大小
                imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
                    $logo_qr_height, $logo_width, $logo_height);
            }
            //输出图片
            imagepng($QR, $img_name);

            $img_arr[] = $img_name;
        }
        $zip_name = 'uploads/ercode/ercode_' . date('YmdHis') . '.zip';
        $this->makeZip($zip_name, $img_arr);
        $this->download($zip_name);

		//如果需要输出图片
		// imagepng($QR, 'helloweba.png');
        exit(0);
    }

QRcode::png() 参数说明

//完整格式
QRcode::png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)

$text – URL网址参数;

$outfile 默认为否,表示不生成文件,只将二维码图片返回,否则需要给出存放生成二维码图片的路径;

$level 默认为 L,控制二维码容错率,不同的参数表示二维码可被覆盖时还能识别的区域百分比,这个参数可传递的值分别是 L(QR_ECLEVEL_L,7%)M(QR_ECLEVEL_M,15%)Q(QR_ECLEVEL_Q,25%)H(QR_ECLEVEL_H,30%)。利用二维维码的容错率,我们可以将头像放置在生成的二维码图片任何区域;

$size – 控制生成图片的大小,默认为3;

$margin – 控制生成二维码的空白区域大小,默认为4;

$saveandprint – 保存二维码图片并显示出来,$outfile 必须传递图片路径。

 

ZIP打包

    /**
     * 生成压缩zip文件
     * @param $file_name 最终生成的文件名,包含路径
     * @param $file_list 生成file_name的文件数组
     */
    public function makeZip($file_name, $file_list)
    {
        if (file_exists($file_name)) {
            unlink($file_name);
        }
        //重新生成文件
        $zip = new ZipArchive();
        if ($zip->open($file_name, ZIPARCHIVE::CREATE) !== TRUE) {
            exit('无法打开文件,或者文件创建失败');
        }
        foreach ($file_list as $val) {
            if (file_exists($val)) {
                $zip->addFile($val);
            }
        }
        $zip->close();//关闭
        if (!file_exists($file_name)) {
            exit('无法找到文件'); //即使创建,仍有可能失败
        }
    }

 

下载

	 /**
     * 下载
     * @param $file 文件路径
     */
    public function download($file)
    {
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
    }

版权声明:本文为chritina原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。