安装 intervention/image 扩展包

composer require intervention/image

config/app.php 中加入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
.....

'providers' => [

....

Intervention\Image\ImageServiceProvider::class

]

'aliases' => [

....

'Image' => Intervention\Image\Facades\Image::class

]

.....

至此,插件安装成功。开始上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$fileSize = getimagesize($file); //获取图片宽高
$fontSize = $fileSize[1]/25; //定义文字与图片占比
$bevel = hypot($fileSize[0],$fileSize[1]); //通过勾股定理求斜面
$img = Image::make($file);
//循环铺满全屏
for ($i=0;$i<intval($bevel/($fontSize*5));$i++){
for ($j=0;$j<intval($bevel/($fontSize*8));$j++){
$img->text('证件仅供展示使用', $j*($fontSize*8), $i*($fontSize*5),function ($font)use($fontSize){
$font->file(public_path('font/FZFSJW.TTF'));
$font->size($fontSize);
$font->angle(45);
$font->color('#f40');
});
}
}