Home >类库下载 >PHP类库 >Generate QR code with picture

Generate QR code with picture

高洛峰
高洛峰Original
2016-10-10 11:03:441398browse

I just started trying it myself, and it may not be easy for the masters to understand. I hope it will be useful.网 Step: 1. Download the phpqrcode.php file in http://phpqrcode.sourceForge.net/, and then put it in your own project;

2, write code and introduce phpqrcode.php files to achieve generating two two seconds to generate two two files to generate two two files to achieve generating two seconds to generate two two two to generate two two files to generate two two files to achieve generating two seconds. QR code.

Code:

1. phpqrcode.php file (download)

2. Test code (erweima.app.php)

<?php
/*
 * 生成二维码
 */ 
class ErweimaApp extends ShoppingbaseApp{
    
    function index()
    {
        $this->display(&#39;erweima.html&#39;);
    }

    /**  
     * @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 &#39;/includes/libraries/phpqrcode.php&#39;; 
        $value = isset($_POST[&#39;url&#39;]) ? $_POST[&#39;url&#39;] : &#39;http://www.baidu.com&#39;;
        //上传图片
        if (isset($_FILES[&#39;image&#39;]) && $_FILES[&#39;image&#39;][&#39;error&#39;] == 0 ){
            $image = $this->_upload_file(&#39;image&#39;, &#39;erweima/&#39;, date(&#39;YmdHis&#39;) . mt_rand(1000, 9999), &#39;index.php?app=credit&act=credit_qrcode&#39;);
            if ($image){
                $logo = $image;
            }
        }
        else
        { 
            $logo = SITE_URL . &#39;/themes/mall/default/styles/default/images/001.jpg&#39;;//准备好的logo图片 
        }
        $errorCorrectionLevel = &#39;H&#39;;//容错级别 
    $matrixPointSize = 8;//生成图片大小 
    //生成二维码图片 
    QRcode::png($value, &#39;qrcode.png&#39;, $errorCorrectionLevel, $matrixPointSize, 2); 
    $QR = &#39;qrcode.png&#39;;//已经生成的原始二维码图 
        
        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, &#39;helloweba.png&#39;); 
    echo &#39;<img  src="helloweba.png" alt="Generate QR code with picture" >&#39;; 
    }
    
    /**
     * 上传文件
     * @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 = &#39;index.php&#39;)
    {
        $file = $_FILES[$file_name];
        $message = array(
            &#39;1&#39; => &#39;上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。&#39;,
            &#39;2&#39; => &#39;上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。&#39;,
            &#39;3&#39; => &#39;文件只有部分被上传。&#39;
        );
        switch ($file[&#39;error&#39;])
        {
            case UPLOAD_ERR_INI_SIZE:
            case UPLOAD_ERR_FORM_SIZE:
            case UPLOAD_ERR_PARTIAL:
            if ($ret_url)
            {
                $this->show_warning($message[$file[&#39;error&#39;]], &#39;go_back&#39;);
                return false;
            }
            else
            {
                return array(&#39;done&#39; => FALSE, &#39;msg&#39; => $message[$file[&#39;error&#39;]]);
            }
            break;
        }
        if ($file[&#39;error&#39;] != UPLOAD_ERR_OK)
        {
            return &#39;&#39;;
        }
        import(&#39;uploader.lib&#39;);
        $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(), &#39;go_back&#39;, $ret_url);
                return false;
            }
            else
            {
                return array(&#39;done&#39; => FALSE, &#39;msg&#39; => $uploader->get_error());
            }
        }
        $uploader->root_dir(ROOT_PATH);
        return $uploader->save(&#39;data/files/mall/&#39;.$path_name, $save_name);
    }
}

3. Template file (erweima.html)

 <div style="height:100px;border:1px solid gray;text-align:center;padding-top:20px;">
        <form action="index.php?app=erweima&act=credit_qrcode" method="post" enctype="multipart/form-data">
               请输入网址:<input type="text" name="url" ><br />
               图片上传:<input type="file" name="image"><br />
               <input type="submit" name="sbt" value="提交">
        </form>
 </div>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more