Home > Backend Development > PHP Tutorial > Personalize your own QR code, personalize your own QR code_PHP tutorial

Personalize your own QR code, personalize your own QR code_PHP tutorial

WBOY
Release: 2016-07-13 10:21:12
Original
2267 people have browsed it

Personalize your own QR code, personalize your own QR code

1. What is a QR code

2. How do we make QR code

3. How to make your own personalized QR code

1. The first step. Download the Php class library phpqrcode, (with download address: http://sourceforge.net/projects/phpqrcode/)

The use cases given online are:

<?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;
?>
Copy after login

2. Understand the above code

What wonderful journey happened in the above code?

Let us open phpqrcode.php and take a look. The code is too long, so I won’t post it. You can download it yourself.

Combine the above code with phpqrcode.php and take a look:

<?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;
?>
Copy after login

We can find that the php class library phpqrcode first converts the text we need into the array $tab through an algorithm, and then draws a picture through image manipulation, which is our QR code.

If you print the array $tab, you will find that it looks like this:

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
)
Copy after login

Okay, do you understand...

Now it’s simple, just draw based on the array $tab:

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

3. How to draw

If we all study the source code, we will find that the most critical method is this:

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

The source code I commented is posted below (the original class library has no comments)

<?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;
	//返回生成的最后图像!
}
Copy after login

4. What is yours is what is practical.

So…………

(1) Can "black dots" be turned into colored dots? Become love? , turns into a photo of your girlfriend? Become text?

(2) Can you add something in the middle of the image, a word "love", or something that can express your heart?

5. Write your own method

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


}
Copy after login

How to make a personalized QR code?

At present, both corporate and personal websites, and even media platforms have posted their own QR codes. Firstly, they can cater to market demand, and secondly, they can attract everyone’s attention. Due to the continuous expansion of the demand for QR codes, there are more and more types of QR code generators on the market. However, the QR codes generated by most QR code generators are black and white, with a relatively simple appearance and no distinctive features. If you want to pursue individuality and generate colorful QR codes, you can take a look at how Sesame does it.

The QR code generated by the QR code generator that we see most in our daily life is nothing more than a regular square matrix QR code with a regular square in the upper right, upper left, and lower left. It has information acquisition (text, With functions such as business cards, maps, WIFI passwords, website addresses, text messages, and videos, Sesame QR code generator is not limited to these. Sesame QR code generator can not only generate QR codes by inputting text, business cards, website addresses, WIFI, maps, pictures, MP3, Sesame numbers and other information, but can also change the shape and color according to user preferences and even create personalized templates to generate personalized patterns and colors. QR code.
Interface for generating QR codes on Sesame.com
How to generate color QR codes on Sesame.com
1. Register as a Sesame user
2. Select text, business card, website, WIFI, map, picture, MP3 , any type of Sesame account
3. Fill in the content to be generated to generate a QR code. You can choose between ordinary QR codes and personalized templates. If you choose a normal QR code, you can choose your favorite color according to your personal preferences, add a LOGO or adjust the shape to generate a QR code; if you choose a personalized template, you can choose your favorite template to generate a personalized and fun QR code, which can be displayed in real time on the right Preview the generated QR code, and finally download the generated QR code to your local computer!
Normal QR code
Personalized template
Generating color QR code is very simple, try it now! Sodium)

How to generate a personalized and fun QR code?

There are many generator software online now, but most of them are nothing new. I recently tried the new QR code generator from Sesame.com, and I was not disappointed. The generator can adjust the color and gradient at will. , you can also adjust the shape, add a logo, and especially you can choose a personalized template to make the QR code cute and lively.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/860061.htmlTechArticlePersonalize your own QR code, personalize your own QR code 1. What is QR code 2. Us How to make a QR code 3. How to make your own personalized QR code 1. Step one. Download P...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template