phpqrcode怎么生成固定宽度高度的二维码?
浏览量:1587
PHP使用phpqrcode.php文件很容易生成一个二维码,但是宽度是没法设定的,只能在<img>里设置宽度、高度。
phpqrcode第四个参数$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);
然后修改image()方法,自己搜索找到位置,将
$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,就会输出200X200px的图像了.
第五个参数,白色边框也改成固定尺寸的,怎么办?
但是我发现生成的图片白色边框参数$margin也不是指的具体像素,好吧,继续修改。
还是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的图像了


感谢支持与鼓励~