Heim > 类库下载 > PHP类库 > Hauptteil

生成带图片二维码

高洛峰
Freigeben: 2016-10-10 11:03:44
Original
1326 Leute haben es durchsucht

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

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

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

代码:

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

二、测试代码(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="生成带图片二维码" >&#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);
    }
}
Nach dem Login kopieren

三、模板文件(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>
Nach dem Login kopieren


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!