ホームページ > php教程 > php手册 > QRcode php生成二维码

QRcode php生成二维码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
リリース: 2016-06-06 20:02:24
オリジナル
1771 人が閲覧しました

QRcode 是二维码的一种。 QRcode 可以存储最多 4296 个字母数字类型的任意文本。这些文本可以是任何内容,例如,网址、联系信息、电话号码(具体科查看 二维码数据式 )。 QRcode 存储的信息可以被安装有适当软件的光学设备读

QRcode是二维码的一种。QRcode可以存储最多4296个字母数字类型的任意文本。这些文本可以是任何内容,例如,网址、联系信息、电话号码(具体科查看二维码数据格式)。QR code存储的信息可以被安装有适当软件的光学设备读取。这种设备既可以是专用的QR code读取器也可以是手机。

通过调用 Google Chart Tools / Image Charts  API ,我们可以很方便的生成QRcode

调用方式也很简单,只要向 http://chart.apis.google.com/chart 传入适合的参数就可以了,参数如下:

1. cht=qr
这个是必需的,告诉 API ,你需要生成的是二维码。

2. chs=width>xheight>
这个同样是必需的,告诉 API ,你需要生成的二维码的尺寸。

3. chl=data>
这个还是必需的,用来告诉 API 二维码所包含的信息。可以是数字、字符数字、字符、二进制信息、汉字。不能混合数据类型。数据必须经过UTF-8 URL-encoded。如果需要传递的信息超过2K个字节,请使用POST方式。

4. choe=output_encoding>
终于来了个不是必须的,这个是用来声明生成的二维码所包含信息的编码,默认是 UTF-8 ;其他可选编码是 Shift_JIS 、 ISO-8859-1

5. chld=error_correction_level>|margin>
可选 纠错等级。QR码支持四个等级的纠错,用来恢复丢失的、读错的、模糊的、数据。下面是可选的值:L-(默认)可以识别已损失7%的数据;M-可以识别已损失15%的数据;Q-可以识别已损失25%的数据;H-可以识别已损失30%的数据。margin 是指生成的二维码离图片边框的距离。

QR码是方形的,有相同的长和宽。QR码的大小是固定的:从21177的长/宽,每次递增4个像素点。每个配置被称为一个等级。长和宽越大,存储的信息就越多。下面是版本摘要:

等级为1QR码长和宽分别为21个像素,最多可以存储25个字母数字和字符。

等级为2QR码长和宽分别为25个像素,最多可以存储47个字母数字和字符。

以此类推 。

Chart API会根据你将存储的信息的大小来决定使用哪个等级的QR码。最棒的QR码阅读器可以读取等级为40QR码中存储的信息。然而通常来说移动设备最多可以读取等级为4QR码中存储的信息。

下面来介绍使用PHP调取Google Chart API 来生成二维码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

<?php class qrcode

{

private $data;

 

  

 

 //creating text qr code

 

 public function text($text){

 

  $this->data = $text;

 

 }

 

  

 

 //creating code with link mtadata

 

 public function link($url){

 

  if (preg_match('/^http:\/\//', $url) || preg_match('/^https:\/\//', $url))

 

  {

 

   $this->data = $url;

 

  }

 

  else

 

  {

 

   $this->data = "<a href="http://%22.%24url.%22">http://".$url."</a>";

 

  }

 

 }

 

  

 

 //creating code with bookmark metadata

 

 public function bookmark($title, $url){

 

  $this->data = "MEBKM:TITLE:".$title.";URL:".$url.";;";

 

 }

 

  

 

 //creating code with email address metadata

public function email_address($email)

{

    $this->data= "<a href="mailto:%22.%24email.%22">MAILTO:".$email."</a>";

}

 

 

 //creating code with email metadata

 

 public function email($email, $subject, $message){

 

  $this->data = "MATMSG:TO:".$email.";SUB:".$subject.";BODY:".$message.";;";

 

 }

 

  

 

  //creating code with phone

 

 public function phone_number($phone){

 

  $this->data = "TEL:".$phone;

 

 }

 

  

 

 //creating code with sms metadata

 

 public function sms($phone, $text){

 

  $this->data = "SMSTO:".$phone.":".$text;

 

 }

 

  

 

 //creating code with mms metadata

 

 public function mms($phone, $text){

 

  $this->data = "MMSTO:".$phone.":".$text;

 

 }

 

  

 

 //creating code with mecard metadata

 

 public function contact_info($name, $address, $phone, $email){

 

  $this->data = "MECARD:N:".$name.";ADR:".$address.";TEL:".$phone.";EMAIL:".$email.";;";

 

 }

 

  

 

 //creating code with geo location metadata

 

 public function geo($lat, $lon, $height){

 

  $this->data = "GEO:".$lat.",".$lon.",".$height;

 

 }

 

  

 

 //creating code with wifi configuration metadata

 

 public function wifi($type, $ssid, $pass){

 

  $this->data = "WIFI:T:".$type.";S".$ssid.";".$pass.";;";

 

 }

 

  

 

 //creating code with i-appli activating meta data

 

 public function iappli($adf, $cmd, $param){

 

  $cur = current($param);

 

  $next = next($param);

 

  $param_str = "";

 

  foreach($cur as $key => $val)

 

  {

 

   $param_str .= "PARAM:".$val.",".$next[$key].";";

 

  }

 

  $this->data = "LAPL:ADFURL:".$adf.";CMD:".$cmd.";".$param_str.";";

 

 }

 

  

 

 //creating code with gif or jpg image, or smf, MFi or ToruCa files as content

 

 public function content($type, $size, $content){

 

  $this->data = "CNTS:TYPE:".$type.";LNG:".$size.";BODY:".$content.";;";

 

 }

 

  

 

 //getting image

 

 public function get_image($size = 150, $EC_level = 'L', $margin = '0'){

 

  $ch = curl_init();

 

  $this->data = urlencode($this->data);

 

  curl_setopt($ch, CURLOPT_URL, 'http://chart.apis.google.com/chart');

 

  curl_setopt($ch, CURLOPT_POST, true);

 

  curl_setopt($ch, CURLOPT_POSTFIELDS, 'chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$this->data);

 

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

  curl_setopt($ch, CURLOPT_HEADER, false);

 

  curl_setopt($ch, CURLOPT_TIMEOUT, 30);

 

  

 

  $response = curl_exec($ch);

 

  curl_close($ch);

 

  return $response;

 

 }

 

  

 

 //getting link for image

 

 public function get_link($size = 150, $EC_level = 'L', $margin = '0'){

 

  $this->data = urlencode($this->data);

 

  return 'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$this->data;

 

 }

 

  

 

 //forsing image download

 

 public function download_image($file){

 

  

 

  header('Content-Description: File Transfer');

 

  header('Content-Type: image/png');

 

  header('Content-Disposition: attachment; filename=QRcode.png');

 

  header('Content-Transfer-Encoding: binary');

 

  header('Expires: 0');

 

  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

 

  header('Pragma: public');

 

  header('Content-Length: ' . filesize($file));

 

  ob_clean();

 

  flush();

 

  echo $file;

 

 }

 

}

 

?>

ログイン後にコピー

使用上述二维码 php类的方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?php include("qrcode.php");

$qr = new qrcode();

//bookmark

$title = "标点符";

$url = "http://www.biaodianfu.com/";

$qr->bookmark($title,$url);

  

//获取二维码图片URL

echo "<img  src="%22.%24qr->get_link().%22" alt="QRcode php生成二维码" >";

  

//here is the way to output image

//header("Content-type:image/png");

//echo $qr->get_image();

  

//and here is the way to force image download

//$file = $qr->get_image();

//$qr->download_image($file)

  

?>

ログイン後にコピー


関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート