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!
Related
I used Interventation image to resize a big size uploaded image. But I don't want to save it into the local directory of Laravel. This is my sample source code.
public function Uploader(Request $request) {
$upload_file = $request->file_upload;
$new_img = Image::make($upload_file)->resize(800, 544);
$new_img->save(\public_path($fileName));
$upload_file_new = "../public/" . $fileName;
}
I want to get the new path of the resized image (by not by saving it). I used
$path = $new_img->basePath();
But it returns the basePath of the $upload_file. How can I get the new path so I can use it on fopen($upload_file_new, 'r'). Please help. Thank you.
you can do like this
$image_new_name = time() . str_random(10) . str_replace(' ','',$upload_file->getClientOriginalName());
I used ImageOptimizer package for reducing image size.
source: http://image.intervention.io/getting_started/installation
in controller
use Image;
if (Input::hasFile('title_image')) {
/*$this->validate($request,[
'photo' =>'required|image|mimes:jpg,jpeg,png|max:2048'
]);*/
$Product = Input::file('title_image');
$Product->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}
Now I want to convert image in this function when I upload. if I add this method $img = Image::make('foo.jpg')->resize(300, 200); it says storage not found error.
Now what can I do. please give me some suggestion. Thanks in advance.
Please use "resize function" before move into desire folder.
use Image;
if (Input::hasFile('title_image')) {
/*$this->validate($request,[
'photo' =>'required|image|mimes:jpg,jpeg,png|max:2048'
]);*/
$Product = Input::file('title_image');
$filename = time() . '.' . $Product->getClientOriginalExtension();
Image::make($Product)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) )->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}
if you want reduce or resize the image you can use the laravel image compression package before upload the image.this is link below how you can do that.
https://www.itsolutionstuff.com/post/laravel-compress-image-before-upload-exampleexample.html
image compression is need more because when browser access the url and that page having the more that 10 image then it first download the all image and send request to server to for each due to which its taking more time to load the page.so,get ride from this problem use image compression package.
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!
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);
So yesterday I tried to make an upload file function , for when user makes his products, he can also upload a picture too.
But the picture was too big when I was iterating through the items, so I decided to use intervention package to resize the picture and also create a thumbnail picture.
I made the function but its partially working.
if($file = $request->hasFile('image')) {
$file = $request->file('image');
$extension = $file->getClientOriginalName();
$username = Auth::user()->username;
$destinationPath = public_path('/uploads/products/' . $username);
$thumb = Image::make($file->getRealPath())->resize(100, 100, function ($constraint) {
$constraint->aspectRatio(); //maintain image ratio
});
$thumb->save($destinationPath.'/thumb_'.$extension);
$destinationPath = public_path('/uploads/products/' . $username);
$file->move($destinationPath, $extension);
$product['imagePath'] = '/uploads/products/'. $username . '/' . $extension;
$product['thumbnail'] = '/uploads/products/'. $username . '/thumb_' . $extension;
}
I made it so, different user will create a different file in /uploads/products.
Also I upload the original picture and the resized so the I should have like:
picture.jpg and thumb_picture.jpg.
When the custom file is not created (from the name of the user) I get this error:
Can't write image data to path
(C:\xampp\htdocs\shop\public/uploads/products/book/thumb_Jellyfish.jpg)
When I comment 6,7,8 lines, the function works but it uploads only the original picture as it supposed to. If I remove the comment, the thumbnail works too!
So I guess, after the custom folder has been created, the whole function works fine, but before it has a writable problem.
Any ideas? Everything will be appreciated!
For anyone wonder how to fix this or do something similar, I just found the solution:
if($file = $request->hasFile('image')) {
$file = $request->file('image');
$extension = $file->getClientOriginalName();
$username = Auth::user()->username;
$thumb = Image::make($file->getRealPath())->resize(100, 100, function ($constraint) {
$constraint->aspectRatio(); //maintain image ratio
});
$destinationPath = public_path('/uploads/products/' . $username);
$file->move($destinationPath, $extension);
$thumb->save($destinationPath.'/thumb_'.$extension);
$product['imagePath'] = '/uploads/products/'. $username . '/' . $extension;
$product['thumbnail'] = '/uploads/products/'. $username . '/thumb_' . $extension;
}
So this piece of code makes a dynamic folder (I chose the username of the authenticated user) inside /uploads/products/. In that folder it uploads the picture and also creates a resized one, for thumbnail use. Also, when it creates the thumbnail, it holds the ratio of the original picture so it doesn't lose proportions