借鉴大佬的,文章出处https://blog.csdn.net/u014391889/article/details/89920439
最近研究图片的生成然后用了canvas,用了下不方便,额可能我前端很菜,调的不是很好,然后干脆后台生成算了
// 图片生成
public function print($post){
$source='./skin/uploads/190412/zhengsu.png';//图片路径
$text1=$post['a'];
$text2=$post['b'];
$text3=$post['c'];
$text4=$post['d'];
$font = './skin/uploads/190412/msyh.ttf';//文字路径
@$date = '' . date ( 'Ymd' ) . '/';
$img = $date . md5 ( $source . $text1 . $text2 . $text3 . $text4 ) . '.jpg';
if (file_exists ( './' . $img )) {//文件是否存在
return $img;
}
$main = imagecreatefrompng( $source );//载入一幅图像,成功返回图像资源,注意后缀我这个是png,需要什么换什么的
//获取宽高
$width = imagesx ( $main );
$height = imagesy ( $main );
$target = imagecreatetruecolor ( $width, $height );//创建画布
$white = imagecolorallocate ( $target, 255, 255, 255 );//调整颜色
imagefill ( $target, 0, 0, $white );//区域填充
imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height );// 重新获取图片
$fontSize = 17;//磅值字体
$fontColor = imagecolorallocate ( $target, 14, 2, 12);//字的RGB颜色
$namelen = mb_strlen($text1,"utf-8");//获取长度调整大小
if($namelen == 2){
$x = 425;
}else{
$x = 410;
}
$fontBox = imagettfbbox($fontSize, 0, $font, $text1);//文字水平居中实质
imagettftext ( $target, 23, 0, $x, 505, $fontColor, $font, $text1 );
$fontBox = imagettfbbox($fontSize, 0, $font, $text2);
imagettftext ( $target, $fontSize, 0, 378, 634, $fontColor, $font, $text2 );
$fontBox = imagettfbbox($fontSize, 0, $font, $text3);
imagettftext ( $target, $fontSize, 0, 378, 678, $fontColor, $font, $text3 );
$fontBox = imagettfbbox($fontSize, 0, $font, $text4);
imagettftext ( $target, $fontSize, 0, 378, 721, $fontColor, $font, $text4 );
//imageantialias($target, true);//抗锯齿,有些PHP版本有问题,谨慎使用
// imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20 + 0, 12 + 142), 3, $fontColor );//画三角形
// imageline($target, 100, 200, 20, 142, $fontColor);//画线
// imagefilledrectangle ( $target, 50, 100, 250, 150, $fontColor );//画矩形
//bof of 合成图片
// $child1 = imagecreatefromjpeg ( 'http://gtms01.alicdn.com/tps/i1/T1N0pxFEhaXXXxK1nM-357-88.jpg' );
// imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
//eof of 合成图片
@mkdir ( './' . $date ); //创建目录
imagejpeg ( $target, './' . $img, 95 ); //输出文件
// 销毁
imagedestroy ( $main );
imagedestroy ( $target );
// imagedestroy ( $child1 );
return $img;
}
总结:
1.字体要去下载的,我随便找了一个就不粘贴了
2.imagecreatefrompng 这个后缀看你引入图片的格式修改不然报错