My problem is that all fields are updated, except the image field.
This is my Controller to update the form:
public function update(Meretrice $meretrice, MeretriceRequest $request)
{
if ($request->hasFile('avatar')) {
$meretrice->avatar = $request->file('avatar');
$filename = $meretrice->avatar->getClientOriginalName();
$save_path = storage_path('app/public') . '/meretrices/id/' . $meretrice->id . '/uploads/images/';
$path = $save_path . $filename;
$public_path = '/images/meretrice/' . $meretrice->id . '/avatar/' . $filename;
File::makeDirectory($save_path, $mode = 0755, true, true);
Image::make($meretrice->avatar)->save($save_path . $filename);
$meretrice->avatar = $public_path;
$meretrice->tags()->sync($request->tags);
$meretrice->update($request->validated());
}
return redirect()->route('meretrice.show', $meretrice)->with('info', 'successfully');
}
I use the same code to create and it creates successfully, but to edit I have the problem that not only the image field is updated
Related
I am trying upload a single image into a Post and it keeps saving it as a private image--heck, I don't even know where to find that file.
PostController.php
...
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('public/posts/' . $request->user()->id, $filename);
}
...
storeAs() function takes three parameters.
storeAs(path,name,options)
'options' is where you specify file visibility. In your case :
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('posts/' . $request->user()->id, $filename,'public');
}
I am trying to upload the binary image to the storage using Laravel Intervention Image but it gives me error as Unable to init from given binary data.
I am using this code
$image = base64_decode($postData['image']);
$destinationPath = storage_path($destinationFolder);
if (!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0777, true, true);
}
$filename = ($fileName != '') ? $fileName : $folderName . '_' . time() . '.jpg';
$imageResult = Image::make($image)->resize($imageWidth, $imageHeight, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath . $filename, imageQuality($image));
if ($imageResult){
return '/image/' . $filename;
}
return false;
The binary image data is
data:image/webp;base64,UklGRlIFAABXRUJQVlA4IEYFAAAQHACdASpPAHkAPlEQlEojkdHMYDgFBLIAZqA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w34T+sQs9s0gOpG+tNM0uaKmwmONjo2L0tFqaiC4V4aHLO3JqptxoxSjn3BQVG5x/ga/7bz/hqeycRIoGrTuIqKAdCVVrYLEgr39NID+sKzK6BYzr8j9r3JMPZ3+2T09lEwe0u+S7B4wI3rlSz5L3DZspwtfkWcGtZpUCRqgOiWnFnpdIlor1+zvQK2ksDUpf0UbyRW/c59RQIfouYA6cKJUxsQzPPL5yvuIprzjZLY2HRkKFbemgUbPH1nxw28qtf2EixENC3uGus24PjYW8Jz/G5kZ2ioG2UXp00Aru2Z1Hk4AB0G2RPAzhdp1WQEnbqXb5a35eSSv5SGUYQbRDbaQNnyeODfb80a056/Cz7wmTtn5xvWT1UhTg7J/9J/4cCoBkweCOc1exDC4bBpdmwNyB82TfMOL+HcsO/UR1EDkEBdEl2HLQOA2mSntOQ3dTgMQX/71+MtpetptbThjL/fnRMMfVnpovAe2jd4SXx+s8fMNJXcEkO9ZKxIWFH0EwLvzIp8SPc5z+AzLEwfn4aBvVL835u4BsodI+usLpWnlC7Xdz6JMVxKivRpoWgrcIsAF91564sRuvXd3rwozLc0Fb/at7P3B3B5Jc7wPXQ/CH5Y6Thjn0W8zAWnd3VwwB3ZS/Uv/ubq8H7W9He6fvg6Ib/101yfwV8xpmLHwZGmXWgAAAAAAA=
In addition to using RAUSHAN KUMAR's answer, you can also use InterventionImage like this
Route::get('test', function() {
$image = 'data:image/webp;base64,UklGRlIFAABXRUJQVlA4IEYFAAAQHACdASpPAHkAPlEQlEojkdHMYDgFBLIAZqA25eJVGRUWLtSnk7iTzHecd0SXUx8+t+zLWt8WJpEWrTTvLe9y9CX9VSAw3YOVEdn4oGm0ZIrnQUIJ7VsI/r+aW0VOJeFoJylth8MmFQHlbPEklNUVbgyVJnINmgXnDbtgj9paOvkDYnVAdv2ErJONHxXyp2eyn7mB6vcVu9AfWuUtDlYxhQG1CEongtTYR0U4jQIbXYTTzN/3G5cAEd0FmVN272q9XHtEAeWzrMHHsZ7YPtpmppalPe8Bvhn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28qtf2EixENC3uGus24PjYW8Jz/G5kZ2ioG2UXp00Aru2Z1Hk4AB0G2RPAzhdp1WQEnbqXb5a35eSSv5SGUYQbRDbaQNnyeODfb80a056/Cz7wmTtn5xvWT1UhTg7J/9J/4cCoBkweCOc1exDC4bBpdmwNyB82TfMOL+HcsO/UR1EDkEBdEl2HLQOA2mSntOQ3dTgMQX/71+MtpetptbThjL/fnRMMfVnpovAe2jd4SXx+s8fMNJXcEkO9ZKxIWFH0EwLvzIp8SPc5z+AzLEwfn4aBvVL835u4BsodI+usLpWnlC7Xdz6JMVxKivRpoWgrcIsAF91564sRuvXd3rwozLc0Fb/at7P3B3B5Jc7wPXQ/CH5Y6Thjn0W8zAWnd3VwwB3ZS/Uv/ubq8H7W9He6fvg6Ib/101yfwV8xpmLHwZGmXWgAAAAAAA=';
$image = imagecreatefromwebp($image);
return Image::make($image)->resize(100)->response();
});
By calling the route 'test' you will see the image.
As this is a webp type image, so i need to use imagecreatefromwebp() to upload the images. I have written this piece of code for that.
$destinationFolder = 'uploads/';
$folderName = $folder . '_' . $adId;
if ($folderName != '') {
$folderNames = explode('_', $folderName);
$folderPath = implode('/', array_map(function ($value) {
return $value;
}, $folderNames));
$destinationFolder .= $folderPath . '/';
}
$destinationPath = storage_path($destinationFolder);
if (!\File::exists($destinationPath)) \File::makeDirectory($destinationPath, 0777, true, true);
$fileName = $folder . '_' . $adId . '_0_' . time() . '.jpg';
$fileName = ($fileName != '') ? $fileName : $folderName . '_' . time() . '.jpg';
$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);
if ($imageResult) return '/image/' . $fileName;
return "/DefaultImage.jpg";
If you still wants to use your code, you can remove data:image/webp;base64, then use base64_decode after you remove it.
$image=explode(",",$postData['image']);
$image=base64_decode($image['1']);
Image Intervention can decode your base64 image you can try this
$imageResult = Image::make($postData['image'])->resize($imageWidth, $imageHeight, function ($constraint) {
$constraint->aspectRatio();
I used to store my upload files (images) in my public folder but this time I want to store them in my storage folder and I get this error
Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)
Error comes from this line (where I use intervention/image package)
Image::make($image)->resize(1200, 600)->save($location);
My function:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
any idea?
UPDATE
My files will upload in root/storage folder instead of root/storage/app/public/images
why is that?
Update 2
I changed my function to code below and it's uploading where it suppose to upload but it does not delete the old image
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
SOLVED
here is final code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename); // root storage path
Image::make($image)->resize(1200, 600)->save($location);
Storage::delete('images/' . $indexabout->image); //public storage path
$indexabout->image = $filename;
}
Basically I gave Root/Storage path to store data and Public/Storage path for deleting them in update method.
When uploading my image it is saved to D:\xampp\tmp\phpD0E0.tmp directory. But I want to save it in public/uploads/banner. Anyone please help me. Here is my code:
BannersController.php
public function store(Request $request)
{
$requestData = $request->all();
if ($request->hasFile('banner_image')) {
foreach($request['banner_image'] as $file){
$uploadPath = public_path('/uploads/banner');
$extension = $file->getClientOriginalExtension();
$fileName = rand(11111, 99999) . '.' . $extension;
$file->move($uploadPath, $fileName);
$requestData['banner_image'] = $fileName;
}
}
Banner::create($requestData);
Session::flash('flash_message', 'Banner added!');
return redirect('Banner/banners');
}
Can you please revert that path to 'root' => storage_path('app/public')
and just try with change line:
$extension = $file->getClientOriginalExtension();
$fileName = rand(11111, 99999) . '.' . $extension;
with this:
$fileName = rand(11111, 99999) . '.' . $file->getClientOriginalName();
Hope this helps you.
I'm working with laravel 5.4 I have a form which I can upload my logoimage and in update function I have this code:
//Save logo
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$general_Settings->logo = $filename;
Storage::delete($oldFilename);
}
$general_Settings->save();
for updating my image which is work but as you see I have Storage::delete($oldFilename); this part doesn't work and just keep the old image.
what do you think is issue of that?
Solved:
The issue was Filesystem.php I made my local root set to 'root' => public_path('avatars/'), and changed all my functions in my app because no way to save images in sub-folders and delete them just can save in sub-folders.
then my update function become like this:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = 'sitelogo' . '-' . time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/');
$request->file('logo')->move($location, $filename);
$general_Settings->logo = $filename;
}
$general_Settings->save();
I hope this help someone.
Since, You have stored logo file name only not full path of file,
You need to give full path from public folder to delete image. Change delete line as:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$file_path = "avatars/logos/".$oldFilename;
$general_Settings->logo = $filename;
Storage::delete($file_path);
}
$general_Settings->save();
This might work.