PHP实现文字写入图片功能
本文实例为大家分享了PHP实现文字写入图片的具体代码,供大家参考,具体内容如下
/**
*PHP实现文字写入图片
*/
classwordsOnImg{
public$config=null;
/**
*@param$config传入参数
*@param$config['file']图片文件
*@param$config['size']文字大小
*@param$config['angle']文字的水平角度
*@param$config['fontfile']字体文件路径
*@param$config['width']预先设置的宽度
*@param$config['x']开始写入时的横坐标
*@param$config['y']开始写入时的纵坐标
*/
publicfunction__construct($config=null){
if(empty($config)){
return'mustbeconfig';
}
$fileArr=explode(".",$config['file']);
$config['file_name']=$fileArr[0];
$config['file_ext']=$fileArr[1];
$this->config=$config;
}
/**
*PHP实现图片上写入实现文字自动换行
*@param$fontsize字体大小
*@param$angle角度
*@param$font字体路径
*@param$string要写在图片上的文字
*@param$width预先设置图片上文字的宽度
*@param$flag换行时单词不折行
*/
publicfunctionwordWrap($fontsize,$angle,$font,$string,$width,$flag=true){
$content="";
if($flag){
$words=explode("",$string);
foreach($wordsas$key=>$value){
$teststr=$content."".$value;
$testbox=imagettfbbox($fontsize,$angle,$font,$teststr);
//判断拼接后的字符串是否超过预设的宽度
if(($testbox[2]>$width)){
$content.="\n";
}
$content.=$value."";
}
}else{
//将字符串拆分成一个个单字保存到数组letter中
for($i=0;$i$width)&&($content!=="")){
$content.="\n";
}
$content.=$l;
}
}
return$content;
}
/**
*实现写入图片
*@param$text要写入的文字
*@param$flag是否直接输出到浏览器,默认是
*/
publicfunctionwriteWordsToImg($text,$flag=true){
if(empty($this->config)){
return'mustbeconfig';
}
//获取图片大小
$img_pathWH=getimagesize($this->config['file']);
//打开指定的图片文件
$im=imagecreatefrompng($this->config['file']);
#设置水印字体颜色
$color=imagecolorallocatealpha($im,0,0,255,75);//蓝色
$have=false;
if(stripos($text,"
")!==false){
$have=true;
}
if($have){
$words_text=explode("
",$text);
$words_text[0]=$this->wordWrap($this->config['size'],$this->config['angle'],$this->config['fontfile'],$words_text[0],$this->config['width']);//自动换行处理
$words_text[1]=$this->wordWrap($this->config['size'],$this->config['angle'],$this->config['fontfile'],$words_text[1],$this->config['width']);//自动换行处理
$words_text[2]=$this->wordWrap($this->config['size'],$this->config['angle'],$this->config['fontfile'],$words_text[2],$this->config['width']);//自动换行处理
imagettftext($im,$this->config['size'],$this->config['angle'],$this->config['x'],$this->config['y'],$color,$this->config['fontfile'],$words_text[0]);
imagettftext($im,$this->config['size'],$this->config['angle'],$this->config['x'],$this->config['y']+30,$color,$this->config['fontfile'],"".$words_text[1]);
imagettftext($im,$this->config['size'],$this->config['angle'],$img_pathWH[0]/2+70,$img_pathWH[1]-80,$color,$this->config['fontfile'],$words_text[2]);
if($flag){
header("content-type:image/png");
imagepng($im);
imagedestroy($im);
}
imagepng($im,$this->config['file_name'].'_1.'.$this->config['file_ext']);
imagedestroy($im);
}
$words_text=$this->wordWrap($this->config['size'],$this->config['angle'],$this->config['fontfile'],$text,$this->config['width']);//自动换行处理
imagettftext($im,$this->config['size'],$this->config['angle'],$this->config['x'],$this->config['y'],$color,$this->config['fontfile'],$words_text);
if($flag){
header("content-type:image/png");
imagepng($im);
imagedestroy($im);
}
imagepng($im,$this->config['file_name'].'_1.'.$this->config['file_ext']);
imagedestroy($im);
}
}
$text="DearKang
Ifyoucanholdsomethingupandputitdown,itiscalledweightlifting;ifyoucanholdsomethingupbutcanneverputitdown,it'scalledbuedenbearing.Pitifully,mostofpeoplearebearingheavyburdenswhentheyareinlove.\n\nBeingnicetosomeoneyoudislikedoesn'tmeanyou'reahypocriticalpeople.Itmeansyou'rematureenoughtotolerateyourdisliketowardsthem.
Mr.Kang";
$data=array(
'file'=>'20171226152410.png',
'size'=>12,
'angle'=>0,
'fontfile'=>'./Font/ChalkboardSE.ttc',
'width'=>270,
'x'=>20,
'y'=>70
);
//使用
$wordsOnImgObj=newwordsOnImg($data);
$wordsOnImgObj->writeWordsToImg($text);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。