简体   繁体   中英

I want to resize a photo with Intervention/image

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

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);

        }

    }
}

This is the function we use for resize an image with proportions using Intervention\\Image .

public static function imageResizeWithFallback($img, $width, $height, $new_name = false, $quality = 90){

    $new_name = (!empty($new_name)) ? $new_name : $img;

    $imgHandler = Intervention\Image\ImageManagerStatic::make($img);
    $imgHandler->orientate();

    $old = array(
        'filesize'  => $imgHandler->filesize(),
        'width'     => $imgHandler->width(),
        'height'    => $imgHandler->height()
    );

    $imgHandler->widen($width, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    })->heighten($height, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    });

    $imgHandler->save($new_name, $quality);

    $status = array(
        'status' => true,
        'filename' => $new_name
    );


    //fallback if the new image filesize > old image filesize
    if(file_exists($new_name) && $imgHandler->filesize() >= $old['filesize'] && ($old['width'] == $imgHandler->width() && $old['height'] == $imgHandler->height())){
        if($new_name != $img) {
            unlink($new_name);
        }

        $status = array(
            'status' => false,
            'filename' => $img,
            'debug' => array(
                'old' => $old,
                'new' => array(
                    'filesize'  => $imgHandler->filesize(),
                    'width'     => $imgHandler->width(),
                    'height'    => $imgHandler->height()
                )
            )
        );
    }

    return $status;

}

move() is not supported. use save() instead of 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);

            }

          }
        }

You can see the intervention documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM