doumi7861 2019-08-02 19:06
浏览 71
已采纳

我想用干预/图像调整照片大小

I want to resize the images I upload with Intervention/image but I don't know how.

I tried a few methods of uploading it resized but I don't know how to make it work. The function storeImage is where i want to resize it.

HomeController.php

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\User;
    use Carbon\Carbon;
    use Illuminate\Support\Facades\Storage;
    use Illuminate\Support\Facades\File;
    use Intervention\Image\ImageManager;
    use Intervention\Image\Facades\Image;

    class HomeController extends Controller
    {

      public function update(User $user){
          $user = auth()->user();

          $user-> name = request('name');
          $user-> phone = request('phone');
          $user-> image = request('image');


          $oldFilename = $user->image;

          $this->storeImage($user);

          File::delete(public_path('images'),$oldFilename);

          $imageExtensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'svg', 'svgz', 'cgm', 'djv', 'djvu', 'ico', 'ief','jpe', 'pbm', 'pgm', 'pnm', 'ppm', 'ras', 'rgb', 'tif', 'tiff', 'wbmp', 'xbm', 'xpm', 'xwd'];

          $foo = \File::extension($user->image);

          if(in_array($foo, $imageExtensions))
          {
            $user->save();
          }


          return redirect('user/account');
        }


        private function storeImage($user){
          if(request()->has('image')){

            $user->image = $user->email.'.'.request()->image->getClientOriginalExtension();

            $imageExtensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'svg', 'svgz', 'cgm', 'djv', 'djvu', 'ico', 'ief','jpe', 'pbm', 'pgm', 'pnm', 'ppm', 'ras', 'rgb', 'tif', 'tiff', 'wbmp', 'xbm', 'xpm', 'xwd'];

            $foo = \File::extension($user->image);
            if(in_array($foo, $imageExtensions))
            {
              $file = request()->image;
              $img = Image::make($file->getRealPath())->resize(320, 240);
              $img->move(public_path('images'), $user->image);

            }

          }
        }
    }

It throws me the error:

Command (Move) is not available for driver (Gd).

I solved it with save and

$img->save(public_path('images'.$user->image)); 

but it doesn't move image to the folder. How can I solve that? Thank you

  • 写回答

3条回答 默认 最新

  • dt3999 2019-08-02 19:18
    关注

    As per the documentation for Intervention image, you need to call save() instead move():

    private function storeImage($user)
    {
        if (request()->has('image')) {
    
            $user->image = $user->email . '.' . request()->image->getClientOriginalExtension();
    
            $imageExtensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'svg', 'svgz', 'cgm', 'djv', 'djvu', 'ico', 'ief', 'jpe', 'pbm', 'pgm', 'pnm', 'ppm', 'ras', 'rgb', 'tif', 'tiff', 'wbmp', 'xbm', 'xpm', 'xwd'];
    
            $foo = \File::extension($user->image);
            if (in_array($foo, $imageExtensions)) {
                $img = Image::make(request()->image)
                    ->resize(320, 240)
                    ->save('images/' . $user->image);
    
            }
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器