0

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

Peppermintology
  • 9,343
  • 3
  • 27
  • 51

1 Answers1

0

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!');
}
EHF Shahab
  • 700
  • 4
  • 14
  • I see this error https://i.stack.imgur.com/x5mUu.png . And I want to upload in the storage folder – Mahmoud Khosravi Jul 04 '22 at 14:30
  • you should create "storage" inside of your public directory and these subdirectories "images", "blogs", "fullSize" and "resized". – EHF Shahab Jul 04 '22 at 14:34
  • I did not understand what you mean. Can you update your post? – Mahmoud Khosravi Jul 04 '22 at 14:49
  • You have public folder, right? Open it and create new folder storage. Open storage and create new folder with name images. Open images and create new folder blogs. Open blogs and create new folders resized and fullSize. In the sample code fullSize goes with capital S. fullSize. – EHF Shahab Jul 04 '22 at 15:02