原生PHP 调用原生GD库 生成海报
Posted 哈尔滨洛弘科技有限公司
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生PHP 调用原生GD库 生成海报相关的知识,希望对你有一定的参考价值。
$config=['background'=>'海报背景图',
'image'=>[
['src' => '图片地址1', 'left' => '距左边距', 'top' => '距上边距', 'opacity' => '透明度 0.1-1', 'width' =>'宽度', 'height' => '高度']
,
['src' => '图片地址2', 'left' => '距左边距', 'top' => '距上边距', 'opacity' => '透明度 0.1-1', 'width' =>'宽度', 'height' => '高度']
],
'text'=>[
['text'=>'文字内容1', 'left' => '距左边距', 'top' => '距上边距','fontSize'=>'字号','transform'=>'字体角度','fontPath'=>'文字字体路径'],
['text'=>'文字内容2', 'left' => '距左边距', 'top' => '距上边距','fontColor'=>'字体颜色','transform'=>'字体角度','fontPath'=>'文字字体路径']
]];
/**
* 创建海报
* @param array $config 生成海报的配置
* @param string $filename 生成后保存地址
* @return bool|string
*/
function createPoster($config = array(), $filename = "")
//如果要看报什么错,可以先注释调这个header
ini_set('default_socket_timeout', 1);
if (empty($filename)) header("content-type: image/png");
$imageDefault = [
'left' => 0,
'top' => 0,
'right' => 0,
'bottom' => 0,
'width' => 100,
'height' => 100,
'opacity' => 100
];
$textDefault = [
'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 = $backgroundInfo[0]; //背景宽度
$backgroundHeight = $backgroundInfo[1]; //背景高度
$imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
//imageColorTransparent($imageRes, $color); //颜色透明
imagefill($imageRes, 0, 0, $color);
imagecopyresampled($imageRes, $background, 0, 0, 0, 0, $backgroundInfo[0], $backgroundInfo[1], imagesx($background), imagesy($background));
//处理了图片
if (!empty($config['image']))
foreach ($config['image'] as $key => $val)
$val = array_merge($imageDefault, $val);
if (strpos($val['src'], 'http://thirdwx.qlogo.cn') === false)
$info = getimagesize($val['src']);
$function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
if (!empty($val['stream'])) //如果传的是字符串图像流
$info = getimagesizefromstring($val['src']);
$function = 'imagecreatefromstring';
$res = $function($val['src']);
$resWidth = $info[0];
$resHeight = $info[1];
//建立画板 ,缩放图片至指定尺寸
$canvas = imagecreatetruecolor($val['width'], $val['height']);
imageColorTransparent($canvas, $color);
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);
else
$res = imagecreatefromjpeg($val['src']);
$resWidth = 132;
$resHeight = 132;
$canvas = imagecreatetruecolor($val['width'], $val['height']);
imageColorTransparent($canvas, $color);
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'], 0, 0, $val['width'], $val['height'], $val['opacity'] * 100);//左,上,右,下,宽度,高度,透明度
//处理文字
if (!empty($config['text']))
foreach ($config['text'] as $key => $val)
$val['fontPath'] = '/static/index/simhei.ttf';
$val = array_merge($textDefault, $val);
if (strpos('rgb', $val['color']) === false)
$rgb = self::hex2rgb($val['color']);
$fontColor = imagecolorallocate($imageRes, $rgb['r'], $rgb['g'], $rgb['b']);
else
$val['color'] = str_replace('rgb(', '', $val['color']);
$val['color'] = str_replace(')', '', $val['color']);
list($R, $G, $B) = explode(',', $val['color']);
$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['font_size'], -$val['transform'], $val['left'], $val['top'] + $val['font_size'], $fontColor, $val['fontPath'], $val['text']);
//生成图片
if (!empty($filename))
$res = imagepng($imageRes, $filename); //保存到本地
imagedestroy($imageRes);
if (!$res) return false;
return $filename;
else
imagepng($imageRes); //在浏览器上显示
imagedestroy($imageRes);
//直接调用即可使用
createPoster($config,'文件保存地址');
如果您的php没有GD库,请看下面安装GD库
liunx安装教程
安装教程
Windows安装教程
把你PHP目录下的ext文件夹里的php_gd2.dll复制到系统目录下,然后修改你的PHP.ini文件,找到以下位置
;extension=php_gd2.dll
把前面的;去掉…
重启你的网页服务器apache或者IIS就可以了
以上是关于原生PHP 调用原生GD库 生成海报的主要内容,如果未能解决你的问题,请参考以下文章