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 相关文章推荐
php字符串截取问题
Nov 28 PHP
解决GD中文乱码问题
Feb 14 PHP
php分页示例代码
Mar 19 PHP
解决了Ajax、MySQL 和 Zend Framework 的乱码问题
Mar 03 PHP
php的$_FILES的临时储存文件与回收机制实测过程
Jul 12 PHP
PHP中的多行字符串传递给JavaScript的两种方法
Jun 19 PHP
php关联数组快速排序的方法
Apr 17 PHP
PHP输出一个等腰三角形的方法
May 12 PHP
注释PHP和html混合代码的小技巧(分享)
Nov 03 PHP
Yii框架创建cronjob定时任务的方法分析
May 23 PHP
PHP实现微信退款功能
Oct 02 PHP
对php 判断http还是https,以及获得当前url的方法详解
Jan 15 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实现多服务器session共享之NFS共享的方法
2007/03/16 PHP
smarty模板引擎中内建函数if、elseif和else的使用方法
2015/01/22 PHP
php中实现获取随机数组列表的自定义函数
2015/04/02 PHP
我见过最全的个人js加解密功能页面
2007/12/12 Javascript
Extjs Ajax 乱码问题解决方案
2009/04/15 Javascript
基于MooTools的很有创意的滚动条时钟动画
2010/11/14 Javascript
基于jQuery实现文本框缩放以及上下移动功能
2014/11/24 Javascript
Node.js中的缓冲与流模块详细介绍
2015/02/11 Javascript
Javascript对象Clone实例分析
2015/06/09 Javascript
使用contextMenu插件实现Bootstrap table弹出右键菜单
2017/02/20 Javascript
bootstrap table使用入门基本用法
2017/05/24 Javascript
Angular 2父子组件之间共享服务通信的实现
2017/07/04 Javascript
vue loadmore 组件滑动加载更多源码解析
2017/07/19 Javascript
解决vue this.$forceUpdate() 处理页面刷新问题(v-for循环值刷新等)
2018/07/26 Javascript
JavaScript数据结构与算法之检索算法示例【二分查找法、计算重复次数】
2019/02/22 Javascript
Centos7 安装Node.js10以上版本的方法步骤
2019/10/15 Javascript
[01:11:08]Winstrike vs NB 2018国际邀请赛淘汰赛BO1 8.21
2018/08/22 DOTA
Python选择网卡发包及接收数据包
2019/04/04 Python
使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)
2019/05/28 Python
安装python及pycharm的教程图解
2019/10/10 Python
Pytorch基本变量类型FloatTensor与Variable用法
2020/01/08 Python
python小程序基于Jupyter实现天气查询的方法
2020/03/27 Python
python学习将数据写入文件并保存方法
2020/06/07 Python
凯撒娱乐:Caesars Entertainment
2018/02/23 全球购物
药品业务员岗位职责
2014/04/17 职场文书
带病坚持工作事迹
2014/05/03 职场文书
解除财产保全担保书
2014/05/20 职场文书
导师就业推荐信范文
2014/05/22 职场文书
作风建设演讲稿
2014/05/23 职场文书
长江三峡导游词
2015/01/31 职场文书
2015年前台个人工作总结
2015/04/03 职场文书
毕业论文答辩稿范文
2015/06/23 职场文书
呐喊读书笔记
2015/06/30 职场文书
使用numpy实现矩阵的翻转(flip)与旋转
2021/06/03 Python
解决persistence.xml配置文件修改存放路径的问题
2022/02/24 Java/Android
Golang Elasticsearches 批量修改查询及发送MQ
2022/04/19 Golang