一、什麼是二維碼
二、我們如何制作二維碼
三、如何制作自己的個性二維碼
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 — 新建一個基於調色板的圖像,換句話說,我們現在可以基於$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, $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,尤其是還可以選擇個性化模版讓二維碼變得可愛靈動起來。