PHP生成推广海报的方法分享


Posted in PHP onApril 22, 2018

经常有这样的需求,就是需要在生成推广海报,包含指定的二维码,分享出去别人扫码之后就可以确定用户推荐关系。

仔细分析一下,推广海报必要的要素就是海报背景图和二维码,这两者都容易生成,但要两者结合到一起组合成为一张图二维还要可以保存到本地便于分享出去,这就是难点了,在H5中可以借助canvas画出来完成类似于截图的功能,但放到小程序里边很多局限性。那么我们直接在后台生成海报,前台直接调用。

前期准备:

1.海报背景图,背景图一般存服务器,程序本地读取;
2.推广二维码,可以是二维码图片链接,也可以是字符串图像流,如果自己生成二维码,详见:使用phpqrcode生成二维码。

方法如下:

/**

生成宣传海报
@param array 参数,包括图片和文字
@param string $filename 生成海报文件名,不传此参数则不生成文件,直接输出图片
@return [type] [description]
*/
function createPoster($config=array(),$filename=""){
//如果要看报什么错,可以先注释调这个header
if(empty($filename)) header("content-type: image/png");
$imageDefault = array(
'left'=>0,
'top'=>0,
'right'=>0,
'bottom'=>0,
'width'=>100,
'height'=>100,
'opacity'=>100
);
$textDefault = array(
'text'=>'',
'left'=>0,
'top'=>0,
'fontSize'=>32, //字号
'fontColor'=>'255,255,255', //字体颜色
'angle'=>0,
);
$background = $config['background'];//海报最底层得背景
//背景方法
$backgroundInfo = getimagesize($background);
$backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
$background = $backgroundFun($background);
$backgroundWidth = imagesx($background); //背景宽度
$backgroundHeight = imagesy($background); //背景高度
$imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
imagefill($imageRes, 0, 0, $color);
// imageColorTransparent($imageRes, $color); //颜色透明
imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
//处理了图片
if(!empty($config['image'])){
foreach ($config['image'] as $key => $val) {
$val = array_merge($imageDefault,$val);
$info = getimagesize($val['url']);
$function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
if($val['stream']){ //如果传的是字符串图像流
$info = getimagesizefromstring($val['url']);
$function = 'imagecreatefromstring';
}
$res = $function($val['url']);
$resWidth = $info[0];
$resHeight = $info[1];
//建立画板 ,缩放图片至指定尺寸
$canvas=imagecreatetruecolor($val['width'], $val['height']);
imagefill($canvas, 0, 0, $color);
//关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
$val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
$val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
//放置图像
imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
}
}
//处理文字
if(!empty($config['text'])){
foreach ($config['text'] as $key => $val) {
$val = array_merge($textDefault,$val);
list($R,$G,$B) = explode(',', $val['fontColor']);
$fontColor = imagecolorallocate($imageRes, $R, $G, $B);
$val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
$val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
}
}
//生成图片
if(!empty($filename)){
$res = imagejpeg ($imageRes,$filename,90); //保存到本地
imagedestroy($imageRes);
if(!$res) return false;
return $filename;
}else{
imagejpeg ($imageRes); //在浏览器上显示
imagedestroy($imageRes);
}
}

使用示例一:生成带有二维码的海报

//2. 在生成的二维码中加上logo(生成图片文件) 
function scerweima1($url=''){ 
require_once 'phpqrcode.php'; 
$value = $url; //二维码内容 
$errorCorrectionLevel = 'H'; //容错级别 
$matrixPointSize = 6; //生成图片大小 
//生成二维码图片 
$filename = 'qrcode/'.microtime().'.png'; 
QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = 'qrcode/logo.jpg'; //准备好的logo图片 
$QR = $filename; //已经生成的原始二维码图 
if (file_exists($logo)) { 
$QR = imagecreatefromstring(file_get_contents($QR)); //目标图象连接资源。 
$logo = imagecreatefromstring(file_get_contents($logo)); //源图象连接资源。 
$QR_width = imagesx($QR); //二维码图片宽度 
$QR_height = imagesy($QR); //二维码图片高度 
$logo_width = imagesx($logo); //logo图片宽度 
$logo_height = imagesy($logo); //logo图片高度 
$logo_qr_width = $QR_width / 4; //组合之后logo的宽度(占二维码的1/5) 
$scale = $logo_width/$logo_qr_width; //logo的宽度缩放比(本身宽度/组合后的宽度) 
$logo_qr_height = $logo_height/$scale; //组合之后logo的高度 
$from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点 
//重新组合图片并调整大小 
/*

imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中 
*/ 
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height); 
} 
//输出图片 
imagepng($QR, 'qrcode.png'); 
imagedestroy($QR); 
imagedestroy($logo); 
return '<img src="qrcode.png" alt="使用微信扫描支付">'; 
} 
//调用查看结果 
echo scerweima1('https://www.baidu.com');

使用示例二:生成带有图像,昵称和二维码的海报

$config = array(
'text'=>array(
array(
'text'=>'昵称',
'left'=>182,
'top'=>105,
'fontPath'=>'qrcode/simhei.ttf', //字体文件
'fontSize'=>18, //字号
'fontColor'=>'255,0,0', //字体颜色
'angle'=>0,
)
),
'image'=>array(
array(
'url'=>'qrcode/qrcode.png', //图片资源路径
'left'=>130,
'top'=>-140,
'stream'=>0, //图片资源是否是字符串图像流
'right'=>0,
'bottom'=>0,
'width'=>150,
'height'=>150,
'opacity'=>100
),
array(
'url'=>'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83eofD96opK97RXwM179G9IJytIgqXod8jH9icFf6Cia6sJ0fxeILLMLf0dVviaF3SnibxtrFaVO3c8Ria2w/0',
'left'=>120,
'top'=>70,
'right'=>0,
'stream'=>0,
'bottom'=>0,
'width'=>55,
'height'=>55,
'opacity'=>100
),
),
'background'=>'qrcode/bjim.jpg',
);
$filename = 'qrcode/'.time().'.jpg';
//echo createPoster($config,$filename);
echo createPoster($config);
PHP 相关文章推荐
ZF等常用php框架中存在的问题
Jan 10 PHP
PhpMyAdmin中无法导入sql文件的解决办法
Jan 08 PHP
PHP 5.3新特性命名空间规则解析及高级功能
Mar 11 PHP
php数据库配置文件一般做法分享
Jul 07 PHP
利用php下载xls文件(自己动手写的)
Apr 18 PHP
getJSON跨域SyntaxError问题分析
Aug 07 PHP
php实现可用于mysql,mssql,pg数据库操作类
Dec 13 PHP
Web程序工作原理详解
Dec 25 PHP
php抓取网站图片并保存的实现方法
Oct 29 PHP
PHP的RSA加密解密方法以及开发接口使用
Feb 11 PHP
PHP实现的AES加密、解密封装类与用法示例
Aug 02 PHP
Laravel timestamps 设置为unix时间戳的方法
Oct 11 PHP
PHP排序算法之归并排序(Merging Sort)实例详解
Apr 21 #PHP
PHP排序算法之快速排序(Quick Sort)及其优化算法详解
Apr 21 #PHP
Laravel模型间关系设置分表的方法示例
Apr 21 #PHP
PHP排序算法之基数排序(Radix Sort)实例详解
Apr 21 #PHP
PHP排序算法之堆排序(Heap Sort)实例详解
Apr 21 #PHP
PHP实现Huffman编码/解码的示例代码
Apr 20 #PHP
PHP排序算法之希尔排序(Shell Sort)实例分析
Apr 20 #PHP
You might like
PHP中冒号、endif、endwhile、endfor使用介绍
2010/04/28 PHP
JavaScript 组件之旅(三):用 Ant 构建组件
2009/10/28 Javascript
jQuery性能优化的38个建议
2014/03/04 Javascript
ie 7/8不支持trim的属性的解决方案
2014/05/23 Javascript
JQuery判断checkbox是否选中及其它复选框操作方法合集
2015/06/01 Javascript
jQuery插件animateSlide制作多点滑动幻灯片
2015/06/11 Javascript
JavaScritp添加url参数并将参数加入到url中及更改url参数的方法
2015/10/26 Javascript
JavaScript实现复制内容到粘贴板代码
2016/03/31 Javascript
扩展bootstrap的modal模态框-动态添加modal框-弹出多个modal框
2017/02/21 Javascript
js实现省市级联效果分享
2017/08/10 Javascript
JS如何实现在页面上快速定位(锚点跳转问题)
2017/08/14 Javascript
vue2里面ref的具体使用方法
2017/10/27 Javascript
javaScript 连接打印机,打印小票的实例
2017/12/29 Javascript
使用JavaScript实现node.js中的path.join方法
2018/08/12 Javascript
element-ui的回调函数Events的用法详解
2018/10/16 Javascript
js实现通过开始结束控制的计时器
2019/02/25 Javascript
Angular利用HTTP POST下载流文件的步骤记录
2020/07/26 Javascript
基于python进行桶排序与基数排序的总结
2018/05/29 Python
详解windows python3.7安装numpy问题的解决方法
2018/08/13 Python
python用for循环求和的方法总结
2019/07/08 Python
在python plt图表中文字大小调节的方法
2019/07/08 Python
详解Python3迁移接口变化采坑记
2019/10/11 Python
python单元测试框架pytest的使用示例
2020/10/07 Python
CSS3样式linear-gradient的使用实例
2017/01/16 HTML / CSS
GIVENCHY纪梵希官方旗舰店:高定彩妆与贵族护肤品
2018/04/16 全球购物
Java面试题:请说出如下代码的输出结果
2013/04/22 面试题
大专毕业生简历的自我评价
2013/10/20 职场文书
文员的职业生涯规划发展方向
2014/02/08 职场文书
汽车销售员如何做职业生涯规划
2014/02/16 职场文书
行政部岗位职责范本
2014/03/13 职场文书
中学生家长评语大全
2014/04/16 职场文书
暑期培训班招生方案
2014/08/26 职场文书
优秀少先队辅导员事迹材料
2014/12/24 职场文书
解决python绘图使用subplots出现标题重叠的问题
2021/04/30 Python
JavaWeb 入门篇(3)ServletContext 详解 具体应用
2021/07/16 Java/Android
Innodb存储引擎中的后台线程详解
2022/04/03 MySQL