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);
Related
When I try to upload images in dropzone from my localhost then I get ERROR! but on server I can upload this successful.This problem made me difficult to test product.
and this is my code
public function storeMedia(Request $request)
{
//resize image
$path = storage_path('tmp/uploads');
$imgwidth = 1000;
$imgheight = 1000;
if (!file_exists($path)) {
mkdir($path, 775, true);
}
$file = $request->file('file');
$name = uniqid() . '_' . trim($file->getClientOriginalName());
$full_path = storage_path('tmp/uploads/' . $name);
$img = \Image::make($file->getRealPath());
if ($img->width() > $imgwidth || $img->height() > $imgheight) {
$img->resize($imgwidth, null, function ($constraint) {
$constraint->aspectRatio();
});
}
$img->save($full_path);
}
I think maybe about permission but i'm not sure. I really don't know to solve this problem please help me
I'm working with laravel 7 and using intervention/image to store images. However, I want to encode and store images as webp, I'm using the following code but it is not encoding the image in webp rather it is storing in the original format. Can you please tell me what I'm doing wrong?
public function storePoster(Request $request, Tournament $tournament)
{
if ($request->hasFile('poster')) {
$tournament->update([
'poster' => $request->poster->store('images', ['disk' => 'public_uploads']),
]);
$image = Image::make(public_path('uploads/' . $tournament->poster))->encode('webp', 90)->resize(200, 250);
$image->save();
}
}
Try this :
public function storePoster(Request $request, Tournament $tournament)
{
if ($request->hasFile('poster')) {
$tournament->update([
'poster' => $request->poster->store('images', ['disk' => 'public_uploads']),
]);
$classifiedImg = $request->file('poster');
$filename = $classifiedImg->getClientOriginalExtension();
// Intervention
$image = Image::make($classifiedImg)->encode('webp', 90)->resize(200, 250)->save(public_path('uploads/' . $filename . '.webp')
}
}
This is my code to convert to .webp and resize (keep image's ratio)
$imageResize = Image::make($image)->encode('webp', 90);
if ($imageResize->width() > 380){
$imageResize->resize(380, null, function ($constraint) {
$constraint->aspectRatio();
});
}
$destinationPath = public_path('/imgs/covers/');
$imageResize->save($destinationPath.$name);
if you want to convert image in to WEBP without any service or package, try this method. work for me. have any question can ask. Thankyou
$post = $request->all();
$file = #$post['file'];
$code = 200;
$extension = $file->getClientOriginalExtension();
$imageName = $file->getClientOriginalName();
$path = 'your_path';
if(in_array($extension,["jpeg","jpg","png"])){
//old image
$webp = public_path().'/'.$path.'/'.$imageName;
$im = imagecreatefromstring(file_get_contents($webp));
imagepalettetotruecolor($im);
// have exact value with WEBP extension
$new_webp = preg_replace('"\.(jpg|jpeg|png|webp)$"', '.webp', $webp);
//del old image
unlink($webp);
// set qualityy according to requirement
return imagewebp($im, $new_webp, 50);
}
i want to resize uploaded image and store in folder.then show in web.
i used enctype="multipart/form-data" on form in blade.php.
file successfully show in web without resize.
when try to resize image i got error
controller.php
public function dili(Request $request)
{
$di = new diligent;
$di->jobtype = $request->jobtype;
$di->jobC = $request->jobC;
$di->details = $request->details;
$image = $request->file('image');
$path = $image->getClientOriginalName();
$destinationPath = public_path('img');
Image::make($image)->resize(300, 100)->save($image);
$a = $image->move($destinationPath, $path);
$di->image = $path;
$di->save();
$de = diligent::all();
return view('admin')->with('dw', $de);
}
Error Message
Encoding format (tmp) is not supported.
1) use getRealPath() inside Image::make()
2) save image in particular path. try like this.
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 100);
$image_resize->save(public_path('img/' .$filename));
}
Make sure you installed Image intervention library.
The Intervention image save() method requires a filename so it knows what file format (jpg, png, etc..) to save your image in.
The reason you are getting the error is it does not know what encoding to save the temporary image object (tmp) in.
Here is an example
->save('my-image.jpg', 90)
There is also a optional second parameter that controls the quality output. The above outputs at 90% quality.
http://image.intervention.io/api/save
I am using Image intervention to save an image to the storage folder. I have the code below and it seems to just save a file name with a blank image. I think I need a way for the file contents to be written to the folder but struggling for the snippet.
if ($request->hasFile('photo')) {
$image = $request->file('photo');
$fileName = time() . '.' . $image->getClientOriginalExtension();
$img = Image::make($image->getRealPath());
$img->resize(120, 120, function ($constraint) {
$constraint->aspectRatio();
});
//dd();
Storage::disk('local')->put('images/1/smalls'.'/'.$fileName, $img, 'public');
You need to do
if ($request->hasFile('photo')) {
$image = $request->file('photo');
$fileName = time() . '.' . $image->getClientOriginalExtension();
$img = Image::make($image->getRealPath());
$img->resize(120, 120, function ($constraint) {
$constraint->aspectRatio();
});
$img->stream(); // <-- Key point
//dd();
Storage::disk('local')->put('images/1/smalls'.'/'.$fileName, $img, 'public');
}
if ($request->hasFile('photo')) {
// $path = Storage::disk('local')->put($request->file('photo')->getClientOriginalName(),$request->file('photo')->get());
$path = $request->file('photo')->store('/images/1/smalls');
$product->image_url = $path;
}
Simple Code.
if($request->hasFile('image')){
$object->image = $request->image->store('your_path/image');
}
Thanks.
Here is another way to save images using intervention package on storage path with desired name. (using Storage::putFileAs method )
public function store(Request $request)
{
if ($request->hasFile('photo')) {
$image = $request->file('photo');
$image_name = time() . '.' . $image->extension();
$image = Image::make($request->file('photo'))
->resize(120, 120, function ($constraint) {
$constraint->aspectRatio();
});
//here you can define any directory name whatever you want, if dir is not exist it will created automatically.
Storage::putFileAs('public/images/1/smalls/' . $image_name, (string)$image->encode('png', 95), $image_name);
}
}
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