如何给Laravel图片文件上传加水印功能

2021-06-08   阅读:1900   分类:后端    标签: Laravel

有时我们需要给图片加上版权信息,比如水印。laravel在图片上传中如何添加水印?

1、安装 intervention/image 扩展包

composer require intervention/image

image.png

2、引入扩展包

use Intervention\Image\Facades\Image;

3、Laravel5.8之前版本需要修改 config/app.php 文件(其他版本可以跳过)

<?php
‘providers’ => [
Intervention\Image\ImageServiceProvider::class
]
‘aliases’ => [
‘Image’ => Intervention\Image\Facades\Image::class
]
?>

4、使用

img.jpg为上传原图,water.png为水印图片

<?php
$img = Image::make(public_path('Uploads/img.jpg'));
$img->insert(public_path('Uploads /water.png'),'bottom-right',10, 10);
$img->save();
?>

5、案例例子(laravel8)

<?php
  /*
     * 图片上传接口
     * 文件名
     * 文件夹名
     * */
    public function uploadImg($file,$folder){
        $tmp = $file;
        $folder=$folder ? $folder : "";
        $path = '/Uploads'; //public下的Uploads
        if ($tmp->isValid()) { //判断文件上传是否有效
            $FileType = $tmp->getClientOriginalExtension(); //获取文件后缀
 
            $FilePath = $tmp->getRealPath(); //获取文件临时存放位置
 
            $FileName = $folder.'/'.date('Y-m-d') . uniqid() . '.' . $FileType; //定义文件名
 
            Storage::disk('Uploads')->put($FileName, file_get_contents($FilePath)); //存储文件
 
            $img = Image::make(public_path('Uploads/'.$FileName));
            $img->insert(public_path('Uploads/config/water.png'),'bottom-right',10, 10);
            $img->save();
            return $data = [
                'code' => 0,
                'msg' => '图片上传成功',
                'data'=>['src'=>$path . '/' . $FileName],
            ];
        }
    }
?>

6、相关参数:

left:水印在左边;

right:水印在右边;

top:水印在上方;

bottom:水印在下方;

bottom-left:水印在左下方;

bottom-right:水印在右下方;

top-left:水印在左上方;

top-right:水印在右上方;

laravel文件上传相关文章:Laravel图片上传简单封装及调用方法


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/707.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×