2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > php两张图片动态合成thinkphp实现二维码及文字水印合并拼接到背景图上

php两张图片动态合成thinkphp实现二维码及文字水印合并拼接到背景图上

时间:2019-04-09 08:00:57

相关推荐

php两张图片动态合成thinkphp实现二维码及文字水印合并拼接到背景图上

实现场景:

1,本站注册的推广分销人员需要有自己的独有邀请码,这个邀请码需要转换成二维码,并让推广员保存在手机相册中,分发到其它群中,进行二维码图片推广

2,但是单独二维码过于简陋, 不够吸引眼球.需要加一个好看的背景.

3,同时,每个会员进会员中心,需要自动把自己的邀请码水印到完全一样的背景广告图上,所以.带二维码的推广图要动态生成.每个人都不同.

4,两个图片不能用css定位遮罩的方式假合成,因为这样,没法在微信上长按保存图片.

5,二维码用第三方的接口.我用的是/api.php?bg=ffffff&fg=000000&el=l&w=200&m=10&text=这是推广码

6,试过用canvas将多图合成单图,电脑上可正常右键保存,但是手机上,没法长按保存为一张图片.参考地址/notes/18363/09eb20879937204f90808dd80418cd81.html

最终选择用thinkphp的方式,把thinkphp以外挂插件的形式引入自己项目

一, 首先下载thinkphp3.2.2版本或上以,php需要5.4版本及以上

下载后,在原网站的根目录中建立水印目录,把thinkphp解压到shuiyin中,目录结构如下

二,在自己网站需要显示二维码图片的模板上增加以下内容

注意,uid_cookie变量表示本站登录会员的邀请码,即会员id,进行固定长度的加密后,放在二维码接口链接中,post到thinkphp中去.以动态生成二维图片.

<a href="javascript:;" onClick="return open_l();" style="margin:0 auto;height:25px;text-align:center;width:70%;display:block;line-height:25px;background:#337ab7;padding:10px;border:solid 1px #eee; border-radius:5px;color: #fff;font-size:16px;">立即邀请</a><div style="color:red;text-align:center">打开图片后,请长按图片,保存到相册</div><br/><div style="padding:10px;color:#888"><p>如何推广?</p><br/>1,点击"立即邀请",请将专属二维码保存至相册<br/>2,找到微信好友,将二维码发给对方<br/>3,好友扫码后进入本站会自动注册并成为您的下线<br/>4,如果该好友发展了下线,则他的下线是您的二级下线<br/>5,佣金提现后,24小时内到账</div><img src="" alt="" id="showImg"width="100%"onclick="return close_l();" style="text-align:center;position: fixed;;bottom:0;top:0;z-index:9999999999999999999;left:0;display:none"><script>function open_l(){document.getElementById('showImg').style.display='block';}function close_l(){document.getElementById('showImg').style.display='none';}$(function(){var json={qrcode:"/api.php?bg=ffffff&fg=000000&el=l&w=200&m=10&text=<?php echo WEB_PATH; ?>/mobile/user/login/<?php echo $uid_cookie;?>"};$.post('/shuiyin/', json, function(data, textStatus, xhr) {/*optional stuff to do after success */$('#showImg').attr('src','/shuiyin/resource/all/'+data)});})</script>

三,在shuiyin\Application\Home\Controller\IndexController.class.php

上面的默认控制器中增加如下内容

解释,本控制器用于接收上面post过来的邀请码及二维码地址,根据网址匹配,从44位向后的部分,就是邀请码了

dlfile()可以远程读取二维码图片

<?php namespace Home\Controller;use Think\Controller;class IndexController extends CommonController{public function index(){if(IS_AJAX){$image = new \Think\Image();$path = I('post.qrcode');$path = I('qrcode');$path = ltrim($path,"\"");$path = rtrim($path,"\"");define('BASE_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/../../../'))."/");$path_qr = BASE_PATH.'/resource/qrcode/';$path_bg = BASE_PATH.'/resource/bg.png';$path_all = BASE_PATH.'/resource/all/';$uid=substr($path, -44);$this->dlfile($path,$path_qr.substr($path, -44).'.png');//echo './shuiyin/resource/qrcode/'.substr($path, -44).'.png';//echo 333;die;//echo $path_qr.substr($path, -44).'.png';die;//echo dirname(__ROOT__);die;//realpath(dirname(__FILE__).'/../')//echo BASE_PATH;die;//echo dirname(file);die;$image->open($path_qr.substr($path, -44).'.png');// 生成一个居中裁剪为120*120的缩略图并保存为thumb.jpg$image->thumb(100, 100,\Think\Image::IMAGE_THUMB_CENTER)->save($path_qr.substr($path, -44).'.png');$location=array(23,366); //图片水印指定位置$image->open($path_bg)->water($path_qr.substr($path, -44).'.png',$location ,80)->save($path_all.substr($path, -44).".png"); $location=array(23,340);//文字水印指定位置$image->open($path_all.substr($path, -44).".png")->text('推广编号 '.$this->_encrypt($uid,'DECODE'),BASE_PATH.'/ThinkPHP/Library/Think/Verify/ttfs/hanyi.ttf',10,'#ffffff',$location)->save($path_all.substr($path, -44).".png");echo substr($path, -44).".png";//echo "<script>self.location.href='".U('/').'./resource/all/'.substr($path, -44).".png"."'</script>";}//$this->theme('Template')->display();}/*加密解密 ENCODE 加密 DECODE 解密*/public function _encrypt($string, $operation = 'ENCODE', $key = '', $expiry = 0){if($operation == 'DECODE') {$string = str_replace('_', '/', $string);}$key_length = 4;// if(defined("G_BANBEN_NUMBER")){// $key = md5($key != '' ? $key : System::load_sys_config("code","code"));// }else{// $key = md5($key != '' ? $key : G_WEB_PATH);// }$key="3svssv";$fixedkey = md5($key);$egiskeys = md5(substr($fixedkey, 16, 16));$runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';$keys = md5(substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));$string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));$i = 0; $result = '';$string_length = strlen($string);for ($i = 0; $i < $string_length; $i++){$result .= chr(ord($string{$i}) ^ ord($keys{$i % 32}));}if($operation == 'ENCODE') {$retstrs = str_replace('=', '', base64_encode($result));$retstrs = str_replace('/', '_', $retstrs);return $runtokey.$retstrs;} else {if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {return substr($result, 26);} else {return '';}}}public function dlfile($file_url, $save_to){$ch = curl_init();curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch,CURLOPT_URL,$file_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $file_content = curl_exec($ch);curl_close($ch);$downloaded_file = fopen($save_to, 'w');fwrite($downloaded_file, $file_content);fclose($downloaded_file);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。