1. What is a QR code?
2. How to Create a QR code
3. How to create your own personalized QR code
1. Step 1. Download PHP class library phpqrcode, (Appendix: http://sourceforge.net/projects/phpqrcode)
The use cases on the Internet are as follows:
<? PHP/* $ errorcorrectionlevel Error Correction level: l, m, Q, h $ matrixpointsize indicates the pixel size of each black spot in the image: 1 to 10 */include '/phpqrcode. PHP '; // introduce the php qr code library file $ value = "personalize your own QR code"; // QR code data $ errorcorrectionlevel = "L"; // Error Correction level: l, m, Q, h $ matrixpointsize = "10"; // The point size: 1 to 10 qrcode: PNG ($ value, false, $ errorcorrectionlevel ); exit;?>
2. Read the above Code
What makes the above Code a wonderful journey?
Let me open phpqrcode. php and take a look. The code is too long to be pasted. You can download it yourself.
Take a look at the above Code and phpqrcode. php:
<? PHP/* $ errorcorrectionlevel Error Correction level: l, m, Q, h $ matrixpointsize indicates the pixel size of each black spot in the image: 1 to 10 */include 'phpqrcode/phpqrcode. PHP '; // introduce the php qr code library file $ intext = "personalize your own QR code"; // QR code data $ errorcorrectionlevel = "L"; // Error Correction level: l, m, Q, h $ matrixpointsize = "2"; // vertex size: 1 to 10 $ margin = 1; $ size = 10; $ OUTFILE = false; $ saveandprint = false; $ ENC = qrencode: Factory ($ errorcorrectionlevel, $ size, $ margin); // $ ENC-> encodepng ($ value, false, $ saveandp RINT = false); try {ob_start (); $ tab = $ ENC-> encode ($ intext); print_r ($ tab); $ err = ob_get_contents (); ob_end_clean (); if ($ Err! = '') Qrtools: log ($ OUTFILE, $ ERR);/* mark */$ 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;?>
We can find that the PHP class library phpqrcode first converts the text we need into an array $ tab through an algorithm, and then draws an image, that is, our QR code, through image operations.
If you print the array $ tab, you will find that it is 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)
Okay, do you understand ............
Now it's easy. You can draw images based on the array $ tab:
QRimage::png($tab, $outfile, min(max(1, $enc->size), $maxSize), $enc->margin,$saveandprint);
3. How to draw
If we look at the source code, we will find that the most critical method is:
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4);
The source code I commented out is shown below (the original class library is not commented out)
<? Phpfunction image ($ frame, $ pixelperpoint = 4, $ outerframe = 4) {// $ frame is an array $ tab, $ pixelperpoint, $ outerframe cannot tell what it is, $ H = count ($ frame); $ W = strlen ($ frame [0]); // calculates the length of the image to be drawn. $ h indicates the height, $ W indicates the width $ imgw = $ W + 2 * $ outerframe; $ imgh = $ H + 2 * $ outerframe; // It makes the canvas bigger! Description $ outerframe is the surrounding white size $ base_image = imagecreate ($ imgw, $ imgh); // imagecreate-create a palette-based image, in other words, we can now draw $ Col [0] = imagecolorallocate ($ base_image, 255,255,255) based on $ base_image; $ Col [1] = imagecolorallocate ($ base_image, 0 ); // imagecolorallocate-assign color to an image // The first parameter is set up, and the last three parameters are R, G, and B (the size ranges from 0 to 255 ), you can understand it as pigment ......, The three paints are mixed in different proportions to produce different colors, so $ Col [0] is a white paint brush, $ Col [1] is a black paint brush (Why do the three 255 s are white and the Three 0 s are black? You can imagine an experiment where white light can be broken down in the middle school physics ......) Imagefill ($ base_image, 0, 0, $ Col [0]); // fill in the imagefill area. The entire canvas is white. 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]) ;}}// fill 1 in the $ tab array with black through two loops, the remaining 0 values are white. // $ outerframe indicates white space. $ target_image = imagecreate ($ imgw * $ pixelperpoint, $ imgh * $ pixelperpoint); // imagecreate this function has just been introduced, why call it again ............ And the size is twice that of the original $ pixelperpoint! // Well, $ pixelperpoint is a magnification. Here we start to enlarge the generated image as needed (only the enlarged canvas is generated now) imagecopyresized ($ target_image, $ base_image, 0, 0, 0, 0, $ imgw * $ pixelperpoint, $ imgh * $ pixelperpoint, $ imgw, $ imgh ); // imagecopyresized-copy part of the image and resize it. // zoom in the image you just created and multiply it by $ pixelperpoint. Then copy the image to the newly created canvas ($ base_image ); // imagedestroy-destroy an image return $ target_image; // return the generated final image !}
4. Be steadfast.
So ............
(1) Can "black spots" be changed to colored points? Become love ?, Become a photo of your girlfriend? Into text?
(2) Can I add something in the middle of the image, a word of "love", or something that can express my mind?
5. compile your own Method
Private Static function myimage ($ frame, $ pixelperpoint = 4, $ outerframe = 4, $ point, $ centerpoint) {/** array $ point indicates the style of the filled point * array $ centerpoint indicates the style of the middle part of the image * $ point = array ('kind' => '', // Col, IMG, word 'info' => ''// RGB, filename) * $ centerpoint = array ('kind' =>'', // Col, IMG, word 'info' => '') * not completed, but the idea is the same */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) ;}} 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); 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 ;}}