Home > Software design >  Resize image with intervention/image
Resize image with intervention/image

Time:10-15

I have a function to save the picture, and I need it to reduce the picture with the specified size.

I decided to use the package intervention/image

Installed the package with composer require intervention/image

Then I made changes to my function, in the end it turned out like this:

Controller

use Intervention\Image\Facades\Image;

if ($request->hasFile('preview_image')) {
            $file = $request->file('preview_image');
            $namewithextension = $file->getClientOriginalName();
            $name = explode('.', $namewithextension)[0];
            $extension = $file->getClientOriginalExtension();
            $uploadname = $name. '-' .DateTime::createFromFormat('U.u', microtime(TRUE))->format('U-u') . '.' .$extension;
            $img = Image::make($file)->resize(240, 120);
            $article_block_image->preview_image = $img->save(public_path('public/article-block-image/preview' , $uploadname));
        }

Model

public function getOriginPreviewImageUrl()
    {
        return $this->attributes['preview_image'];
    }

    public function getPreviewImageAttribute($value)
    {
        return Storage::exists($value) ? Storage::url($value) : null;
    }

But when I try to upload a picture I get this error:

"message": "Can't write image data to path (D:\\wamp64\\www\\test\\public\\public/article-block-image/preview)", "exception": "Intervention\\Image\\Exception\\NotWritableException", "file": "D:\\wamp64\\www\\test\\vendor\\intervention\\image\\src\\Intervention\\Image\\Image.php",

CodePudding user response:

$path = public_path('public/article-block-image/preview' . $uploadname);

Image::make($image->getRealPath())->resize(468, 249)->save($path);

Run command: php artisan storage:link

  • Related