phpqrcode固定二维码大小

我们都知道一个二维码PHP API接口使用phpqrcode.php文件编写很容易就可以写出一个API接口,网页所出现的二维码图像一般都是随内容的增加而增加,我想要一个固定的png图像尺寸大小,怎么办?

一般网上都会给出答案说确定img的尺寸大小,虽然可以确定大小但是却变成了一个标签页面,并不是我们所需要的png页面

一般我们使用的是一下代码产生,注意请先下载phpqrcode.php

 

<?php
//载入qrcode类库
include "phpqrcode.php";
//取得GET参数
$text       = isset($_GET["text"]) ? $_GET["text"] : ''; //二维码内容
$errorLevel = isset($_GET["e"]) ? $_GET["e"] : 'H'; //容错级别 默认L
$PointSize  = isset($_GET["p"]) ? $_GET["p"] : '10.882353'; //二维码尺寸 默认12
$margin     = isset($_GET["m"]) ? $_GET["m"] : '3'; //二维码白边框尺寸 默认2
function getqrcode($value,$errorCorrectionLevel,$matrixPointSize,$margin) {
    QRcode::png($value,false, $errorCorrectionLevel, $matrixPointSize, $margin,false);
}
getqrcode($text, $errorLevel, $PointSize, $margin);
?>

关于上面的QRcode::png()这个函数的参数这里不做多解释,网络搜一下解释的比我详细,我们这里直说一下那个$matrixPointSize这个参数,因为有些人说修改这个可以改变图片的大小,这个可以改变,但是一旦你内容变了长短,你的图片照样并不会是你想要的那个尺寸,因为这个只是改变像素点的大小。

我们可以在别人的基础上增加这样的功能

当我们打开phpqrcode.php时候我们会发现,里面有很多类和类方法,我们可以在它图片的输出地方增加这样的函数(图像的放大与缩小)。

这是图像的放大与缩小函数,$im是输入的16进制图像,$maxwidth尺寸宽$maxheight尺寸长。

将下列类容增加在phpqrcode.php里面

 function resizeImage($im,$maxwidth,$maxheight)  
{  
    $pic_width = imagesx($im);  
    $pic_height = imagesy($im);  
  
    if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))  
    {  
        if($maxwidth && $pic_width>$maxwidth)  
        {  
            $widthratio = $maxwidth/$pic_width;  
            $resizewidth_tag = true;  
        }  
  
        if($maxheight && $pic_height>$maxheight)  
        {  
            $heightratio = $maxheight/$pic_height;  
            $resizeheight_tag = true;  
        }  
  
        if($resizewidth_tag && $resizeheight_tag)  
        {  
            if($widthratio<$heightratio)  
                $ratio = $widthratio;  
            else  
                $ratio = $heightratio;  
        }  
  
        if($resizewidth_tag && !$resizeheight_tag)  
            $ratio = $widthratio;  
        if($resizeheight_tag && !$resizewidth_tag)  
            $ratio = $heightratio;  
  
        $newwidth = $pic_width * $ratio;  
        $newheight = $pic_height * $ratio;  
  
        if(function_exists("imagecopyresampled"))  
        {  
            $newim = imagecreatetruecolor($newwidth,$newheight);  
           imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);  
        }  
        else  
        {  
            $newim = imagecreate($newwidth,$newheight);  
           imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);  
        }  
  
        //$name = $name.$filetype; 
        Header("Content-type: image/png"); 
        imagepng($newim);  
        //return $newim;
       //imagedestroy($newim);  
    }  
    else  
    {  
       // $name = $name.$filetype;  
        //imagepng($im,$name); 
        //Header("Content-type: image/png");
        Header("Content-type: image/png"); 
        imagepng($im); 
    }             
}

下面是phpqrcode.php里面改变的内容,将class QRimge的png函数替换下列即可。

 

class QRimage {
    
        //----------------------------------------------------------------------
        public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE) 
        {
            $image = self::image($frame, $pixelPerPoint, $outerFrame);
            
            if ($filename === false) {
                $maxwidth = 300;
                $maxheight = 300;
                resizeImage($image,$maxwidth,$maxheight);

            } else {
                if($saveandprint===TRUE){
                    ImagePng($image, $filename);
                    header("Content-type: image/png");
                    ImagePng($image);
                }else{
                    ImagePng($image, $filename);
                }
            }
            ImageDestroy($image);
        }

}

 

最后你无论怎么改变信息内容都会变成一样的大小。

增加信息量

原文:https://blog.csdn.net/charmean/article/details/78623622