Laravel图片处理-缩略图-水印

Posted on 2022-03-09 15:57  君子不卿  阅读(229)  评论(0编辑  收藏  举报

1.安装图像处理扩展

执行命令:

composer require intervention/image 

或者修改composer.json文件
添加 "intervention/image":"2.5.0" 到composer.json文件中
执行命令: composer update 

2. 修改 config/app.php 添加 ServiceProvider完成注册服务提供者

'providers' => [  
  //自定义 图片处理 扩展         
  Intervention\Image\ImageServiceProvider::class, ];
   // 别名 
 'aliases' => [         '
   'Image'=>Intervention\Image\ImageManagerStatic::class
 ] 
 

3. 生成缩略图

//导入 
use Intervention\Image\ImageManagerStatic 
$img = ImageManagerStatic::make('./uploads/1.jpg'); 
//生成缩略图 
$img->resize(325, 434); 
//保存图片 
$img->save('./uploads/1_325X434.jpg'); 
//销毁图片资源 
$img->destroy(); 

4. 图片水印

use Intervention\Image\ImageManagerStatic 
$img = ImageManagerStatic::make('./upload/1.jpg'); 
$filename = $path."1.jpg".microtime(true).mt_rand(100000,999999).'.jpg'; 
$water = './uploads/water.png'; 
$img->insert($water,'center-center',10,20)->save($filename); 
//销毁图片资源 
$img->destroy(); 

5.带透明度的图片水印

use Intervention\Image\ImageManagerStatic 
$water = ImageManagerStatic::make('./uploads/water.png')->opacity(10);
$img = ImageManagerStatic::make('./uploads/1.jpg');
$filename = "./uploads/filename.jpg";
$img->insert($water,'center-center',10,20)->save($filename);
//销毁图片资源
$img->destroy();

 

Copyright © 2024 君子不卿
Powered by .NET 9.0 on Kubernetes

//评论