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 正则学习实例
Jul 30 PHP
php 遍历数据表数据并列表横向排列的代码
Sep 05 PHP
php引用地址改变变量值的问题
Mar 23 PHP
iis下php mail函数的sendmail配置方法(官方推荐)
Apr 25 PHP
解析PHP中如何将数组变量写入文件
Jun 06 PHP
php引用返回与取消引用的详解
Jun 08 PHP
用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤
Jan 19 PHP
ThinkPHP后台首页index使用frameset时的注意事项分析
Aug 22 PHP
php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证
May 04 PHP
浅谈PHP的排列组合(如输入a,b,c 输出他们的全部组合)
Mar 14 PHP
PHP中使用mpdf 导出PDF文件的实现方法
Oct 22 PHP
PHP Trait功能与用法实例分析
Jun 03 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 Image Resize图片大小调整的函数代码
2011/01/17 PHP
基于PHPExcel的常用方法总结
2013/06/13 PHP
apache配置虚拟主机的方法详解
2013/06/17 PHP
php foreach循环中使用引用的问题
2013/11/06 PHP
采用thinkphp自带方法生成静态html文件详解
2014/06/13 PHP
PHP实现批量重命名某个文件夹下所有文件的方法
2017/09/04 PHP
AJAX架构之Dojo篇
2007/04/10 Javascript
jquery创建div 实现代码
2009/04/27 Javascript
JavaScript 模式之工厂模式(Factory)应用介绍
2012/11/15 Javascript
jquery.ajax的url中传递中文乱码问题的解决方法
2014/02/07 Javascript
基于JavaScript代码实现兼容各浏览器的设为首页和加入收藏
2016/01/07 Javascript
javascript拖拽效果延伸学习
2016/04/04 Javascript
Javascript 函数的四种调用模式
2016/11/05 Javascript
vue2.0的contextmenu右键弹出菜单的实例代码
2017/07/24 Javascript
js匿名函数使用&amp;传参(实例)
2017/09/08 Javascript
Grunt针对静态文件的压缩,版本控制打包的实例讲解
2017/09/29 Javascript
Vue实现table上下移动功能示例
2019/02/21 Javascript
JS监听组合按键思路及实现过程
2020/04/17 Javascript
vue中axios封装使用的完整教程
2021/03/03 Vue.js
[39:19]完美世界DOTA2联赛PWL S2 SZ vs LBZS 第二场 11.26
2020/11/30 DOTA
python使用urlparse分析网址中域名的方法
2015/04/15 Python
Python读写ini文件的方法
2015/05/28 Python
python实时监控cpu小工具
2018/06/21 Python
对django2.0 关联表的必填on_delete参数的含义解析
2019/08/09 Python
python实现飞船游戏的纵向移动
2020/04/24 Python
pycharm sciview的图片另存为操作
2020/06/01 Python
如何理解python对象
2020/06/21 Python
如何用Matlab和Python读取Netcdf文件
2021/02/19 Python
HTML5混合开发二维码扫描以及调用本地摄像头
2017/12/27 HTML / CSS
北大自主招生自荐信
2013/10/19 职场文书
数控专业推荐信范文
2013/12/02 职场文书
运动会开幕式邀请函
2014/01/22 职场文书
《绿色蝈蝈》教学反思
2014/03/02 职场文书
股东合作协议书
2014/04/14 职场文书
关于建议书的格式范文
2014/05/20 职场文书
Python编程编写完善的命令行工具
2021/09/15 Python