Laravel 5.4 Image Intervention uploads images on S3 with 0 Bytes - php

I have successfully created a Laravel 5.4 web application which is hosted on Heroku. I have also have managed that images gets uploaded on AWS S3. However the images gets uploaded as blank or empty. I have tried various online sources and have not been able to find a solution to my problem. Below I have attached my controller, which I believe is causing the error.
public function updateavatar(Request $request, User $user){
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
// $destinationPath = public_path('/uploads/avatars/');
$imageS3 = Image::make($avatar)->resize(300,300);
Storage::disk("s3")->put($filename, $imageS3->__toString());
//$disk->put("img/album/$id/$filename", $image->__toString());
$motcall = $this->carCheck($user);
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
An example how the issue looks like in my bucket

I have managed to resolve the issue by changign the storage line to Storage::disk("s3")->put($filename, file_get_contents($avatar)); P.S it ignores the image crop

encode worked for me:
for "s3"
Storage::disk('s3')->put('/downloads/' . $fileName, $interventionImage->encode(), 'public');
for local storage
Storage::disk('public')->put('/downloads/' . $fileName, $interventionImage->encode(), 'public');

Related

How to get Interventation/Image after Resize in Laravel

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

Laravel and Intervention Image getSize() error

I'm trying to integrate Intervention/Image into my laravel project to create a thumbnail upon uploading an image.
The image upload itself works fine, it doesn't seem to have any issue recognizing Intervention itself.
Below is the code block. The error seems to happen on the line with the save statement, I'm able to die and dump the contents of $img after it's set.
$file = $request->file('image');
$name = md5($file->getClientOriginalName() . time());
$extension = $file->getClientOriginalExtension();
$fileName = $name . '.' . $extension;
$file->move('./uploads/images/', $fileName);
$img = Image::make($file)->fit(300);
$img->save('/uploads/thumbnails/' . $name, 60, 'jpg');
This is the error I'm getting:
SplFileInfo::getSize(): stat failed for /private/var/folders/87/p5x7mgy914qg9ytf2zccc6q00000gn/T/php3lshFS
After some searching I've found that this could be related to file size upload limits, but I've altered my php.ini file (all of this is local btw) to accept 20MB files and the file I'm trying to upload is less than 100kb. I've also reset both php through homebrew and apache. Still getting the error.
Is there any glaringly obvious issues in my use of Intervention? I'll happily provide more info, this is in the store function in one of my controllers btw.
Untested, but I do it like this:
public function thumbnail(Request $request){
$thumbDir= storage_path('app/public').'/uploads/thumbnails/';
$file = $request->file('image');
$filename = md5($file->getClientOriginalName() . time()).'.jpg';
// $name = md5($file->getClientOriginalName() . time());
// $extension = $file->getClientOriginalExtension();
// $fileName = $name . '.' . $extension;
// $file->move('./uploads/images/', $fileName);
Image::make($file)->encode('jpg', 60)->fit(300, null, function ($c) {
$c->aspectRatio();
$c->upsize();
})->save($thumbDir . $filename);
return back()->with('success','The Image Has Been Added.');
}

Can't save files in my LARAVEL API folder with POSTMAN

I have a small problem when I try to upload an image from POSTMAN to my LARAVEL API, I am creating the path and name well, but when making the POST request the image is not saved in the supposed folder where it should be saved.
The route is for the creation of a product, and the image corresponds to it:
Route:
Route::post('product/new','ProductController#store');
In my controller:
$product = new Product;
$product->name = $request->input('name');
//Other staff
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename = $product->name . '.' . $extension;
$path = $file->move(public_path("/uploads/image/products/"), $filename);
$product->image = $path;
And the POSTMAN config:
Already try to configure the Content type headers with: x-www-form-urlencoded, with Content type multipart/form-data and without any Content type, and none of the 3 worked
The result I'm getting is:
{
"id": 1,
"name": "test1",
//staff
"image": "/app/public/uploads/image/products/test1.png",
}
But if I check there:/app/public/uploads/image/products/ there are no saved images
Thank you in advance :)
Use the store method on the file from the request instead and symlink the storage directory for the public
$extension = $file->getClientOriginalExtension();
$filename = $product->name . '.' . $extension;
$request->image->storeAs('uploads/image/products/', $filename);
And run php artisan storage:link

Laravel move() is not uploading images everytime

I use code below:
$image_name = 'image' . time() . '.' . $request->file('image')->getClientOriginalExtension();
$destinationFolder = public_path('images');
$request->file('image')->move($destinationFolder, $image_name);
but sometime its not working, images are not storing. Im using heroku as host.
Try to use Storage facade :
$path = \Storage::putFile('images', $request->file('image'));
Laravel will generate a name automatically, About Storage facade, just see this. :)
I made function for upload file and if there is no uploaded file do it again till its upload
$imageFile->move($destinationFolder, $fileName);
if(file_exists(public_path('images') . '/' . $fileName))
return public_path('images') . '/' . $fileName;
else
self::uploadImage($imageFile, $fileName);
I used the code and it's working fine.
self::$image = $request->file('image');
self::$imageName = self::$image->getClientOriginalName();
self::$directory = 'category-images/';
self::$image->move(self::$directory, self::$imageName);
return self::$directory.self::$imageName;

Laravel: Trouble with uploading files on localhost

I've trouble getting the file upload working with Laravel on my Windows 7 system. There is no issue with uploading the files but when I see the destination directory the uploaded file is not present.
After searching in Google and forums I found that there may be a problem with the 'Temp' directory.
The output of dd(sys_get_temp_dir()) is C:\Users\RAGHAV~1\AppData\Local\Temp.
However there is no directory called RAGHAV~1(I've enabled to see the hidden folders). In php.ini the upload_tmp_dir is set to C:\xampp\tmp.
Is there any conflict between these settings? Can you please help me to get the file upload working?
The code in the controller that process the uploaded files:
$validator = $this->brandValidator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$image_directory = public_path() . '/Uploads/Products/';
$result = $request->file('image')->move($image_directory);
$brand_name = $request->input('brand_name');
$image = $image_directory . $request->file('image')->getClientOriginalName();
$id = Brand::create([
'brand_name' => $brand_name,
'image' => $image,
]);
You have not specified the path for the file. Simply replace this
$image_directory = public_path() . '/Uploads/Products/';
$result = $request->file('image')->move($image_directory);
with this
$file = $request->file('image');
$filename = $file->getClientOriginalName().'.' . $file->getClientOriginalExtension();
$file->move(public_path('Uploads/Products/'), $filename);

Categories