当前位置: 移动技术网 > IT编程>开发语言>PHP > php制作中间带自己定义图片二维码的方法

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

2019年03月31日  | 移动技术网IT编程  | 我要评论
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./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,在来一个入口文件 代码如下:
复制代码 代码如下:

<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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网