laravel 框架上传文件生成简单缩略图

安装:
    需求:
        PHP >= 5.4
        Fileinfo 扩展
        GD库 >= 2.0
        Imagick 扩展 >=6.5.7
    composer安装:
        composer require intervention/image

基本使用:使用 ImageManager 的静态版本
        use Intervention\Image\ImageManagerStatic as Image;
        Image::configure(array('driver' => 'imagick'));        ------- api中未出现此方法
        Image::make('public/foo.jpg');  

上代码------------------:

   public function test2(Request $request)
    {
      
        $this->validate($request,[
            'name' =>'required',
            'content' =>'required',
        ]);
        
         if($request->hasFile('img_file') && $request->file('img_file')->isValid()){
            // 图片 重新命名
           $img_name =  uniqid(mt_rand(100000,999999)) . '.' . $request->img_file-> extension();
            $foldername=date('Y-m-d');        //文件夹名称
            $folderpath="./uploads/{$foldername}";    //文件夹路径
            if(!is_dir($folderpath)){
              $data =  mkdir($folderpath,0777,true);
            }
            // 图片地址
            $image_file = $folderpath .DIRECTORY_SEPARATOR. $img_name;
            // 移动图片并生成缩略图
             Image::make($request->img_file)->resize(100, null, function($constraint){$constraint->aspectRatio();})->save($image_file);
            // dump($image);die;
            // 添加数据表路径
            $path = $image_file;
         }else{
            return "<script> alert('上传图片不能为空!')</script>";
         }
        //  过滤非数据表字段
        $data = $request->except('_token','img_file');
         //  添加到数据表
         $info = [
             'title' => $data['name'],
             'content' => $data['myany'],
             'image_path' =>$path,
             'createtime' =>time(),
         ];
         $data = Stus::insert($info);
        //  添加成功跳转
        if($data){
            return redirect('test1');
        }
      
       
    }

 效果:

 


    

猜你喜欢

转载自www.cnblogs.com/shenchanglu/p/11351865.html