public function getMakeImg(){
}
<?php
//指定图片路径
$src = 'j.png';
// var_dump(file_exists('j.png'));exit;
//获取图片信息
$info = getimagesize($src);
//获取图片扩展名
$type = image_type_to_extension($info[2],false);
//动态的把图片导入内存中
$fun = "imagecreatefrom{$type}";
$image = $fun($src);
//指定字体颜色
$col = imagecolorallocatealpha($image,0,0,0,50);
$kefd_color = imagecolorallocate($image, 255, 255, 255);
//指定字体内容
$content_str = '圣路易斯华盛顿大学辅导介绍';
//计算字符串长度
$num = strlen($content_str);
//判断文字是否需要换行
$shang = bcdiv($num,15);
$begin=0;
for($i=0; $i<=$shang; $i++){
if(empty($begin)){
$content = mb_strimwidth($content_str, 0, 15);
$begin += mb_strlen($content);
} else{
$content = mb_strimwidth($content_str, $begin, 15);
$begin += mb_strlen($content);
}
//给图片添加文字
$x=10;
$y=50+20*$i;
imagettftext($image,15,0,$x,$y,$col,"c:/windows/fonts/simhei.ttf",$content);
}
$kefd = '课业辅导';
//给图片添加文字
imagettftext($image,15,0,10,120,$kefd_color,"c:/windows/fonts/simhei.ttf",$kefd);
//指定输入类型
header('Content-type:'.$info['mime']);
//动态的输出图片到浏览器中
$func = "image{$type}";
$func($image);
//销毁图片
imagepng($image, 'image1.png'); //输出到目标文件
imagedestroy($image); //销毁内存数据流
其他:
mb_strwidth — 返回字符串的宽度
imagettfbbox — 取得使用 TrueType 字体的文本的范围
本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。
imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:
0 左下角 X 位置
1 左下角 Y 位置
2 右下角 X 位置
3 右下角 Y 位置
4 右上角 X 位置
5 右上角 Y 位置
6 左上角 X 位置
7 左上角 Y 位置
图片水平居中
$bbox = imagettfbbox(20, 0,"c:/windows/fonts/simhei.ttf",$content);
//给图片添加文字
$x = ($img_width-($bbox['2']))/2;
$y = $img_hight/2;
imagettftext($image, 20, 0, $x, $y, $col, "c:/windows/fonts/simhei.ttf",$content);