I have successfully uploaded image. But getting below error while trying to resize before upload an image.
Intervention \ Image \ Exception \ NotReadableException
Image source not readable
Controller.php
$image=$request->product_image;
$thumbnailSize = '150X150';
$upload_path='image/product/'.$request->product_name.'/';
$imageName = $request->product_name.time().'.'.$image->getClientOriginalExtension();
$thumbnailImageName = $request->product_name.time().'.'.$image->getClientOriginalExtension().$thumbnailSize;
$image_url = $upload_path.$imageName;
$thumbnail_image_url = $upload_path.$thumbnailImageName;
$image->move(storage_path($upload_path), $imageName);
$resize_image = Image::make($image->getRealPath());
$resize_image->resize(150, 150, function($constraint){
$constraint->aspectRatio();
})->save($upload_path);
I have also updated my composer with "intervention/image": "dev-master" in config/app.php
Anybody Help please ? Thanks in advance.
Try this code
$upload_path = storage_path().'image/product/'.$request->product_name.'/';
$image=$request->product_image;
$imageName = $request->product_name.time().'.'.$image->getClientOriginalExtension();
$image->move($upload_path, $imageName);
$thumbnailSize = '150X150';
$thumbnailImageName = $thumbnailSize.$imageName;
File::copy($upload_path . $imageName, $upload_path . $thumbnailImageName);
Image::make($upload_path . $thumbnailImageName)
->resize(150, 150, function($constraint){
$constraint->aspectRatio();
})->save($upload_path . $thumbnailImageName);
Add this in top
use Image;
use File;
hope this will help!
Related
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!
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);
I have simple resize image function in my laravel project. It should upload original image and make thumbnail of it.
After form submitting I got two images but the original image is placed in wrong directory. This is the function in my controller
if (Input::hasFile('image') && !Input::get('remove_image')) {
$file = Input::file('image');
$filename = str_random(20);
$image = new ImageResize($file);
$original = $filename . '.'. $file->getClientOriginalExtension();
$thumb = $filename . '-thumb.' . $file->getClientOriginalExtension();
$file->move(public_path() . '/uploads', $original);
$imagePath = '/uploads/' . $original;
$thumbPath = '/uploads/' . $thumb;
$image->crop(200, 200);
$image->save('uploads/' . $thumb);
}
Basically when I upload image.jpg I get two images image.jpg and image-thumb.jpg. Both images should be save in uploads i.e. public/uploads/ BUT only thumbnail is saved there.
The original image is saved in **bootstrap**/uploads/. Why is going in bootstrap... directory? I didn't mentioned it anywhere?
You can try to replace the public_path() method to url('/'). Not sure this will help but I don't have good experiences with public_path()
Image::make($request->file('image'))->resize(462, 462)->save('upload_path/filename.jpg'));
Try This Code..
Use Image Intervention to resize and save the image
Image::make($avatar)->resize(250, 250)->save(public_folder('/uploads/'.$filename));
The resize function will resize the image and save function will save the image to uploads folder. Give a desired filename to $filename variable.
Leave only the directory to where you want to save the original. Try to change this line which is moving the image
$file->move(public_path() . '/uploads', $original);
with this one ( remove the public path )
$file->move('uploads', $original);
I am trying to upload and resize an image in Laravel 5 and the Intervention library.
My code in the Controller is this:
// Upload img
if ($req->hasFile('img')) {
// Original
$image = Image::make(Input::file('img'));
// Mid sized
$image_mid = $image->fit(500,500, function($constraint) {
$constraint->upsize();
});
// Thumbnail
$image_thumb = $image->fit(100,100, function($constraint) {
$constraint->upsize();
});
// Path
$path = public_path('upload/artists/' . $id . '/profile/');
if (!File::exists($path)) {
File::makeDirectory($path . 'original', 0775, true, true);
File::makeDirectory($path . 'midsize', 0775, true, true);
File::makeDirectory($path . 'thumb', 0775, true, true);
}
// Save images
$image->save($path . 'original/' . $imgName);
$image_mid->save($path . 'midsize/' . $imgName);
$image_thumb->save($path . 'thumb/' . $imgName);
}
I am already trying to create de directories before moving the image, but it still gives me the same error, that I don't have permissions to move the image to that folder "NotWritableException".
Before using Intervention library, I was moving the image with the ->move() method and it was creating the directory if necessary.
I would really appreciate some help..
Thank you all in advance!
public function newItem(Request $request){
$image = $request->file('image');
$img = time().'.'.$image->getClientOriginalExtension();
$watermark = Image::make('images/watermark.png');
$destinationPath = public_path('/products');
$img = Image::make($image->getRealPath());
$img->resize(300, 365, function ($constraint) {
$constraint->aspectRatio();
})->insert($watermark, 'center');
File::exists($destinationPath) or File::makeDirectory($destinationPath);
$img->save($destinationPath.'/'.$img);
}
I keep getting Can't write image data to path
Can anyone figure out what I'm doing wrong?
The question might seem duplicate, but other suggestions in similar questions did not work for me.
Thanks in advance
For the sake of others that might have the same issue. This is how I solved it:
$image = $request->file('image');
$img = time().'.'.$image->getClientOriginalExtension();
$watermark = Image::make('images/watermark.png');
$destinationPath = public_path('/products');
Image::make($image->getRealPath())->resize(300, 365, function ($constraint) {
$constraint->aspectRatio();
})->insert($watermark, 'center')->save($destinationPath.'/'.$img);
The mistake I was making was assigning Image::make() to a variable. You can look at my code here and the one above in my question.
Try this code it's worked for me
public function newItem(){
$image = Input::file('image');
$destinationPath = '/products';
$img = time().'.'.$image->getClientOriginalExtension();
$watermark = Image::make('images/watermark.png');
$img = Image::make($image->getRealPath());
$img->resize(300, 365, function ($constraint) {
$constraint->aspectRatio();
})->insert($watermark, 'center');
File::exists($destinationPath) or File::makeDirectory($destinationPath);
$img->save($destinationPath.'/'.$img);
}
Make sure to create mentioned path folders (passing with image save) in laravel public folder. This will work automatically.
If somebody use File Facade:
File::exists($destinationPath) or File::makeDirectory($destinationPath);
You have to remember that if your $destinationPath contains more than 1 folder, you have to set 2nd & 3rd parameters like $mode & $recursive to create final destination folder and prepare directory for file upload.
Example:
File::exists($imagePath) or File::makeDirectory($imagePath, 777, true);