最近工作中用到生成海报,看了半天的GD库函数,自己参考封装了几个方法,不一定对所有人适用,算是个记录吧,因为只是满足业务需求没有深度去学习,有不正确的地方和不当的专业术语,恳请大牛指导。
参考文章路径:https://www.jb51.net/article/136425.htm
<?php
/**
*这是我做海报的api接口
*/
//这是第一步图片背景
function pictureBackground($background=""){
//1.获取背景图片的信息
$backgroundInfo = getimagesize($background);//$backgroundInfo[2] 是返回的一个可以被image_type_to_extension接收的int图片常量类型
//2.获取我想要合成背景图片的类型方法
$backgroundFun = "imagecreatefrom".image_type_to_extension($backgroundInfo[2], false);
//3.获取背景图片组成一个新的图片(资源)
$background = $backgroundFun($background);//成功获得背景图片资源
//4.新建一个图片资源(用于之后合并资源)
$backgroundWidth = imagesx($background); //背景宽度
$backgroundHeight = imagesy($background); //背景高度
$resource = imagecreatetruecolor($backgroundWidth,$backgroundHeight);//创建一个和背景相同的新资源
//该资源默认黑色背景
//5.将图片资源作为缩略图,合并到新资源当中
imagecopyresampled($resource,$background,0,0,0,0,$backgroundWidth,$backgroundHeight,$backgroundWidth,$backgroundHeight);
//imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);
/*$dst_image:新建的图片
$src_image:需要载入的图片
$dst_x:设定需要载入的图片在新图中的x坐标
$dst_y:设定需要载入的图片在新图中的y坐标
$src_x:设定载入图片要载入的区域x坐标
$src_y:设定载入图片要载入的区域y坐标
$dst_w:设定载入的原图的宽度(在此设置缩放)
$dst_h:设定载入的原图的高度(在此设置缩放)
$src_w:原图要载入的宽度
$src_h:原图要载入的高度*/
return array("resource"=>$resource,"backgroundWidth"=>$backgroundWidth,"backgroundHeight"=>$backgroundHeight);
}
//这是第二步往背景图片上面放你想放的图片
function picturePutImage($resource,$backgroundWidth,$backgroundHeight,$Image=array()){
if(empty($Image))
return false;
foreach($Image as $key=>$val){
//1.获取图片的对应信息
$imageInfo = getimagesize($val["url"]);//获取图片信息
//2.获取图片的资源类型
$function = "imagecreatefrom".image_type_to_extension($imageInfo[2], false);
if(!empty($val['stream'])){ //如果传的是字符串图像流
$imageInfo = getimagesizefromstring($val['url']);//从字符串中获取图像尺寸信息
$function = 'imagecreatefromstring';//定义新建图像的对应方法为字符串
}
//3.获取背景图片组成一个新的图片(资源)
$res = $function($val['url']);//成功获得背景图片资源
$resWidth = $imageInfo[0];//图像的宽度
$resHeight = $imageInfo[1];//图像的高度
$canvas = imagecreatetruecolor($val['width'],$val['height']);//创建一个需要宽高的新资源
if(!empty($val['bakcground_color'])){//里是给图片加上背景色
list($R,$G,$B) = explode(',', $val['bakcground_color']);//组装颜色列表对应的颜色进入对应的值
$color = imagecolorAllocate($canvas,$R,$G,$B);//分配背景色
imagefill($canvas,0,0,$color);//填充背景色
}
//4.将图片资源放大或者缩小放入新建资源中
imagecopyresampled($canvas,$res,0,0,0,0,$val['width'],$val['height'],$resWidth,$resHeight);
if(!empty($val["isRound"])){//是否需要为圆角
$img = $canvas;//将canvas转换到img中
$resWidth= $resHeight=min($val['width'],$val['height']);//获取最小的数值
$canvas = imagecreatetruecolor($resWidth,$resHeight);//新创建一个需要宽高的新资源
$color =imagecolorallocate($canvas,255,255,255);//提取一个白色背景(按照需求设置色值)
imagefill($canvas,0,0,$color);//填充背景色
imageColorTransparent($canvas, $color); //设置背景色为透明色
$r = $resWidth/2;//获取原中心点
for($x=0;$x<$resWidth;$x++){
for($y=0;$y<$resHeight;$y++){
$rgbColor = imagecolorat($img,$x,$y);//获取到对应的像素点
if((($x-$r)*($x-$r)+($y-$r)*($y-$r))<($r*$r)){
imagesetpixel($canvas,$x,$y,$rgbColor);//将获取到的像素点放入canvas资源中
}
}
}
}
//5.将载入好的资源放入大背景资源中
$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($resource,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
}
return $resource;
}
//这是第三步往背景图片中添加文字哦
function picturePutText($resource,$backgroundWidth,$backgroundHeight,$Text=array()){
if(empty($Text))
return false;
foreach($Text as $key=>$val){
list($R,$G,$B) = explode(',', $val['fontColor']);//组装颜色列表对应的颜色进入对应的值
$fontColor = imagecolorallocate($resource, $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'];//获取文字在新建图像中距离左边的定位
$val['fontSize'] = $val['fontSize']/96*72;//将像素转换成磅
if(empty($val['lineFeed'])){
$a = imagettfbbox($val['fontSize'], $val['angle'], $val['fontPath'], $val['text']); //得到字符串虚拟方框四个点的坐标
if(!empty($val['horizontally'])){//表示需要文字居中
$len = $a[2] - $a[0];
$val['left']= ($backgroundWidth-$len)/2;
}
}
if(!empty($val['lineFeed'])){//表示文字换行
$a = imagettfbbox($val['fontSize'], $val['angle'], $val['fontPath'], "来来"); //因为要加上字符的间距所以写两个中文
$oneWord = ($a[2] - $a[0])/2;//获得一个字的长度
if(empty($val['lineFeedWord'])){//表示一行需要显示多少字
$val['lineFeedWord']=20;
}
$oneLenWord =$oneWord*$val['lineFeedWord'];//获得一行字的长度
$val['left']= ($backgroundWidth-($oneLenWord))/2;//获取文字可以排满的宽
$val['text'] = addLineFeed($val['text'],$val['lineFeedWord']);
}
imagettftext($resource,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);//用 TrueType 字体向图像写入文本
}
return $resource;
}
//生成图片
function pictureSaveImage($resource,$filename=""){
if(!empty($filename)){
$res = imagejpeg ($resource,$filename,90); //保存到本地
imagedestroy($resource);
if(!$res) return false;
return $filename;
}else{
imagejpeg ($resource); //在浏览器上显示
imagedestroy($resource);
}
}
function addLineFeed($text="",$num=0){//这是一个检测字符的程序
$i=0;
$j=0;
$str="";
$p=0;
while(!empty($text[$i])){
if(preg_match('/^[\x7f-\xff]+$/', $text[$i].$text[$i+1].$text[$i+2])){
$str.=$text[$i].$text[$i+1].$text[$i+2];
$i+=3;
$j++;
}else{
$str.=$text[$i];
$i++;
if($p==1){
$p=0;
$j++;
}else{
$p=1;
}
}
if($j%($num-1)==0){
$str.=PHP_EOL;
}
}
return $str;
}
header("content-type: image/png");
//需要数据模拟
$background = './7.png';
$image = array(
array(
'url'=>'./zhiwen.png', //图片资源路径
'left'=>462,
'top'=>947,
'stream'=>0, //图片资源是否是字符串图像流
'right'=>0,
'bottom'=>0,
'width'=>106,
'height'=>106,
'opacity'=>100,
'bakcground_color'=>'248,248,248',
'isRound'=>0,
),
array(
'url'=>'./redmoney.png', //图片资源路径
'left'=>321,
'top'=>267,
'stream'=>0, //图片资源是否是字符串图像流
'right'=>0,
'bottom'=>0,
'width'=>100,
'height'=>100,
'opacity'=>100,
'bakcground_color'=>'248,248,248',
'isRound'=>1,
)
);
$Text = array(
array(
'text'=>'您的好友[微信名称]',
'left'=>230,
'top'=>384,
'fontPath'=>'./simhei.ttf', //字体文件
'fontSize'=>30, //字号
'fontColor'=>'51,51,51', //字体颜色
'angle'=>0,
'horizontally'=>1,//文字是否水平居中
),
array(
'text'=>'发现一篇好文章,快来看看吧',
'left'=>194,
'top'=>427,
'fontPath'=>'./simhei.ttf', //字体文件
'fontSize'=>26, //字号
'fontColor'=>'102,102,102', //字体颜色
'angle'=>0,
'horizontally'=>1,//文字是否水平居中
),
array(
'text'=>'产品名称产品名称产品名称产...',
'left'=>34,
'top'=>520,
'fontPath'=>'./simhei.ttf', //字体文件
'fontSize'=>48, //字号
'fontColor'=>'34,34,34', //字体颜色
'angle'=>0,
'horizontally'=>1,//文字是否水平居中
),
array(
'text'=>'文本文本文本文本av本ads文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本',
'left'=>30,
'top'=>656,
'fontPath'=>'./simhei.ttf', //字体文件
'fontSize'=>30, //字号
'fontColor'=>'102,102,102', //字体颜色
'angle'=>0,
'horizontally'=>1,//文字是否水平居中
'lineFeed'=>1,//文字是否自动换行
'lineFeedWord'=>23,
)
);
$res= pictureBackground($background);
$resImage= picturePutImage($res["resource"],$res["backgroundWidth"],$res["backgroundHeight"],$image);
$resText = picturePutText($resImage,$res["backgroundWidth"],$res["backgroundHeight"],$Text);
echo pictureSaveImage($resText,"");