phpqrcode自定义二维码大小

2024-08-11 08:50:09 举报文章

image.png

我们都知道一个二维码PHP API接口使用phpqrcode.php文件编写很容易就可以写出一个API接口,网页所出现的二维码图像一般都是随内容的增加而增大,

//by www.qzphp.cn
static QRcode::png	(
 	$text,
 	$outfile = false,
 	$level = QR_ECLEVEL_L,
 	$size = 3,
 	$margin = 4,
 	$saveandprint = false 
)

关于上面的QRcode::png()这个函数的参数这里不做多解释,网络搜一下解释的比我详细,我们这里只说一下$size这个参数,这个参数指的是像素大小,意思就是二维码中每个方块的大小,所以$text内容越多图片越大,我想要一个固定的png图像尺寸大小,怎么办?

不多扯了,直接修改phpqrcode.php,首先修改文件最下面的encodePNG()方法,把

//by www.qzphp.cn
QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);

改为

//by www.qzphp.cn
QRimage::png($tab, $outfile, $this->size, $this->margin,$saveandprint);

完整代码:

//by www.qzphp.cn
public function encodePNG($intext, $outfile = false,$saveandprint=false) 
{
    try {
    
        ob_start();
        $tab = $this->encode($intext);
        $err = ob_get_contents();
        ob_end_clean();
        
        if ($err != '')
            QRtools::log($outfile, $err);
        
        $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
        
        //QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
        QRimage::png($tab, $outfile,  $this->size , $this->margin,$saveandprint);//修改内容
    } catch (Exception $e) {
    
        QRtools::log($outfile, $e->getMessage());
    
    }
}

然后修改private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)方法,自己搜索找到位置,将

//by www.qzphp.cn
$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);

改为

//by www.qzphp.cn
$target_image =ImageCreate( $pixelPerPoint, $pixelPerPoint);
ImageCopyResized($target_image, $base_image, 0, 0, 0, 0,  $pixelPerPoint, $pixelPerPoint, $imgW, $imgH);

好了这样指定$size=200,就会输出200X200的图像了,但是我发现生成的图片白色边框参数$margin也不是指的具体像素,好吧,继续修改。

还是private static function image()函数,最终完整代码如下

以下为隐藏内容:

//by www.qzphp.cn
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) 
{
     
    $h = count($frame);
    $w = strlen($frame[0]);
   
    $imgW = $w;// + 2*$outerFrame;  修改
    $imgH = $h;// + 2*$outerFrame;  修改
    
    $base_image =ImageCreate($imgW, $imgH);
    
    $col[0] = ImageColorAllocate($base_image,255,255,255);
    $col[1] = ImageColorAllocate($base_image,0,0,0);
    imagefill($base_image, 0, 0, $col[0]);
    
    for($y=0; $y<$h; $y++) {
        for($x=0; $x<$w; $x++) {
            if ($frame[$y][$x] == '1') {
                ImageSetPixel($base_image,$x,$y,$col[1]); 
            }
        }
    }
  
    $target_image =ImageCreate( $pixelPerPoint-$outerFrame*2, $pixelPerPoint-$outerFrame*2);//修改
  
    ImageCopyResized($target_image, $base_image, 0, 0, 0, 0,  $pixelPerPoint-$outerFrame*2, $pixelPerPoint-$outerFrame*2, $imgW, $imgH);//修改
    ImageDestroy($base_image);
    //添加----
  	$iowen_image =ImageCreate( $pixelPerPoint, $pixelPerPoint);
    ImageColorAllocate($iowen_image,255,255,255);
  
  imagecopy ( $iowen_image, $target_image, $outerFrame , $outerFrame , 0, 0, $pixelPerPoint-$outerFrame*2, $pixelPerPoint-$outerFrame*2 );
    ImageDestroy($target_image);
  
    return $iowen_image;
}

完成,现在就可以随意定义边框10px,大小200px的图像了。

如果你认为本文可读性较差,内容错误,或者文章排版错乱,请点击举报文章按钮,我们会立即处理!