Laravel 5.2 upload image to storage - laravel

I want to ask, how can I make that somebody selects an image, then he clicks upload and the image will save into my project (example: project/public/images). Thank you :)

You can just do:
$file = $request->file('image');
$name = $file->getClientOriginalName();
$path = public_path("images/$name");
Storage::put($path, File::get($file->getRealPath()));
However. I would recommend to use laravel medialibrary package:
https://docs.spatie.be/laravel-medialibrary/v4/introduction
This way you can just do:
$post->addMediaFromRequest('image')->toCollection('images');

Or else you can do like this
$destinationPath = '';
$filename = '';
if (Input::hasFile('file')) {
$file = Input::file('file');
$destinationPath = public_path().'/images/';
$filename = time() . '_' . $file->getClientOriginalName();
$filename = str_replace(' ','_',$filename);
$uploadSuccess = $file->move($destinationPath, $filename);
}

Related

Image pixelate on resize browser( responsive design ) after upload on laravel

After upload the image, on resize browser, or enter the project on the mobile browser, quality of image is very weak, how can i improve that ?
if($request->hasFile('image')){
$image = $request->file('image');
$filename = $request->name;
$foldername = $request->name;
$imagename = $filename .'.' . $request->image->extension();
$path = public_path('images/produse/'. $filename .'/');
if(!File::exists($path)){
File::makeDirectory($path, 0777, true, true);
}
Image::make($image)->resize(200, 200)->save( public_path('images/produse/' . $foldername . '/' . $imagename ));
}
You should try with a larger resize resolution. For example (800, 600)

Laravel deploy: storage image doesn't work correctly

After deploying my laravel project from local to Apache web server, all works correctly except images link. Here the code:
Images are stored in:
storage/app/public/photos
after i've run command:
php artisan storage:link
Images are linked at:
public/storage/photos
Controller:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photo = $file->storeAs('public/photos', 'foto-' . time() . '.' . $extension);
$user->photo = $photo;
$user->save();
}
Images are uploaded correctly on storage/app/public/photos and correctly linked in public/storage/photos but it doesn't display on frontend.
in blade, i've tried to use Storage::url to retrieve the path
{{Storage::url($user->photo)}}
and asset()
{{asset($user->photo)}}
in both cases, image doesn't exist
The public path of image is:
http://mywebsite.com/storage/photos/foto-1522914164.png
You should Use url function to show your image like below way.
url($user->photo);
I'd suggest to change the controller code as follows:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'foto-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$user->photo = 'photos/' . $photoFileName;
$user->save();
}
Then you can use {{asset($user->photo)}} in your blade.
on my webspace, it seems that the only way to display image correctly is to create a custom route that read and serve the image.
i'm solved like this:
i'm storing only image name in db:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'photo-' . $model->id . '.-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$store = $photoFileName;
}
then, i've create custom route that read images and display them:
Route::get('storage/{filename}.{ext}', function ($filename, $ext) {
$folders = glob(storage_path('app/public/*'), GLOB_ONLYDIR);
$path = '';
foreach ($folders as $folder) {
$path = $folder . '/' . $filename . '.' . $ext;
if (File::exists($path)) {
break;
}
}
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header('Content-Type', $type);
return $response;
});
in blade, i'm using Storage to display image:
{{ Storage::url($photo->photo) }}}

Laravel - Image source not readable after publish website

I was created simple CMS and everything works at localhost. After published website I can't upload image. I have an error "Image source not readable". I changed chmod upload's folder to 777.
My code:
$minphoto_path = 'uploads/img';
$minphoto = $request->minphoto;
$minphoto_new_name = time().$minphoto->getClientOriginalName();
$minphoto->move($minphoto_path, $minphoto_new_name);
$img = Image::make(public_path($minphoto_path . '/' .$minphoto_new_name))->resize(240, 338)->save(public_path($minphoto_path . '/' .$minphoto_new_name));
Try upload that way:
$file = Input::file('file');
$fileName = time() . '-' . $file->getClientOriginalName();
$file->move('uploads', $fileName);
$img = Image::make($file->getRealPath())->resize(320, 240)
->save('public/uploads/'. $file->getClientOriginalName());
Or shorter:
$file = Input::file('file');
$img = Image::make($file)->resize(320, 240)
->save('public/uploads/'. $file->getClientOriginalName());

i m saving the multiple images at a single time with differnt ID in database in Laravel

and it is saved but the issue is all the images have different auto
increment ids in database i want to save the images with the same ids
which is save at the same time,kindly guide me.
****strong text** the code is below;**
$field = Input::file('image');
$location = public_path('uploads/');
foreach ($field as $k => $fd) {
$filename = str_random(2).'-' . time() . "." . $fd->getClientOriginalExtension();
$fd->move($location , $filename);
$product->image = $filename;
}
$user->save();
I think bellow code helps you..
You can try.
$picture = '';
if ($request->hasFile('image')) {
$files = $request->file('image');
foreach($files as $file){
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('His').$filename;
$destinationPath = base_path() . '\public\images';
$file->move($destinationPath, $picture);
}
}
if (!empty($product['images'])) {
$product['images'] = $picture;
} else {
unset($product['images']);
}

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

I'm trying to make an image upload script with laravel 4. (using Resource Controller) and i'm using the package Intervention Image.
And what i want is: when uploading an image to save it as 3 different images (different sizes).
for example:
1-foo-original.jpg
1-foo-thumbnail.jpg
1-foo-resized.jpg
This is what i got so far.. it's not working or anything, but this was as far as i could get with 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();
}
Hopefully someone can help me out, thanks!
You may try this:
$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);
}
Try this
$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();

Resources