我们都知道一个二维码PHP API接口使用phpqrcode.php文件编写很容易就可以写出一个API接口,网页所出现的二维码图像一般都是随内容的增加而增大,
static QRcode::png ( $text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint = false )关于上面的QRcode::png()这个函数的参数这里不做多解释,网络搜一下解释的比我详细,我们这里只说一下$size这个参数,这个参数指的是像素大小,意思就是二维码中每个方块的大小,所以$text内容越多图片越大,我想要一个固定的png图像尺寸大小,怎么办?
不多扯了,直接修改phpqrcode.php,首先修改文件最下面的encodePNG()方法,把
QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);改为
QRimage::png($tab, $outfile, $this->size, $this->margin,$saveandprint);完整代码:
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)方法,自己搜索找到位置,将
$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);改为
$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()函数,最终完整代码如下
此处为隐藏的内容!
发表评论并刷新,才能查看
发表评论
完成,现在就可以随意定义边框10px,大小200px的图像了,可以访问我搭的在线生成二维码
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。