I tried to resize my image before uploading but is not working but I am able to upload the image, Here is my Controller:
public function store(Request $request)
{
$validatedData = $request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:20480',
]);
$imageFile = $request->file('image');
$name = $imageFile->getClientOriginalName();
$path = $request->file('image')->store('images', 'public');
$resize = Image::make($imageFile)->resize(50, 50)->stream();
$save = new Gallery;
$save->image = $path;
$save->status = 1;
$save->user_id = Auth::id();
$save->save();
return redirect('gallery')->with('success', 'Image has been uploaded')->with('image',$name);
}
try below example source code. I have done image resize functionality in one of my project with Laravel version 8
<?php
if($request->hasFile('dealimg')){
$file = $request->File('dealimg');
$original_name = $file->getClientOriginalName();
$file_ext = $file->getClientOriginalExtension();
$destinationPath = 'uploads/deals';
$file_name = "deal".time().uniqid().".".$file_ext;
// $path = $request->deal_img->store('uploads');
$resize_image = Image::make($file->getRealPath()); //for Resize the Image
$resize_image->resize(150, 150, function($constraint){ //resize with 150 x 150 ratio
$constraint->aspectRatio();
})->save(public_path($destinationPath) . '/' . $file_name);
$deal_img = $destinationPath."/".$file_name;
$request->request->add(['deal_img' => $deal_img]);
}
?>
I hope this one helps to you... see this
Related
I want to after uploading an image save it like this
images/blogs/1650953308.jpg
But I see this
1650953308.jpg
I want to save like this
images/blogs/1650953308.jpg
public function store(Request $request)
{
$fileNameService = $request->file('image') ?? null;
if ($request->hasFile('image'))
{
$file = $request->file('image');
$name = time();
$extension = $file->getClientOriginalExtension();
$fileNameService = $name . '.' . $extension;
$img = Image::make($file->path());
$img->resize(600, 300, function ($constraint) {
$constraint->aspectRatio();
})->save('storage/images/blogs/'.$fileNameService);
}
Blog::query()->create([
'image' => $fileNameService,
]);
return redirect()->route('admin.blogs.index');
}
Concatenate folder path with image name.
Blog::query()->create([
'image' => 'images/blogs/'.$fileNameService,
]);
It will save as images/blogs/1650953308.jpg.
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
Images must be saved in this way
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!');
}
I'm Using Laravel 8 and I'm Facing this error.
Can't write image data to path (/home/u520518518/domains/dev.peacockindia.in/public_html/public/Backend/Image/Brand/2003012866_Brand-Image_Mobile_jpg)
This is My Code
if($request->logo){
$image = $request->file('logo');
$img = rand().'_'.'Brand-Image'.'_'.$request->title.'_'.$image->getClientOriginalExtension();
$loc = public_path('Backend/Image/Brand/'.$img);
Image::make($image)->save($loc);
$brand->logo = $img;
}
$image = $request->file('image');
$slug = Str::slug($request->name);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
if (!file_exists('storage/uploads/employee-images'))
{
mkdir('storage/uploads/employee-images',0777,true);
}
$image->move('storage/uploads/employee-images',$imagename);
}else{
$imagename = "default.png";
}
I tried this
$loc = 'Backend/Image/Brand/'.$img;
and its worked
I created a form to store article with an image , and generate a resized version as thumbnail from it.
I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error
This is the image upload handler function in my controller :
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug;
$successUploaded = $image->storeAs('img/articles-images', $fileName);
if($successUploaded) {
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make('img/articles-images' . '/' . $fileName)
->resize($width, $height)
->save('img/articles-images' . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
storeAs() method, which receives the path, the file name, and the (optional) disk as its arguments :
$successUploaded = $request->file('image')->storeAs(
'images', $fileName
);
I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug.'.' .$image->getClientOriginalExtension();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
// //
I am uploading an image on my form then resizing it using a library. I can upload the pic with no problem here is my code:
public function store()
{
$this->validate(request(),[
'title'=>'required',
'body'=>'required|min:5 ',
'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
$image = request()->file('photo');
$fileName = $image->getClientOriginalName();
$publicPath = 'images/';
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1200, 800);
$image_resize->save($publicPath.$fileName);
auth()->user()->addPost(new Posts( [
'title'=>request('title'),
'body'=>request('body'),
'photo'=> $fileName
]));
return redirect('/');
}
my issue is that the image doesn't show up on my front view. I have tried to remove the resizing its still the same
Well the issue here is that when u are saving the post on database photo option u are saving only the filename, you should concatinate your public path with the image path like so: 'photo'=> $publicPath.$fileName
$image = request()->file('photo');
$fileName = $image->getClientOriginalName();
$publicPath = 'images/';
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1200, 800);
$image_resize->save($publicPath.$fileName);
auth()->user()->addPost(new Posts( [
'title'=>request('title'),
'body'=>request('body'),
'photo'=> $publicPath.$fileName
]));
So try now it should work.