为图片加水印
composer require topthink/think-image
//【水印类库】
namespace app\api\classes;
use think\Image;
class imgWaterClass
{
/**图片文字水印
* object(think\Image)#47 (3) {
["im":protected] => resource(96) of type (gd)
["gif":protected] => NULL
["info":protected] => array(4) {
["width"] => int(750)
["height"] => int(450)
["type"] => string(4) "jpeg"
["mime"] => string(10) "image/jpeg"
}
}
*
*/
public function imageWaterText($path,$text){
$img = ".".$path;
$image = Image::open($img);
$image->text($text,'./static/style/font/simsun.ttc',20,'#ffffff',9 ,"-10px")->save($img);
return $img;
}
public function imageWaterImg($path,$logo){
$img = ".".$path;
$logo = ".".$logo;
$image = Image::open($img);
$image->water($logo,Image::WATER_SOUTHEAST)->save($img);
return $img;
}
}
后台程序处理
public function save(){
$data = Request::param();
$water = new imgWaterClass();
$img_url = $data['face'];//需要添加水印的图片
$path = "/uploads/logo.png";//水印图片
$img = $water->imageWaterImg($img_url,$path);//添加水印图片
$img_text = $water->imageWaterText($img_url,'我是水印');//添加水印文字
if($img){
return ['code'=>1,'msg'=>'保存成功'];
}else{
return ['code'=>0,'msg'=>'保存失败'];
}
}