1,首先你必鬚生成二維碼具體代碼如下:
複製代碼 代碼如下:
class QRCode{
public $w;
public $h;
public $s;
function __construct($w1,$h1,$s1){
$this->w = $w1;
$this->h = $h1;
$this->s = $s1;
$this->outimgase();
}
function qrcode(){
$post_data = array();
$post_data['cht'] = 'qr';
$post_data['chs'] = $this->w."x".$this->h;
$post_data['chl'] = $this->s;
$post_data['choe'] = "UTF-8";
$url = "http://chart.apis.google.com/chart";
$data_Array = array();
foreach($post_data as $key => $value)
{
$data_Array[] = $key.'='.$value;
}
$data = implode("&",$data_Array);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function outimgase(){
echo $this->qrcode();
}
}
header("Content-type:image/png");
$t = new QRCode(300,300,"tianxin");
2,然後通過一個php檔案將二維碼和你的目的圖片畫在一起代碼如下:
複製代碼 代碼如下:
$surl = $_POST["url"];
function GrabImage($url,$filename="") {
if($url==""):return false;endif;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg"):return false;endif;
$filename=date("dMYHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
$source = GrabImage("http://localhost/QRCode/QRCode.php","Myqrcode.png");
$water =GrabImage($surl,"t.png");
function getImageInfo($img){
$imageInfo = getimagesize($img);
if ($imageInfo !== false) {
$imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
$imageSize = filesize($img);
$info = array(
"width" => $imageInfo[0],
"height" => $imageInfo[1],
"type" => $imageType,
"size" => $imageSize,
"mime" => $imageInfo['mime']
);
return $info;
} else {
return false;
}
}
function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
// 擷取原圖資訊
$info = getImageInfo($image);
if ($info !== false) {
$srcWidth = $info['width'];
$srcHeight = $info['height'];
$type = empty($type) ? $info['type'] : $type;
$type = strtolower($type);
$interlace = $interlace ? 1 : 0;
unset($info);
$scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 計算縮放比例
if ($scale >= 1) {
// 超過原圖大小不再縮減
$width = $srcWidth;
$height = $srcHeight;
} else {
// 縮圖尺寸
$width = (int) ($srcWidth * $scale);
$height = (int) ($srcHeight * $scale);
}
// 載入原圖
$createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
$srcImg = $createFun($image);
//建立縮圖
if ($type != 'gif' && function_exists('imagecreatetruecolor'))
$thumbImg = imagecreatetruecolor($width, $height);
else
$thumbImg = imagecreate($width, $height);
// 複製圖片
if (function_exists("ImageCopyResampled"))
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
if ('gif' == $type || 'png' == $type) {
//imagealphablending($thumbImg, false);//取消預設的混色模式
//imagesavealpha($thumbImg,true);//設定儲存完整的 Alpha 色板資訊
$background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一個綠色
imagecolortransparent($thumbImg, $background_color); // 設定為透明色,若注釋掉該行則輸出綠色的圖
}
// 對jpeg圖形設定隔行掃描
if ('jpg' == $type || 'jpeg' == $type)
imageinterlace($thumbImg, $interlace);
// 產生圖片
$imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
$imageFun($thumbImg, $thumbname);
imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname;
}
return false;
}
function water($source, $thumb, $savename="", $alpha=100){
//檢查檔案是否存在
if (!file_exists($source) || !file_exists($thumb))
return false;
//圖片資訊
$sInfo = getImageInfo($source);
$water = thumb($thumb,"wy.jpg","jpg",$sInfo["width"]/4,$sInfo["height"]/4);
$wInfo = getImageInfo($water);
//如果圖片小於浮水印圖片,不產生圖片
if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height'])
return false;
//建立映像
$sCreateFun = "imagecreatefrom" . $sInfo['type'];
$sImage = $sCreateFun($source);
$wCreateFun = "imagecreatefrom" . $wInfo['type'];
$wImage = $wCreateFun($water);
//設定映像的混色模式
imagealphablending($wImage, true);
//映像位置,預設為右下角靠右對齊
// $posY = $sInfo["height"] - $wInfo["height"];
// $posX = $sInfo["width"] - $wInfo["width"];
$posY = ($sInfo["height"] - $wInfo["height"])/2;
$posX = ($sInfo["width"] - $wInfo["width"])/2;
//產生混合映像
imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
//輸出映像
$ImageFun = 'Image' . $sInfo['type'];
//如果沒有給出儲存檔案名稱,預設為原映像名
if (!$savename) {
$savename = $source;
@unlink($source);
}
//儲存映像
$ImageFun($sImage, $savename);
imagedestroy($sImage);
}
water($source,$water);
在上面的代碼中用3個函數 GrabImage()函數是將產生二維碼的檔案轉化成圖片 接下來的函數就是處理圖片的縮放 將目的圖片添加到二位上。
3,在來一個入口檔案index.html 代碼如下:
複製代碼 代碼如下:
<BR>中間可以自己定義圖片的二維碼產生器 <BR>
注意提交的URL" method="post">
中間可以自己定義圖片的二維碼產生器
http://www.bkjia.com/PHPjc/728109.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/728109.htmlTechArticle1,首先你必鬚生成二維碼具體代碼如下: 複製代碼 代碼如下: class QRCode{ public $w; public $h; public $s; function __construct($w1,$h1,$s1){ $this-w = $w1; $...