<?php/** php使用二维码**/class MyQrcode{ const SIZE = 150; const LEVEL = "L"; const MARGIN = 4; /** logo 存放logo文件的地址 工厂方法创建二维码 **/ public static function factoryCreateQrcode($type, $string, $filename = false, $logo = null,$level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN){ $type = strtolower($type); if(!in_array($type, array("google", "php", "logo"))){ return false; } $method = "createQrcodeFrom".ucfirst($type); return call_user_func_array( 'self::'.$method, array($string, $filename, $logo, $level, $size, $margin, $logo) ); } /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param string $chl 二维码包含的信息,可以是数字、字符、二进制信息、汉字。不能混合数据类型,数据必须经过UTF-8 URL-encoded.如果需要传递的信息超过2K个字节,请使用POST方式 * @param int $widhtHeight 生成二维码的尺寸设置 * @param string $EC_level 可选纠错级别,QR码支持四个等级纠错,用来恢复丢失的、读错的、模糊的、数据。 * L-默认:可以识别已损失的7%的数据 * M-可以识别已损失15%的数据 * Q-可以识别已损失25%的数据 * H-可以识别已损失30%的数据 * @param int $margin 生成的二维码离图片边框的距离 @return 图像 */ public static function createQrcodeFromGoogle($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN){ return 'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$level.'|'.$margin.'&chl='.urlencode($string); } /** 参数同上 **/ public static function createQrcodeFromPhp($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN) { //加载你的php QR库 $dir = dirname(__FILE__).DIRECTORY_SEPARATOR; require_once $dir."phpqrcode".DIRECTORY_SEPARATOR."phpqrcode.php"; $filename = $dir.$filename; QRcode::png($string, $filename, $level, $size, $margin); return $filename; } public static function createQrcodeFromLogo($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN) { $qrfile = self::createQrcodeFromPhp($string, $filename,$logo, $level, $size, $margin); return self::creatQrcodeWithLogo($logo, $qrfile); } /** @param $logo logo文件 @param $qrfile qrfile本地文件 @return 输出到本地文件或者输出到浏览器 **/ public static function creatQrcodeWithLogo($logo, $qrfile, $size=6)
{ if(!is_file($qrfile) || !is_file($logo)){ return false; } $QR = imagecreatefrompng($qrfile);//外面那QR图 if ($logo !== FALSE) { $logo = imagecreatefromstring(file_get_contents($logo)); $QR_width = imagesx($QR); $QR_height = imagesy($QR); $logo_width = imagesx($logo); $logo_height = imagesy($logo); $logo_qr_width = $QR_width/$size; $scale = $logo_width/$logo_qr_width; $logo_qr_height = $logo_height/$scale; $from_width = ($QR_width-$logo_qr_width)/2; imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); } //二维码文件存在输出到文件否则输出到浏览器 if(is_file($qrfile) && strpos($qrfile, "http") === false){ imagepng($QR, $qrfile); } else{ header('Content-type: image/png'); imagepng($QR); imagedestroy($QR); } }}/*$type = "php";$string = "beck.bi";$filename = "test1.png";MyQrcode::factoryCreateQrcode($type, $string, $filename );*//*$type = "google";$string = "beck";var_dump(MyQrcode::factoryCreateQrcode($type, $string));*/$type = "logo";$string = "beck.bi";$filename = "test2.png";$logo = "1.jpg";MyQrcode::factoryCreateQrcode($type, $string, $filename, $logo);?>