Heim  >  Artikel  >  php教程  >  生成带图片二维码

生成带图片二维码

WBOY
WBOYOriginal
2016-09-07 12:57:581584Durchsuche

自己刚开始尝试,对大神来说可能入不了法眼,希望有用。

步骤:一、在网址http://phpqrcode.sourceforge.net/中下载phpqrcode.php文件,然后放到自己的项目中去;

        二、编写代码并引入phpqrcode.php文件,实现生成二维码。

代码:

一、phpqrcode.php文件(下载即可)

二、测试代码(erweima.app.php)

/*
 * 生成二维码
 */
class ErweimaApp extends ShoppingbaseApp{
    
    function index()
    {
        $this->display('erweima.html');
    }

    /**  
     * @param string $chl 二维码包含的信息,可以是数字、字符、二进制信息、汉字。
     不能混合数据类型,数据必须经过UTF-8 URL-encoded
     * @param int $widhtHeight 生成二维码的尺寸设置
     * @param string $EC_level 可选纠错级别,QR码支持四个等级纠错,用来恢复丢失的、读错的、模糊的、数据。
     * L-默认:可以识别已损失的7%的数据
     * M-可以识别已损失15%的数据
     * Q-可以识别已损失25%的数据
     * H-可以识别已损失30%的数据
     * @param int $margin 生成的二维码离图片边框的距离
     */
    function credit_qrcode()
    {
        include '/includes/libraries/phpqrcode.php';
        $value = isset($_POST['url']) ? $_POST['url'] : 'http://www.baidu.com';
        //上传图片
        if (isset($_FILES['image']) && $_FILES['image']['error'] == 0 ){
            $image = $this->_upload_file('image', 'erweima/', date('YmdHis') . mt_rand(1000, 9999), 'index.php?app=credit&act=credit_qrcode');
            if ($image){
                $logo = $image;
            }
        }
        else
        {
            $logo = SITE_URL . '/themes/mall/default/styles/default/images/001.jpg';//准备好的logo图片
        }
        $errorCorrectionLevel = 'H';//容错级别
    $matrixPointSize = 8;//生成图片大小
    //生成二维码图片
    QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
    $QR = 'qrcode.png';//已经生成的原始二维码图
        
        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, 'helloweba.png');
    echo '生成带图片二维码';
    }
    
    /**
     * 上传文件
     * @return mix false表示上传失败,空串表示没有上传,string表示上传文件地址
     * $file_name 为上传文件name
     * $path_name 为上传路径
     * $save_name 为保存文件名
     * $ret_url 为回调URL
     **/
    function _upload_file($file_name, $path_name, $save_name, $ret_url = 'index.php')
    {
        $file = $_FILES[$file_name];
        $message = array(
            '1' => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。',
            '2' => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。',
            '3' => '文件只有部分被上传。'
        );
        switch ($file['error'])
        {
            case UPLOAD_ERR_INI_SIZE:
            case UPLOAD_ERR_FORM_SIZE:
            case UPLOAD_ERR_PARTIAL:
            if ($ret_url)
            {
                $this->show_warning($message[$file['error']], 'go_back');
                return false;
            }
            else
            {
                return array('done' => FALSE, 'msg' => $message[$file['error']]);
            }
            break;
        }
        if ($file['error'] != UPLOAD_ERR_OK)
        {
            return '';
        }
        import('uploader.lib');
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE);
        $uploader->addFile($file);
        if ($uploader->file_info() === false)
        {
            if ($ret_url)
            {
                $this->show_warning($uploader->get_error(), 'go_back', $ret_url);
                return false;
            }
            else
            {
                return array('done' => FALSE, 'msg' => $uploader->get_error());
            }
        }
        $uploader->root_dir(ROOT_PATH);
        return $uploader->save('data/files/mall/'.$path_name, $save_name);
    }
}

三、模板文件(erweima.html)

 


       

               请输入网址:

               图片上传:

              
       

 
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:剖析ajaxNächster Artikel:接口调用,字符串截取处理。