[php-01]-生成并保存二维码

需要用到的资源文件:

https://files.cnblogs.com/files/Eyes-Chan/phpqrcode-2010100721_1.1.4.zip

实际上只需要用到以上资源文件中的 phpqrcode.php 。(如果你使用的是CI框架,只需要将phpqrcode.php文件放在 controller 层中)

使用方式:

<?php

//
将$str带表的字符串生成二维码并输出到网页 $str = 'http://www.google.com'; public function make_qrcode($str){ include 'phpqrcode.php'; QRcode::png($str); }

?>

如果想要生成完二维码并直接保存:

<?php

//将$str所指代的字符串生成二维码并保存在$filename所指代的路径下
$str = 'http://www.google.com';
$filename = '/*the url you want to save the qrcode image*/';
public function make_save_qrcode($str,$filename)
{
    include 'phpqrcode.php';
    QRcode::png($str,$filename,$pixelPerPoint = 4, $outerFrame = 4,$saveandprint=false);
}

?>

如果想生成带logo的二维码并直接保存:

<?php

$value = 'www.google.com'; //二维码内容 
$filename = '/*the url you want to save your qrcode image*/';
public function make_qrcode($value)
{
    include 'phpqrcode.php';
    $errorCorrectionLevel = 'L';//容错级别 
    $matrixPointSize = 6;//生成图片大小 
    //生成二维码图片 
    QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); 
    $logo = 'logo.png';//准备好的logo图片 
    $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, $filename);     
}

?>     

以上是常用的三个方法,那么如果需要细节的调整,或者满足其他需求,可以详细研究一下 phpqrcode.php这个类库文件。

posted @ 2015-09-18 11:42  Eyes-Chan  阅读(582)  评论(0)    收藏  举报