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);
}
}
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 want to after uploading an image save it like this
images/blogs/1650953308.jpg
But I see this
1650953308.jpg
I want to save like this
images/blogs/1650953308.jpg
public function store(Request $request)
{
$fileNameService = $request->file('image') ?? null;
if ($request->hasFile('image'))
{
$file = $request->file('image');
$name = time();
$extension = $file->getClientOriginalExtension();
$fileNameService = $name . '.' . $extension;
$img = Image::make($file->path());
$img->resize(600, 300, function ($constraint) {
$constraint->aspectRatio();
})->save('storage/images/blogs/'.$fileNameService);
}
Blog::query()->create([
'image' => $fileNameService,
]);
return redirect()->route('admin.blogs.index');
}
Concatenate folder path with image name.
Blog::query()->create([
'image' => 'images/blogs/'.$fileNameService,
]);
It will save as images/blogs/1650953308.jpg.
Why does not this work?
I want to change the size and save it in the storage folder.
public function store(Request $request)
{
$image = $request->file('image') ?? null;
if ($request->hasFile('image'))
{
$file = $request->file('image');
$name = time();
$extension = $file->getClientOriginalExtension();
$fileName = $name . '.' . $extension;
$imageResize = Image::make($file)->resize(600, 300)->save('images/blogs/'.$fileName);
$image = $file->storeAs('',$imageResize, 'public');
}
Blog::query()->create([
'image' => $image,
]);
return redirect()->route('admin.blogs.index');
}
I see this error
Images must be saved in this way
Please try this and if you want add time to the name of your image simply add it to the input image name.
About permission if you are an a production server please set storage directory permission to the 755 but if you are on a local windows machine check if your storage directory has not read-only attribute.
public function store(Request $request)
{
$image = $request->file('image') ?? null;
if ($request->hasFile('image'))
{
$image = $request->file('image');
$input['imagename'] = $request->file('image')->getClientOriginalName();
$destinationPath = public_path('storage/images/blogs/resized');
$img = Image::make($image->path());
$img->resize(600, 300, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath.'/'.$input['imagename']);
$destinationPath = public_path('storage/images/blogs/fullSize');
$image->move($destinationPath, $input['imagename']);
}
Blog::query()->create([
'image' => $image,
]);
return redirect()->route('admin.blogs.index')->withSuccess('Image successfully uploaded!');
}
I created a form to store article with an image , and generate a resized version as thumbnail from it.
I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error
This is the image upload handler function in my controller :
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug;
$successUploaded = $image->storeAs('img/articles-images', $fileName);
if($successUploaded) {
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make('img/articles-images' . '/' . $fileName)
->resize($width, $height)
->save('img/articles-images' . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
storeAs() method, which receives the path, the file name, and the (optional) disk as its arguments :
$successUploaded = $request->file('image')->storeAs(
'images', $fileName
);
I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug.'.' .$image->getClientOriginalExtension();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
// //
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,UklGRlIFAABXRUJQVlA4IEYFAAAQHACdASpPAHkAPlEQlEojkdHMYDgFBLIAZqA25eJVGRUWLtSnk7iTzHecd0SXUx8+t+zLWt8WJpEWrTTvLe9y9CX9VSAw3YOVEdn4oGm0ZIrnQUIJ7VsI/r+aW0VOJeFoJylth8MmFQHlbPEklNUVbgyVJnIN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3+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/3G5cAEd0FmVN272q9XHtEAeWzrMHHsZ7YPtpmppalPe8BvhnrwSA+ctaG9iQ6b7pEVBVPAAD+/uy97OjEKfy53WiZp+vshoaLbmP0cVKb4k6hnKsxJdcP+CgaRYQkOjb4FGLJO55Q/c+afu5UzMOW3Tx4pq6YezZD8PSoPz4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g7J/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();