今儿特开心 花了点时间把任务完成了 先上效果图
本来这个效果打算时前端写的 但是要求在微信中以及H5 中使用 ,为了用户体验 就只有自己手动通过GD 函数库来画
嗯 不逼逼 先 上代码 看不懂的函数 自己一个个百度
$hostname = $_SERVER["HTTP_HOST"];
if(strpos($hostname,'www') === false){
$hostname = 'www.'.$hostname;
}
//背景图 这里自己注意函数哈 JPG.跟PNG 的函数不一样
$canvas = imagecreatefrompng($this->getRootDir()."/public/static/shared/images/shared2.png");
//设置字体颜色
$fontcolour = imagecolorallocate($canvas, 209, 68, 89);
//封面大图 OSS 图片
$data['coverimages'] =imagecreatefrompng(url);
//头像图片 我这里是阿里云OSS 图片
$data['userhead'] =imagecreatefrompng(url);
//二维码
/*二维码生成我这里就不写了 每个人都不一样 我用的时QRCODE库生成的*/
//由于我是本地生成的 就是流的形式 通过下面函数获得内容 imagecreatefromstring 其余的根据你自己的图片类型 通过不同的函数获得
$qrcodeimages-getqrcode();
$qrcode = imagecreatefromstring($qrcodeimages);
//获得二维码高宽度
//由于getimagesize() 获得参数的时候报错 我就用了下面两个
$imagesx = imagesx($qrcode);
$imagesy = imagesy($qrcode);
//为了不同的背景图 我设了个数组
$coordinate = [
'qrcodex'=>440,
'qrcodey'=>600,
'qrcodesize'=>128,
'coverx'=>80,
'covery'=>80,
'userheadx'=>75,
'userheady'=>630,
'titlesizex'=>80,
'titlesizey'=>400,
'usersizex'=>190,
'usersizey'=>680,
'fontsize'=>20,
'titlefont'=>$this->getRootDir()."font/msyh.ttc",//字库
'usernamefont'=>$this->getRootDir()."/font/msyh.ttc",//字库
'fontcolour'=>$fontcolour
];
//把二维码放在背景图片上
imagecopy($canvas,$qrcode,$coordinate['qrcodex'], $coordinate['qrcodey'],0, 0,$imagesx,$imagesy);
//这里由于需要圆形的 头像 做个转换 我这个HEAD 是提前做过处理 $data['userhead'];
$data['userhead'] = $this->roundnessimages($data['userhead']);
$headimagex = imagesx($data['userhead']);
$headimagey = imagesy($data['userhead']);
//将头像放上去
imagecopy($canvas,$data['userhead'],$coordinate['userheadx'], $coordinate['userheady'],0, 0,$headimagex,$headimagey);
//注意对文字进行格式转换
$fileType = mb_detect_encoding($title , array('UTF-8','GBK','LATIN1','BIG5')) ;
if( $fileType != 'UTF-8'){
$title = mb_convert_encoding($title ,'UTF-8' , $fileType);
}
//标题文字(用于换行)
$titlefont= $this->autowrap(20,0,$coordinate['titlefont'], $title,$coverimagesX);
imagettftext($canvas,20,0,$coordinate['titlesizex'],$coordinate['titlesizey'],$coordinate['fontcolour'],$coordinate['titlefont'],$titlefont);
//名字的文字
imagettftext($canvas,20,0,$coordinate['usersizex'],$coordinate['usersizey'],$coordinate['fontcolour'],$coordinate['usernamefont'],$name);
//输出图片 销毁图片
imagejpeg($canvas);
imagedestroy($canvas);
/*将图片裁剪成圆形主要用于头像*/
private function roundnessimages($src_img){
if(!$src_img){
return false;
}
$w = imagesx($src_img);
$h = imagesy($src_img);
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圆半径
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
return $img;
}
//附送一个比较好用的文字换行的方法
/**
* @desc GD库生成图片中文自动换行
* 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
* */
private function autowrap($fontsize, $angle, $fontface, $string, $width) {
$content = "";
// 将字符串拆分成一个个单字 保存到数组 letter 中
for ($i=0;$i<mb_strlen($string);$i++) {
$letter[] = mb_substr($string, $i, 1);
}
foreach ($letter as $l) {
$teststr = $content."".$l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
// 判断拼接后的字符串是否超过预设的宽度
if (($testbox[2] > $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
$content = mb_convert_encoding($content, "html-entities","utf-8" );
return $content;
}