get error while file upload in laravel - php

I am trying to upload image in laravel. But when i upload it gives error.
Call to a member function getClientOriginalExtension() on null
Here is my Blade file form
{{Form::open(array('url' => '/AddNewBlog','id'=>'blogadd' ,
'method' => 'post','class'=>'form-row','files'=>true,
"enctype"=>"multipart/form-data"))}}
And here is controller
$imagename_bg = time() . '.' . $photo->getClientOriginalExtension();
$destinationPath = public_path('/uploads/blog');
$thumb_img = Image::make($photo->getRealPath())->resize(750, 450);
$thumb_img->save($destinationPath . '/' . $imagename_bg, 80);
$photo->move($destinationPath, $imagename_bg);
Please help me how to resolve this problem.

I am not able to understand your code. if you are looking for uploading image and resize using intervention package try this:-
if($request->hasFile('blogimage')){
if (Input::file('blogimage')->isValid()) {
$file = Input::file('image');
$img = Image::make($file);
$img->resize(400,270);
$name = pathinfo($_FILES['image']['name']);
$destination = public_path('/uploads/blog');
$ext = $name['extension'];
$rand= time().str_random(6).date('h-i-s').'.'.$ext;
$img->save($destination.$rand);
}
}
Or Without intervention pacakge:-
if($request->hasFile('blogimage')){
if (Input::file('blogimage')->isValid()) {
$file = Input::file('blogimage');
$destination = public_path('/uploads/blog');
$ext= Input::file('blogimage')->getClientOriginalExtension();
$mainFilename =time().str_random(5).date('h-i-s');
$file->move($destination, $mainFilename.".".$ext);
}
}
Hope it helps!

$photo = $request->file('file_name');

You only need to use this:
$photo = $request->file("blogimage");
instead of:
$photo = $request->input("blogimage")
Hope this will fixed your problem!

Related

Can't write image data to path in laravel 8 using the Intervention library

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

Failed to rename image and upload using storeAs in Laravel

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);
// //

Intervention \ Image \ Exception \ NotReadableException Unable to find file ()

I have this code to save my post images and it returns error of:
Intervention \ Image \ Exception \ NotReadableException
Unable to find file ().
My code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/');
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename;
}
I've got this code from Intervention \ Image \ Exception \ NotReadableException using laravel 4 but before that i had this code
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/');
$request->file('image')->move($location, $filename);
$food->image = $filename;
}
And it worked just fine, the reason I changed my code was to be able to resize images that's all.
Thanks.
You have to change the permissions on Windows/Temp file. Enable read permisson to the users group.
I have tested your code and fixed error. its working fine now.
$file = $request->file('image');
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/'. $filename);
Image::make($image->getRealPath())->resize(800, 400)->save($location);
}
In my case, i've managed to get it work by adding this entry in php.ini
upload_tmp_dir = "C:\Users\<NAME>\AppData\Local\Temp"
and reload the server
try this
$image = $request->file('image');
$location = public_path('images/');
$filename = $location. ''.'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
Image::make($image->getRealPath())->resize('800','400')->save($filename);
UPDATE
$image = $request->file('image');
$location = public_path('images/');
Image::make($image)->resize('800','400')->save($location.$filename);
Also check this so post here it
$originalImage = 'images/1.jpg';
Image::make($originalImage);
I try many more, finally i got it.I find out if index.php file use in outside public folder then this problem show. Following this way to solve the problem.
$image = $request->file('image');
$input['imagename'] = hexdec(uniqid()).$image->getClientOriginalName();
$location = public_path("upload/image");
$imgs = Image::make($image->getPathname());
$imgs->resize(300 , 300, function ($constraint) { $constraint->aspectRatio(); })->save($location.'/'.$input['imagename']);
I try many more, finally I got it. Following this code I think solved your problem
$location = "assets/images/brand";`
$file = $request->image;
$filename =uniqid().time().'.'.$file->getClientOriginalExtension();
$image = Image::make(file_get_contents($file))->resize('600','600')->save($location.'/'.$filename);

Upload image with different name but keep file extension

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;
}

Uploading image to with custom name in Yii framework

I am trying to upload images from registration form in Yii framework. The image will be saved in "img/avatar" folder and the name of the image should be changed to the username. The piece of code I use for this is below:
//uploading avatar to the img/avatar folder
$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$personModel->picture = $upload_file;
$picture_name = $userModel->username;
$personModel->picture = $picture_name;
if(isset($upload_file))
{
$upload_file->saveAs(Yii::app()->basePath.'/../img/avatar'.$picture_name);
}
$personModel->save();
//end of image uploading part
The problem is: the name of the username has been saved in picture row of the database. But the image was not uploaded to the folder. I am trying to find out the problem in the code. but cannot solve it. Any suggestions?
Well first thing you need to do is to prevent database input if the picture is not saved.
if(isset($uploadedfile))
{
if($upload_file->saveAs(Yii::app()->basePath.'/../img/avatar'.$picture_name)
{
$personModel->save();
}
else
{
//throw error
}
}
As far as problems in the code go. Most common problem is directories not existing, path to them not being correct.
$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$ext = pathinfo($upload_file->picture, PATHINFO_EXTENSION);
$picture_name = $userModel->username . '.' . $ext;
$personModel->picture = $picture_name;
if(isset($upload_file))
{
$upload_file->saveAs('/Your_correct_path/.../etc/'.$picture_name);
}
$personModel->save();
$upload_file = CUploadedFile::getInstance($personModel, 'picture');
$picture_name = $userModel->username . '.' . pathinfo($upload_file, PATHINFO_EXTENSION);
$personModel->picture = $picture_name;
if (isset($upload_file)) {
$upload_file->saveAs(Yii::app()->basePath . '/../img/avatar/' . $picture_name);
}
$personModel->save();
You have to check your folder permission.
The problem has been solved through following code:
$uploadFile = CUploadedFile::getInstance($personModel, 'picture');
$extension = pathinfo($uploadFile, PATHINFO_EXTENSION);
$fileName = $userModel->username . '.' . $extension;
if (isset($uploadFile)) {
$personModel->picture = $fileName;
$uploadFile->saveAs(Yii::app()->basePath . '/../img/avatar/' . $fileName);
}

Categories