tp图像处理
[TOC]
表单:
<form action="{:url('pic')}" method="post" class="form" enctype="multipart/form-data">
选择文件:<input type="file" class="file" name="img" ><br/>
<select name="type">
<option value="1" selected>图片裁剪</option>
<option value="2">生成缩略图</option>
<option value="3">垂直翻转</option>
<option value="4">水平翻转</option>
<option value="5">图片旋转</option>
<option value="6">添加图片水印</option>
<option value="7">添加文字水印</option>
</select>
<br/>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
pic方法:
$file = $request->file('img');
if(true !== $this->validate(['file' => $file],['file' => 'require|image'])){
$this->error('请选择图片文件');
}else{
//读取图片 获取Image对象
$img = Image::open($file);
switch ($request->param('type')){
case 1:
$img->crop(300,300,200,100);//裁剪 whxy
break;
case 2:
$img->thumb(300,300,Image::THUMB_CENTER);//缩略图 w h type
break;
case 3:
$img->flip();//垂直翻转
break;
case 4:
$img->flip(Image::FLIP_Y);//水平翻转
break;
case 5:
$img->rotate(30);// 旋转 degreess
break;
case 6:
$img->water(ROOT_PATH . 'logo.png',Image::WATER_NORTHEAST,50);//图片水印 图片路径 option:位置 alpha
break;
case 7:
$img->text('maid',VENDOR_PATH . 'topthink\think-captcha\assets\ttfs\1.ttf',20,'#ffffff');//文字水印 text path size color
break;
}
$saveName = $request->time() . '.png';
$img->save(ROOT_PATH . 'public/upload/' . $saveName);
$this->success('图片处理完毕...','./upload/' . $saveName,1);
}
网友评论