Laravel 4 upload 1 image and save as multiple (3)

I am trying to do a script image upload using laravel 4. (using the Resource Controller) and I am using the Intervention Image package.

And I want: when loading an image, to save it as 3 different images (different sizes).

eg:

1-Foo-original.jpg

1-Foo-thumbnail.jpg

1-Foo-resized.jpg

This is what I got so far .. it does not work or something else, but that was how much I could handle it.

if(Input::hasFile('image')) { $file = Input::file('image'); $fileName = $file->getClientOriginalName(); $fileExtension = $file->getClientOriginalExtension(); $type = ????; $newFileName = '1' . '-' . $fileName . '-' . $type . $fileExtension; $img = Image::make('public/assets/'.$newFileName)->resize(300, null, true); $img->save(); } 

Hope someone can help me, thanks!

+6
source share
2 answers

You can try the following:

 $types = array('-original.', '-thumbnail.', '-resized.'); // Width and height for thumb and resized $sizes = array( array('60', '60'), array('200', '200') ); $targetPath = 'images/'; $file = Input::file('file')[0]; $fname = $file->getClientOriginalName(); $ext = $file->getClientOriginalExtension(); $nameWithOutExt = str_replace('.' . $ext, '', $fname); $original = $nameWithOutExt . array_shift($types) . $ext; $file->move($targetPath, $original); // Move the original one first foreach ($types as $key => $type) { // Copy and move (thumb, resized) $newName = $nameWithOutExt . $type . $ext; File::copy($targetPath . $original, $targetPath . $newName); Image::make($targetPath . $newName) ->resize($sizes[$key][0], $sizes[$key][1]) ->save($targetPath . $newName); } 
+8
source

try it

 $file = Input::file('userfile'); $fileName = Str::random(4).'.'.$file->getClientOriginalExtension(); $destinationPath = 'your upload image folder'; // upload new image Image::make($file->getRealPath()) // original ->save($destinationPath.'1-foo-original'.$fileName) // thumbnail ->grab('100', '100') ->save($destinationPath.'1-foo-thumbnail'.$fileName) // resize ->resize('280', '255', true) // set true if you want proportional image resize ->save($destinationPath.'1-foo-resize-'.$fileName) ->destroy(); 
+6
source

All Articles