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 Labview机器人问题
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr