laravel8图片缩略图+审核+云存储

1.缩略图

第一步:下载composer包

composer require intervention/image

第二步:修改config/app.php配置

        Intervention\Image\ImageServiceProvider::class,

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

第三步:控制器

       

$logo=$request->file('logo')->store('logo');

 // 修改指定图片的大小

        $img = Image::make($logo)->resize(200, 200)->save($logo);

//文字水印

        $fontPath="你的文字路径";

          $img = Image::make($logo)->text(‘水印名称’,30,30,function($font)use($fontPath){

               $font->file($logo)->size(35)->color('#000);

})->save($logo);


2.审核

第一步:获取token

/**
 * 获取token
 * @return bool|string
 */
public function getToken(){
    $url = 'https://aip.baidubce.com/oauth/2.0/token';
    $post_data['grant_type']       = 'client_credentials';
    $post_data['client_id']      = '你的access_key';
    $post_data['client_secret'] = '你的secret_key';
    $o = "";
    foreach ( $post_data as $k => $v )
    {
        $o.= "$k=" . urlencode( $v ). "&" ;
    }
    $post_data = substr($o,0,-1);

    $res = $this->request_post($url, $post_data);

    return json_decode($res,true);
}
/**
 * curl
 * @param string $url
 * @param string $param
 * @return bool|string
 */
function request_post($url = '', $param = '')
{
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);

    return $data;
}

第二步:图片审核

   

public function store(CreateRequest $request)
{
    $logo=$request->file('logo')->store('logo');

    $tokenRes = $this->getToken();
    $token=$tokenRes['access_token'];
        $url='https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
    $img = file_get_contents($logo);
    $img = base64_encode($img);
    $bodys = array(
        'image' => $img
    );
    $res = $this->request_post($url, $bodys);

    $result=json_decode($res,true);
    if($result['conclusion']=="合规"){
        //图片缩略图+文字水印
        //图片云存储
    }
}

3.七牛云上传

第一步:下载composer包

composer require itbdw/laravel-storage-qiniu

第二步:修改config/app.php配置

itbdw\QiniuStorage\QiniuFilesystemServiceProvider::class,

第三步:修改config/filesystems.php配置

'qiniu' => [  
            'driver'    => 'qiniu',  
            'domain'    => '你的七牛域名',    
            'access_key'=> 'AccessKey',
            'secret_key'=> 'SecretKey',
            'bucket'    => 'Bucket名字,即七牛云存储空间名称 ',
        ],

第四步:控制器

//文件路径
$path = $request->file('logo')->store('logo');
//使用七牛云上传
$disk = \Storage::disk('qiniu');
//上传
$filename = $disk->put($path, file_get_contents($path));

if (!$filename) {
    return redirect(route('create'))->withErrors(['error' => '上传失败']);
}
//获取地址
$logo['name'] = "http://osszfw.czbbt.top/" . $path;

或者:

public function fileAdd(){
 
        $disk = \Storage::disk('qiniu'); //使用七牛云上传
 
        $time = date('Y-m-d');
 
        $filename = $disk->put($time, request()->file('image'));//上传
 
        if(!$filename) {
            echo "上传失败";
        }
 
        $img_url = $disk->getDriver()->downloadUrl($filename); //获取下载链接
 
        echo $img_url;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值