官网
//$bigImgPath = $_REQUEST['image'];//背景图
$bigImgPath = 'opcity.png';//背景图
$new_file = "../upload/";//上传路径
if($_REQUEST['content']){
$font = 'font.ttf';//字体
$fontSize = 30; //字体大小
$circleSize = 0; //旋转角度
$left = 20; //左边距
$top = 60; //顶边距
$content = autowrap($fontSize,0,$font,$_REQUEST['content'],419);
//新的判断行数,行数不同,选择透明的背景高度也不一样
$count = count(explode("\n",$content));
if($count==1){
$bigImgPath = '../images/opcity1.png';//背景图
}elseif($count==2){
$bigImgPath = '../images/opcity2.png';//背景图
}elseif($count==3){
$bigImgPath = '../images/opcity3.png';//背景图
}else{
$bigImgPath = '../images/opcity4.png';//背景图
}
$img = imagecreatefromstring(file_get_contents($bigImgPath));
$black = imagecolorallocate($img, 0, 0, 0);//字体颜色 RGB
$slide_color = imagecolorallocate($img, 255, 255, 255);//字体颜色 RGB
imagesavealpha($img,true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
imagefttext($img, $fontSize, $circleSize, $left+3, $top, $slide_color, $font,'"'.$content.'"');//假装是一个描边
imagefttext($img, $fontSize, $circleSize, $left-3, $top, $slide_color, $font,'"'.$content.'"');
imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font,'"'.$content.'"');
list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
header('Content-Type:image/png');
imagepng($img,$new_file.$_REQUEST['filename']);
echo json_encode($new_file.$_REQUEST['filename']) ;
}
function autowrap($fontsize, $angle, $fontface, $string, $width) {
// 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
$content = "";
// 将字符串拆分成一个个单字 保存到数组 letter 中
for ($i=0;$i<mb_strlen($string,'UTF8');$i++) {
$letter[] = mb_substr($string, $i, 1,'UTF8');
}
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
// 判断拼接后的字符串是否超过预设的宽度
if (($testbox[2] > $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
return $content;
}