Image source not readable on my localhost - php

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

Related

Having trouble using intervention/image in laravel-9

I tried to resize my image before uploading but is not working but I am able to upload the image, Here is my Controller:
public function store(Request $request)
{
$validatedData = $request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:20480',
]);
$imageFile = $request->file('image');
$name = $imageFile->getClientOriginalName();
$path = $request->file('image')->store('images', 'public');
$resize = Image::make($imageFile)->resize(50, 50)->stream();
$save = new Gallery;
$save->image = $path;
$save->status = 1;
$save->user_id = Auth::id();
$save->save();
return redirect('gallery')->with('success', 'Image has been uploaded')->with('image',$name);
}
try below example source code. I have done image resize functionality in one of my project with Laravel version 8
<?php
if($request->hasFile('dealimg')){
$file = $request->File('dealimg');
$original_name = $file->getClientOriginalName();
$file_ext = $file->getClientOriginalExtension();
$destinationPath = 'uploads/deals';
$file_name = "deal".time().uniqid().".".$file_ext;
// $path = $request->deal_img->store('uploads');
$resize_image = Image::make($file->getRealPath()); //for Resize the Image
$resize_image->resize(150, 150, function($constraint){ //resize with 150 x 150 ratio
$constraint->aspectRatio();
})->save(public_path($destinationPath) . '/' . $file_name);
$deal_img = $destinationPath."/".$file_name;
$request->request->add(['deal_img' => $deal_img]);
}
?>
I hope this one helps to you... see this

Failed to rename image and upload using storeAs in Laravel

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

laravel project works on localhost but not working on gcloud after deployment

I have a laravel project that works on gcloud , But image uploading function shows an error and the same function works perfectly on localhost .
please help me to fix it , thanks in advance
$gallery = new Gallery;
$filename = $image->getClientOriginalName();
$org_path = '../public/images/gallery/originals/' . $filename;
$thm_path = '../public/images/gallery/thumbnails/' . $filename;
$gallery->image = 'images/gallery/originals/'.$filename;
$gallery->thumbnail = 'images/gallery/thumbnails/'.$filename;
$gallery->title = $request->title;
$gallery->status = $request->status;
if ( ! $gallery->save()) {
flash('Gallery could not be updated.')->error()->important();
return redirect()->back()->withInput();
}
if (($org_img && $thm_img) == true) {
Image::make($image)->fit(900, 500, function ($constraint) {
$constraint->upsize();
})->save($org_path);
Image::make($image)->fit(270, 160, function ($constraint) {
$constraint->upsize();
})->save($thm_path);
}
enter image description here
Instead of using relative path give the actual path. this will solve the problem.

Laravel Intervention Unable to init from given binary data for WEBP type files

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+aW0VOJeFoJylth8MmFQHlbPEklNUVbgyVJnINmgXnDbtgj9paOvkDYnVAdv2ErJONHxXyp2eyn7mB6vcVu9AfWuUtDlYxhQG1CEongtTYR0U4jQIbXYTTzN/3G5cAEd0FmVN272q9XHtEAeWzrMHHsZ7YPtpmppalPe8BvhnrwSA+ctaG9iQ6b7pEVBVPAAD+/uy97OjEKfy53WiZp+vshoaLbmP0cVKb4k6hnKsxJdcP+CgaRYQkOjb4FGLJO55Q/c+afu5UzMOW3Tx4pq6YezZD8PSoPz4zk6GAAEL9XDf3c3RwQKG1r2lWKnBonjqCV8/oU/xr4Gv59yLHfFPfneUb6BrG9yoc40NAk+xGkgtKGsIDDanX+uuhaKWGntbgweNVylzqaIqZrCMYGgfkbTo+yPQ0JgHev/+hCnqRe4cEi4VfveeAi+7wBLg2w4tZOj0d7O7gJM6Zj9uaLB6l/3xyvdHwzThmi8na5GMB/v+Y7YAIYCGOV62mQ6XSrBMQHKUoPvIVwUeHVkJFWnUCt6S7yOMa9RkZxe8//Bphx4NhJ/dXc3x7HQESKmLUu8nofAJKiyg7v46s90BuZWpbVYysGbdPR9Shc9nqgYoEazEu+ik00Mr+VLM+/lS8aCumf4on0FkZ/Dn4SGJSU8pc02nt7ncW0e0XwVKx9DE8RfVww8GDv33+1ib7qkv1gsaGBdn60MpW2PzyI1ZDReCh25f4z4RsG91nEpjDr4MmVOaW40nXwNnAfuYawSt+b05IQx9GGw0seGFDJ4hbb+tTatszMiOSjhQ9HsO19t/hVFojVco/cKoG9XUSorPouBOqFqDrciO3+BVFFo5l5JW3Ka0ZtamSCrzt1AUzOndTy82imvJ+NZ1D+iXF92d3XITYsveniLVxEjp+pQIp8pXJ3p3DuFQPxzuQ44E3xWcPimJ7wuJnIrIm8jyFaM4AHJ1OBc/BG+0iP5zUHWl36LGK5VpoDkw34T+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,UklGRl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WcPimJ7wuJnIrIm8jyFaM4AHJ1OBc/BG+0iP5zUHWl36LGK5VpoDkw34T+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=';
$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();

How to save uploaded image to Storage in laravel?

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

Categories