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面向对象三大特点学习(充分理解抽象、封装、继承、多态)
May 07 PHP
PHP判断文章里是否有图片的简单方法
Jul 26 PHP
destoon实现会员商铺中指定会员或会员组投放广告的方法
Aug 21 PHP
PHP图片处理之图片旋转和图片翻转实例
Nov 19 PHP
php将文本文件转换csv输出的方法
Dec 31 PHP
PHP实现批量生成App各种尺寸Logo
Mar 19 PHP
PHP的serialize序列化数据以及JSON格式化数据分析
Oct 10 PHP
Zend Framework入门应用实例详解
Dec 11 PHP
PHP实现生成推广海报的方法详解
Mar 14 PHP
Yii框架引入coreseek分页功能示例
Feb 08 PHP
PHP+mysql实现的三级联动菜单功能示例
Feb 15 PHP
laravel model 两表联查示例
Oct 24 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
解决phpmyadmin中缺少mysqli扩展问题的方法
2013/05/06 PHP
php中多维数组按指定value排序的实现代码
2014/08/19 PHP
CentOS 7.2 下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法详解(mini版本)
2016/09/01 PHP
用javascript实现自定义标签
2007/05/08 Javascript
基于jquery实现的表格分页实现代码
2011/06/21 Javascript
js 手机号码合法性验证代码集合
2012/09/29 Javascript
jQuery实现动画效果的实例代码
2013/05/07 Javascript
如何判断元素是否为HTMLElement元素
2013/12/06 Javascript
js和jquery分别验证单选框、复选框、下拉框
2015/12/17 Javascript
jQuery unbind 删除绑定事件详解
2016/05/24 Javascript
JS图片延迟加载插件LazyImgv1.0用法分析【附demo源码下载】
2017/09/04 Javascript
javascript json字符串到json对象转义问题
2019/01/22 Javascript
微信小程序常用简易小函数总结
2019/02/01 Javascript
深入理解nodejs搭建静态服务器(实现命令行)
2019/02/05 NodeJs
微信小程序云开发详细教程
2019/05/16 Javascript
Node.js API详解之 assert模块用法实例分析
2020/05/26 Javascript
[43:03]LGD vs Newbee 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
Python实现时钟显示效果思路详解
2018/04/11 Python
python中实现将多个print输出合成一个数组
2018/04/19 Python
PyQt5每天必学之布局管理
2018/04/19 Python
Python小工具之消耗系统指定大小内存的方法
2018/12/03 Python
十个Python练手的实战项目,学会这些Python就基本没问题了(推荐)
2019/04/26 Python
Python 实现数据结构中的的栈队列
2019/05/16 Python
Django使用Celery加redis执行异步任务的实例内容
2020/02/20 Python
python实现将列表中各个值快速赋值给多个变量
2020/04/02 Python
OpenCV 之按位运算举例解析
2020/06/19 Python
Python通过getattr函数获取对象的属性值
2020/10/16 Python
世界上最大的汽车共享网站:Zipcar
2017/01/14 全球购物
澳大利亚购买太阳镜和眼镜网站:Glamoureyes
2020/09/22 全球购物
俄罗斯第一家篮球店:StreetBall
2020/07/30 全球购物
什么是Remote Module
2016/06/10 面试题
总经理职责范文
2013/11/08 职场文书
污水厂厂长岗位职责
2014/01/04 职场文书
《故都的秋》教学反思
2014/04/15 职场文书
2014年平安建设工作总结
2014/11/19 职场文书
婚礼父母答谢词
2015/01/04 职场文书