Home  >  Article  >  php教程  >  个性化自己的二维码,个性化自己二维码

个性化自己的二维码,个性化自己二维码

WBOY
WBOYOriginal
2016-06-13 09:27:321016browse

个性化自己的二维码,个性化自己二维码

一、什么是二维码

 

二、我们如何制作二维码

 

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

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);

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

<?php
function 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 &mdash; 新建一个基于调色板的图像,换句话说,我们现在可以基于$base_image画画了
	$col[0] = ImageColorAllocate($base_image,255,255,255);
	$col[1] = ImageColorAllocate($base_image,0,0,0);
	//imagecolorallocate &mdash; 为一幅图像分配颜色
	//第一个参数是建立的,后面三个分别是R,G,B(大小都是从0到255),你可以理解为颜料&hellip;&hellip;,三个颜料不同比例混合产生了不同的颜色,所以$col[0]就是白色的画笔啦,$col[1]是黑色的画笔(为什么三个255是白色,三个0是黑色,你可以想象一下中学物理里面白光可以分解的实验&hellip;&hellip;)
	imagefill($base_image, 0, 0, $col[0]);
	//imagefill &mdash; 区域填充 ,整个画布上面都是白色的啊
	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这个函数刚刚介绍过了,干嘛又调用&hellip;&hellip;&hellip;&hellip;而且大小是原来的$pixelPerPoint倍!
	//好吧,$pixelPerPoint是放大倍数,这里开始将刚刚生成的画按需放大(现在只是生成放大的画布)
	ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
	//imagecopyresized &mdash; 拷贝部分图像并调整大小 
	//将刚刚的画放大$pixelPerPoint倍之后复制到新建的画布里面
	ImageDestroy($base_image);
	//imagedestroy &mdash; 销毁一图像 
	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, $image, 0, 0, 0, 0, $imgW * $multi, $imgH * $multi, $imgW, $imgH);
			
			ImageDestroy($image);
			return $target_image;
		}
		
		$h = count($frame);
		$w = strlen($frame[0]);
		
		$imgW = $w + 2*$outerFrame;
		$imgH = $h + 2*$outerFrame;
		
		$base_image =ImageCreate($imgW*$pixelPerPoint, $imgH*$pixelPerPoint);
		

		imagefill($base_image, 0, 0, ImageColorAllocate($base_image,255,255,255));
		
		$pointimg = imagecreatefromjpeg ($point['info']);
		$newimg = getSquare($pointimg, 1);
		$newimgpoint = getSameSize($newimg,$pixelPerPoint);
		
		
		for($y=0; $y<$h; $y++) {
			for($x=0; $x<$w; $x++) {
				if ($frame[$y][$x] == '1') {
					imagecopyresampled($base_image, $newimgpoint, $y*$pixelPerPoint, $x*$pixelPerPoint, 0, 0, $pixelPerPoint, $pixelPerPoint, $pixelPerPoint, $pixelPerPoint);
				}
			}
		}
		
		return $base_image;		
	}elseif($point['kind'] == 'word'){
	
	}else{
		$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+$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;	
	}
   


}

个性二维码怎做?

  目前,不论企业还是个人网站,甚至连媒介平台都贴出了自己的二维码,一来可以迎合市场需求,二来可以获取众人眼球。正因二维码需求的不断扩张,市面上二维码生成器种类也越来越多。但是,多数二维码生成器生成的二维码为黑白色,外形都比较单一,没什么特色。若想追求个性,生成彩色二维码,可以看看芝麻网是怎么做到的。

  我们在日常生活中见到最多的二维码生成器生成的二维码无非是右上、左上、左下各有一个规则正方形的矩阵式二维码,具有信息获取(文本、名片、地图、WIFI密码、网址、短信、视频)等功能,芝麻二维码生成器则不只局限于此。芝麻二维码生成器不仅可以通过输入文本、名片、网址、WIFI、地图、图片、MP3、芝麻号等信息生成二维码,而且可以依照用户喜好改变形状颜色甚至个性模板生成具有个性图案和色彩的二维码。
  芝麻网生成二维码界面
  如何在芝麻网生成彩色二维码
  1、注册成为芝麻用户
  2、选择文本、名片、网址、WIFI、地图、图片、MP3、芝麻号中的任一类型
  3、填写待生成的内容生成二维码,可以选择普通二维码以及个性化模版。如若选择普通二维码可根据个人喜好选择喜欢的颜色,添加LOGO或是调整形状生成二维码;如若选择个性化模版则可选择喜欢的模版生成个性好玩的二维码,右侧可实时显示生成的二维码预览,最后可将生成的二维码下载到本地!
  普通二维码
  个性化模版
  生成彩色二维码很简单,赶紧试试吧!钠)
 

怎生成个性好玩的二维码?

现在网上生成器软件众多,但大多没什么新意,最近尝试了一下用芝麻网新出炉的二维码生成器,果然木有令我失望啊,该生成器可以随意的调整颜色和渐变方式,还能调整形状,加入logo,尤其是还可以选择个性化模版让二维码变得可爱灵动起来。
 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn