图片打文字水印并保存

/*
$dst_path  图片路径
$filepath  图片的保存路径(不带后缀名)
$color 颜色(数组 array(255,255,255)或是8进制色值)
$font 字体路径
$str  写入的字符串
$x  文字在x轴开始位置 int
$y  文字在y轴结束位置 int
$fontsize  文字大小 int
$degree  文字倾斜角度 float
switch($status){
case 0 : 失败;
case 1 : 成功;
case 3 : 图片路径为空;
case 4 : 保存路径为空;
}
*/
function text_watermark($dst_path,$filepath,$color,$font,$str="哭着的小丑",$x=20,$y=20,$fontsize=18,$degree=0){
if($dst_path==''){
$status = 3;
}elseif($filepath==''){
$status = 4;
}else{
if(is_array($color)){
if(count($color)<3){
$color[0] = $color[0]?$color[0]:255;
$color[1] = $color[1]?$color[1]:255;
$color[2] = $color[2]?$color[2]:255;
}
}else{
$color[0] = 255;
$color[1] = 255;
$color[2] = 255;
}
$dst = imagecreatefromstring(file_get_contents($dst_path));
$image_info = getimagesize($dst_path);
$yanse = imagecolorallocate($dst,$color[0],$color[1],$color[2]);
imagefttext($dst,$fontsize,$degree,(int)$x,(int)$y,$yanse,$font,$str);
$status = 1;
$type = explode("/",$image_info['mime']);
switch($image_info[2]){
case 0 : $status = 0;break;
case 1 : imagegif($dst,$filepath.".".$type[1]);break;
case 2 : imagejpeg($dst,$filepath.".".$type[1]);break;
case 3 : imagepng($dst,$filepath.".".$type[1]);break;
case 15 : imagewbmp($dst,$filepath.".".$type[1]);break;
case 16 : imagexbm($dst,$filepath.".".$type[1]);break;
default : $status = 2;break;
}

}

imagedestory($dst); 

return $status;

}
$dst_path = 'zzmx.jpg';
$color = array(255,255,255);
$font = 'simsun.ttc';
$str = "天使之恋";
$filepath = "rel_path";
$x = 20;
$y = 80;
$fontsize = 50;
$degree = 0;

$num = text_shuiyin($dst_path,$filepath);

嗯,字体自己下载吧

猜你喜欢

转载自blog.csdn.net/qq_27596179/article/details/80905109
今日推荐