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程序中的常见漏洞进行攻击
Oct 09 PHP
让你同时上传 1000 个文件 (一)
Oct 09 PHP
php urlencode()与urldecode()函数字符编码原理详解
Dec 06 PHP
用PHP提取中英文词语以及数字的首字母的方法介绍
Apr 23 PHP
PHP中使用addslashes函数转义的安全性原理分析
Nov 03 PHP
WAMP环境中扩展oracle函数库(oci)
Jun 26 PHP
php版微信支付api.mch.weixin.qq.com域名解析慢原因与解决方法
Oct 12 PHP
PHP截取发动短信内容的方法
Jul 04 PHP
深入理解PHP中mt_rand()随机数的安全
Oct 12 PHP
PHP7扩展开发之hello word实现方法详解
Jan 15 PHP
wordpress自定义标签云与随机获取标签的方法详解
Mar 22 PHP
PHP设计模式之 策略模式Strategy详解【对象行为型】
May 01 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
在Windows版的PHP中使用ADO
2006/10/09 PHP
Drupal 添加模块出现莫名其妙的错误的解决方法(往往出现在模块较多时)
2011/04/18 PHP
JavaScript与HTML结合的基本使用方法整理
2015/10/12 PHP
PHP实现发送邮件的方法(基于简单邮件发送类)
2015/12/17 PHP
PHP实现基于文本的摩斯电码生成器
2016/01/11 PHP
46 个非常有用的 PHP 代码片段
2016/02/16 PHP
比较全的JS checkbox全选、取消全选、删除功能代码
2008/12/19 Javascript
浅析tr的隐藏和显示问题
2014/03/05 Javascript
Node.js入门教程:在windows和Linux上安装配置Node.js图文教程
2014/08/14 Javascript
jQuery中replaceWith()方法用法实例
2014/12/25 Javascript
Javascript递归打印Document层次关系实例分析
2015/05/15 Javascript
javascript中的深复制详解及实例分析
2016/12/29 Javascript
微信小程序使用input组件实现密码框功能【附源码下载】
2017/12/11 Javascript
微信小程序使用setData修改数组中单个对象的方法分析
2018/12/30 Javascript
Vue父组件监听子组件生命周期
2020/09/03 Javascript
Python 命令行非阻塞输入的小例子
2013/09/27 Python
python多线程用法实例详解
2015/01/15 Python
Python实现按特定格式对文件进行读写的方法示例
2017/11/30 Python
python常用数据重复项处理方法
2019/11/22 Python
浅谈keras保存模型中的save()和save_weights()区别
2020/05/21 Python
PyTorch: Softmax多分类实战操作
2020/07/07 Python
详解python命令提示符窗口下如何运行python脚本
2020/09/11 Python
美国最灵活的移动提供商:Tello
2017/07/18 全球购物
eDreams德国:南欧领先的在线旅游公司
2020/12/07 全球购物
什么是View State?
2013/01/27 面试题
银行实习生的自我评价
2013/12/09 职场文书
幼儿园教师请假制度
2014/01/16 职场文书
汉语言文学专业求职信
2014/06/19 职场文书
县委常委班子专题民主生活会查摆问题及整改措施
2014/09/27 职场文书
2014年幼儿园教师工作总结
2014/11/08 职场文书
2014年办公室工作总结范文
2014/11/12 职场文书
2014年企业工会工作总结
2014/11/12 职场文书
农民工工资保障承诺书
2015/05/04 职场文书
2016国庆节67周年寄语
2015/12/07 职场文书
八年级作文之感悟亲情
2019/11/20 职场文书
pycharm代码删除恢复的方法
2021/06/26 Python