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

我们都知道一个二维码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()
函数,最终完整代码如下
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的图像了
311
聚合全网优质资源,尽在小彬子的资源站!全站优品,只为传递价值
本站资源均源自互联网,由小彬子收集整理,如果侵犯了您的合法权益,请联系本站2818929499@qq.com我们会及时删除。部分作品由小彬子进行二次创作修改,转载请注明!
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
未经允许不得转载:小彬子的自媒体 » 使用PHP类库phpqrcode自定义二维码大小
- THE END -

微信小程序:小彬子的自媒体
关注我们,搜索目的地古镇美食,有趣有料!
关注小程序不迷路
评论抢沙发