·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> 个性化自己的二维码

个性化自己的二维码

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
个性化自己的二维码

一、什么是二维码

二、我们如何制作二维码

三、如何制作自己的个性二维码

1、第一步。下载php类库phpqrcode,(附下载地址:http://sourceforge.net/PRojects/phpqrcode/)

网上给出的使用案列是:

<?php/*$errorCorrectionLevel 纠错级别:L、M、Q、H  $matrixPointSize表示图片每个黑点的像素点的大小:1到10  */include '/phpqrcode/phpqrcode.php';//引入PHP QR库文件$value="个性化自己的二维码";// 二维码数据 $errorCorrectionLevel = "l";// 纠错级别:L、M、Q、H $matrixPointSize = "10";// 点的大小:1到10  QRcode::png($value, false, $errorCorrectionLevel);exit;?>

2、看懂上面的代码

上面那段代码发生了什么奇妙的旅程呢?

让我么打开phpqrcode.php看一看,代码太长了,就不贴了,各位自己下载去吧。

结合上面的代码和phpqrcode.php,看一看:

<?php/*$errorCorrectionLevel 纠错级别:L、M、Q、H  $matrixPointSize表示图片每个黑点的像素点的大小:1到10  */include 'phpqrcode/phpqrcode.php';//引入PHP QR库文件$intext="个性化自己的二维码";// 二维码数据 $errorCorrectionLevel = "l";// 纠错级别:L、M、Q、H $matrixPointSize = "2";// 点的大小:1到10  $margin = 1;$size = 10;$outfile = false;$saveandprint=false;$enc = QRencode::factory($errorCorrectionLevel, $size, $margin);//$enc->encodePNG($value, false, $saveandprint=false);try {ob_start();$tab = $enc->encode($intext);print_r($tab);$err = ob_get_contents();ob_end_clean();if ($err != '')QRtools::log($outfile, $err);/*标记*/$maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$enc->margin));QRimage::png($tab, $outfile, min(max(1, $enc->size), $maxSize), $enc->margin,$saveandprint);} catch (Exception $e) {QRtools::log($outfile, $e->getMessage());}exit;?>

我们可以发现,php类库phpqrcode首先通过一种算法将我们需要的文字转化为数组$tab ,然后通过图像操作画了一张图片,也就是我们的二维码。

如果打印数组$tab,就会发现他就是这样的:

Array(    [0] => 1111111010101001001111111    [1] => 1000001001111001001000001    [2] => 1011101011100001101011101    [3] => 1011101011101110101011101    [4] => 1011101010011010001011101    [5] => 1000001000110111001000001    [6] => 1111111010101010101111111    [7] => 0000000000101111100000000    [8] => 1111001010110000110011101    [9] => 1010100010101110100111100    [10] => 1011011111111111111000111    [11] => 0010010011100000100001000    [12] => 0101111111101001100101100    [13] => 0100010111010111010001001    [14] => 0110101010110111010100001    [15] => 1001110110101100110111101    [16] => 0000101100110100111110000    [17] => 0000000011110101100010101    [18] => 1111111001010110101011010    [19] => 1000001001101100100010101    [20] => 1011101001100001111110001    [21] => 1011101010010110000000011    [22] => 1011101011000111011001110    [23] => 1000001011001010001001000    [24] => 1111111011000100100101111)

好吧,你懂了吗…………

现在就简单了,根据数组$tab,画画就可以了:

QRimage::png($tab, $outfile, min(max(1, $enc->size), $maxSize), $enc->margin,$saveandprint);

3、如何画画

如果我们人人研究源码,会发现最关键的是这样一个方法:

private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4);

下面贴出我注释过的源码(原类库是没有注释的)

<?phpfunction image($frame, $pixelPerPoint = 4, $outerFrame = 4){//$frame就是数组$tab,$pixelPerPoint,$outerFrame现在看不出来是什么,待会解释$h = count($frame);$w = strlen($frame[0]);//计算应该画多长多宽的画,$h表示高,$w表示宽$imgW = $w + 2*$outerFrame;$imgH = $h + 2*$outerFrame;//它把画布变大了一点!说明$outerFrame是周围留白大小$base_image =ImageCreate($imgW, $imgH);//imagecreate — 新建一个基于调色板的图像,换句话说,我们现在可以基于$base_image画画了$col[0] = ImageColorAllocate($base_image,255,255,255);$col[1] = ImageColorAllocate($base_image,0,0,0);//imagecolorallocate — 为一幅图像分配颜色//第一个参数是建立的,后面三个分别是R,G,B(大小都是从0到255),你可以理解为颜料……,三个颜料不同比例混合产生了不同的颜色,所以$col[0]就是白色的画笔啦,$col[1]是黑色的画笔(为什么三个255是白色,三个0是黑色,你可以想象一下中学物理里面白光可以分解的实验……)imagefill($base_image, 0, 0, $col[0]);//imagefill — 区域填充 ,整个画布上面都是白色的啊for($y=0; $y<$h; $y++) {for($x=0; $x<$w; $x++) {if ($frame[$y][$x] == '1') {ImageSetPixel ($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]); }}}//通过两个循环,将$tab数组中的1填充为黑色,剩下的0为白//$outerFrame表示留白$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);//ImageCreate这个函数刚刚介绍过了,干嘛又调用…………而且大小是原来的$pixelPerPoint倍!//好吧,$pixelPerPoint是放大倍数,这里开始将刚刚生成的画按需放大(现在只是生成放大的画布)ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);//imagecopyresized — 拷贝部分图像并调整大小 //将刚刚的画放大$pixelPerPoint倍之后复制到新建的画布里面ImageDestroy($base_image);//imagedestroy — 销毁一图像 return $target_image;//返回生成的最后图像!}

4、自己的才是踏实的。

So…………

(1)可以将“黑点”变成彩色的点?变成爱心?,变成你女朋友的照片?变成文字?

(2)可以再图像中间部分加点东西,一个“爱”字,还是什么能够表达力心意的东西?

5、编写自己的方法

private static function myImage($frame, $pixelPerPoint = 4, $outerFrame = 4, $point, $centerPoint ){/* * array $point 表示所填充的点的样式 * array $centerPoint 表示图片中间部分的样式 * $point = array('kind'=>'',//col,img,Word'info'=>'' //rgb,filename) * $centerPoint = array('kind'=>'',//col,img,word'info'=>'') * 没有编写完,但是思路是一样的 */if($point['kind'] == 'col'){$R1 = $point['info']['0']['R'];$G1 = $point['info']['0']['G'];$B1 = $point['info']['0']['B'];$R2 = $point['info']['1']['R'];$G2 = $point['info']['1']['G'];$B2 = $point['info']['1']['B'];$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,$R1,$G1,$B1);$col[1] = ImageColorAllocate($base_image,$R2,$G2,$B2);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+$outerFrame,$y+$outerFrame,$col[1]); }}}//////////////////////x$target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);ImageDestroy($base_image);return $target_image;}elseif($point['kind'] == 'img'){function getSquare($image, $multi){$imgW = imagesx($image);$imgH = imagesy($image);$imgMin = min($imgH,$imgW);$target_image =imagecreatetruecolor($imgMin,$imgMin);imagecopyresampled($target_image, $image, 0, 0, 0, 0, $imgMin , $imgMin, $imgW, $imgH);//ImageCopyResized($target_image, $image, 0, 0, 0, 0, $imgW * $multi, $imgH * $multi, $imgW, $imgH);$multi_image =imagecreatetruecolor($imgMin*$multi,$imgMin*$multi);imagecopyresampled($multi_image, $target_image, 0, 0, 0, 0, $imgMin*$multi,$imgMin*$multi, $imgMin, $imgMin);//ImageCopyResized($target_image, $image, 0, 0, 0, 0, $imgW * $multi, $imgH * $multi, $imgW, $imgH);ImageDestroy($image);return $multi_image;}function getSameSize($image,$pixelPerPoint){$imgW = imagesx($image);$imgH = imagesy($image);$target_image =imagecreatetruecolor($pixelPerPoint,$pixelPerPoint);ImageCopyResized($target_image, $image, 0, 0, 0, 0, $pixelPerPoint , $pixelPerPoint, $imgW, $imgH);//ImageCopyResized($target_image, $