php制作中间带自己定义图片二维码的方法


Posted in PHP onJanuary 27, 2014

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文件将二维码和你的目的图片画在一起代码如下:

<?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 代码如下:

<html> 
<head> 
<title> 
中间可以自己定义图片的二维码生成器 
</title> 
</head> 
<body style="margin:0px; padding:0px; font-family:宋体; font-size:12px;"> 
<form action="<span style="font-size:18px;"><strong><span style="color:#FF0000;">注意提交的URL</span></strong></span>" method="post"> 
<div style="width:500px; height:200px; background-color:#CCCCCC; margin:auto; border-width:1px; border-color:#000000;" align="center"> 
<h1 style="margin:0px; padding:20px; font-family:宋体; font-size:12px;">中间可以自己定义图片的二维码生成器</h1> 
<table width="500" border="0"> 
<tr> 
<td width="250" height="40" align="center" valign="middle">二维码要生的内容:</td> 
<td width="250" height="40" align="center" valign="middle"> 
<label> 
<input type="text" name="content" value=""> 
</label> 
</td> 
</tr> 
<tr> 
<td width="250" height="40" align="center" valign="middle">希望能添加自己的图片地址:</td> 
<td width="250" height="40" align="center" valign="middle"> 
<label> 
<input type="text" name="url" value=""> 
</label> 
</td> 
</tr> 
<tr> 
<td height="40" colspan="2" align="center" valign="middle"> 
<label> 
<input type="submit" name="Submit" value="生成我想要的二维码"> 
</label> 
</td> 
</tr> 
</table> </div> 
</body> 
</html>
PHP 相关文章推荐
WinXP + Apache +PHP5 + MySQL + phpMyAdmin安装全功略
Jul 09 PHP
php实现httpclient类示例
Apr 08 PHP
php+mysql不用递归实现的无限级分类实例(非递归)
Jul 08 PHP
php实现图片添加描边字和马赛克的方法
Dec 10 PHP
php 5.6版本中编写一个PHP扩展的简单示例
Jan 20 PHP
Laravel 5框架学习之日期,Mutator 和 Scope
Apr 08 PHP
php实现XSS安全过滤的方法
Jul 29 PHP
php上传大文件设置方法
Apr 14 PHP
PHP使用递归算法无限遍历数组示例
Jan 13 PHP
php7 安装yar 生成docker镜像
May 09 PHP
yii2项目实战之restful api授权验证详解
May 20 PHP
PHP implode()函数用法讲解
Mar 08 PHP
基于php和mysql的简单的dao类实现crud操作功能
Jan 27 #PHP
使用php测试硬盘写入速度示例
Jan 27 #PHP
网页上facebook分享功能具体实现
Jan 26 #PHP
php获取当前时间的毫秒数的方法
Jan 26 #PHP
PHP 利用Mail_MimeDecode类提取邮件信息示例
Jan 26 #PHP
php读取纯真ip数据库使用示例
Jan 26 #PHP
curl不使用文件存取cookie php使用curl获取cookie示例
Jan 26 #PHP
You might like
PHP JSON格式数据交互实例代码详解
2011/01/13 PHP
js GridView 实现自动计算操作代码
2009/03/25 Javascript
jquery实现输入框动态增减的实例代码
2013/07/14 Javascript
JS字符串拼接在ie中都报错的解决方法
2014/03/27 Javascript
基于jQuery实现文本框缩放以及上下移动功能
2014/11/24 Javascript
javascript模拟map输出与去除重复项的方法
2015/02/09 Javascript
JavaScript对象属性检查、增加、删除、访问操作实例
2015/07/08 Javascript
学习掌握JavaScript中this的使用技巧
2016/08/29 Javascript
js防阻塞加载的实现方法
2016/09/09 Javascript
jQuery实现的简单悬浮层功能完整实例
2017/01/23 Javascript
form表单数据封装成json格式并提交给服务器的实现方法
2017/12/14 Javascript
原生js实现拖拽功能基本思路详解
2018/04/18 Javascript
基于elementUI使用v-model实现经纬度输入的vue组件
2019/05/12 Javascript
js实现3D粒子酷炫动态旋转特效
2020/09/13 Javascript
基于ajax实现上传图片代码示例解析
2020/12/03 Javascript
[01:50]WODOTA制作 DOTA2中文宣传片《HERO》
2013/04/28 DOTA
Python实现根据指定端口探测服务器/模块部署的方法
2014/08/25 Python
Python做文本按行去重的实现方法
2016/10/19 Python
Python数据类型中的“冒号“[::]——分片与步长操作示例
2018/01/24 Python
python实现聚类算法原理
2018/02/12 Python
可能是最全面的 Python 字符串拼接总结【收藏】
2018/07/09 Python
numpy数组之存取文件的实现示例
2019/05/24 Python
python编写计算器功能
2019/10/25 Python
Python list与NumPy array 区分详解
2019/11/06 Python
微软香港官网及网上商店:Microsoft HK
2016/09/01 全球购物
Guess美国官网:美国知名服装品牌
2019/04/08 全球购物
Charles & Colvard官网:美国莫桑石品牌
2019/06/05 全球购物
区域总监的岗位职责
2013/11/21 职场文书
审计工作个人的自我评价
2013/12/25 职场文书
高中生评语大全
2014/04/25 职场文书
赞美老师的演讲稿
2014/05/22 职场文书
见习报告格式要求
2014/11/04 职场文书
2015新学期校长寄语(3篇)
2015/03/25 职场文书
2015年班长个人工作总结
2015/04/03 职场文书
事业单位工作人员岗前培训心得体会
2016/01/08 职场文书
《家庭教育》读后感3篇
2019/12/18 职场文书