Home > Enterprise >  Resize image and store in Storage folder
Resize image and store in Storage folder

Time:07-05

Why does not this work?

I want to change the size and save it in the storage folder.

public function store(Request $request)
{
    $image = $request->file('image') ?? null;
    if ($request->hasFile('image'))
    {
        $file = $request->file('image');
        $name = time();
        $extension = $file->getClientOriginalExtension();
        $fileName = $name . '.' . $extension;
        $imageResize = Image::make($file)->resize(600, 300)->save('images/blogs/'.$fileName);
        $image = $file->storeAs('',$imageResize, 'public');
    }
    Blog::query()->create([
        'image' => $image,
    ]);
    return redirect()->route('admin.blogs.index');
}

I see this error

error

Images must be saved in this way

save like this

CodePudding user response:

Please try this and if you want add time to the name of your image simply add it to the input image name. About permission if you are an a production server please set storage directory permission to the 755 but if you are on a local windows machine check if your storage directory has not read-only attribute.

    public function store(Request $request)
    {
    $image = $request->file('image') ?? null;
    if ($request->hasFile('image'))
    {
        $image = $request->file('image');
        $input['imagename'] = $request->file('image')->getClientOriginalName();

        $destinationPath = public_path('storage/images/blogs/resized');
        $img = Image::make($image->path());
        $img->resize(600, 300, function ($constraint) {
            $constraint->aspectRatio();
        })->save($destinationPath.'/'.$input['imagename']);

        $destinationPath = public_path('storage/images/blogs/fullSize');
        $image->move($destinationPath, $input['imagename']);
        
    }
    Blog::query()->create([
        'image' => $image,
    ]);
    return redirect()->route('admin.blogs.index')->withSuccess('Image successfully uploaded!');
}
  • Related