Marcolino922's avatar

Create a thumb and original image

Hi, in my controller very simply I load an image like this:

// upload original
$path = $request->file('thumb')->store('thumbs');

The image is obviously loaded in its original form, but I would like a smaller thumbnail to be created in addition to the original but keeping its same generic ID by simply adding sdfsdf435345_thumb.ext

0 likes
4 replies
Marcolino922's avatar

I did it like this and it works, but at this point I would like to ask if I can simplify, improve the following code, thanks


                $image = $request->file('thumb');
                $input['imagename'] = Str::random(20).'.'.$image->getClientOriginalExtension();
            
                $destinationPath = storage_path('app\thumbs');
                $img = Image::make($image->getRealPath());
                $img->resize(100, 100)->save($destinationPath.'\thumb_'.$input['imagename']);

                $destinationPath = storage_path('app\thumbs');
                $image->move($destinationPath, $input['imagename']);

                // store in Thumbs
                $createThumb = Thumbs::create([
                    'filename' => 'thumbs/'.$input['imagename'],
                    'thumbable_id' => $createItem->id,
                    'thumbable_type' => 'App\Models\Items',
                ]);

Please or to participate in this conversation.