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);
// //
Related
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 want to resize image beofre upload it into database. I store image in this way
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$success = $image->move($upload_path1, $image_full_name);
}
Now, I want to resize image before uploading it. I try to use intervation package, I try this
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$img = Image::make($image)->resize(300, 200);
$img->save($upload_path1, 60, $image_full_name);
}
and I got this error
"Encoding format (1691942233153059.jpg) is not supported."
What is the best way to resize image before upload and also set image with unique name?
1- use getRealPath() inside Image::make()
2- save image in particular path.
if($request->hasFile('image')) {
$image = $request->file('image');
$file_name = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 100);
$image_resize->save(public_path('img/' .$file_name));
}
i try to delete some image file in laravel project. i follow tutorial on website, and its work, but tutorial only deleted image based name in column image. the thumbnail on folder image didnt deleted. how to delete image also the thumbnail image? pls help.
this is the store function.
if ($request->hasFile('image'))
{
$file = $request->file('image');
$image = $file->getClientOriginalName();
$destination = public_path() . '/galeri/';
$successUploaded = $request->file('image')->move($destination, $file->getClientOriginalName());
if($successUploaded)
{
$extension = $file->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $image);
Image::make($destination . '/' . $image)
->resize(250, 170)
->save($destination . '/' . $thumbnail);
}
$photo->image = $image;
}
$photo->save();
and this the destroy function
$photo = Photo::where('id', $id)->first();
File::delete('galeri/'.$photo->image);
if($photo){
$photo->delete();
}
Delete a single file
File::delete($filename);
Delete multiple files
File::delete($file1, $file2, $file3);
Delete an array of files
$files = array($file1, $file2);
File::delete($files);
When i save image with my form i change the file name. Everything works nice except file extension. When i change file name the extension is missing and file is saved without it. Please help me to solve my problem i want to store also image extension.
This is my code where i update image name.
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_');
$store = $request->file('image')->storeAs(
$imgPath, $imgName
);
$post->image = $imgName;
}
Try this:
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_') . '.' . $image->extension();
$store = $image->storeAs($imgPath, $imgName);
$post->image = $imgName;
}