Using Laravel 7, I'm tring to setup a form where a user can edit their details and their image. While trying to upload a PNG or JPG image, I get the following Validation Error:
The image must be an image.
If I try to upload image, I get this error
Call to a member function isvalid() on boolean
And If I remove the validation for image, I get a FatalErrorException:
Call to a member function getClientOriginalExtension() on boolean
which is a helper function provided by Intervention/Image library. This could mean that the image didn't upload at all.
This is my Blade file
1. form role="form" id="updateDetailsForm" method="post" action="{{url('/admin/update-admin-details')}}" enctype="multipart/form-data">
<div class="card-body">
<div class="form-group" >
<label for="exampleInputEmail1">Admin Name</label>
<input type="text" class="form-control" name="admin_name" id="name" value="{{Auth::guard('admin')->user()->name}}" placeholder="Enter Email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Type</label>
<input type="text" class="form-control" name="type" id="type" readonly value="{{Auth::guard('admin')->user()->type}}" placeholder="Enter Email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email" id="email" readonly value="{{Auth::guard('admin')->user()->email}}" placeholder="Enter Email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Mobile</label>
<input type="text" class="form-control" name="mobile" id="mobile" value="{{Auth::guard('admin')->user()->mobile}}" placeholder="Enter Email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Image</label>
<input type="file" class="form-control" name="admin_image" id="admin_image" accept="image/*" >
#if(!empty(Auth::guard('admin')->user()->image))
<a target="_blank" href="{{url('/images/admin_images/admin_photos/'.Auth::guard('admin')->user()->image)}}">View Image</a>
<input type="hidden" name="current_admin_image" value="{{Auth::guard('admin')->user()->image}}">
#endif
</div> ```
This is my Controller
```ADMIN CONTROLLER
public function updateAdminDetails(Request $request){
if($request->isMethod('post')){
$data= $request->all();
//echo "<pre>";print_r($data);die;
//Creating Validation
$rules =[
'admin_name'=>'required|string',
'mobile'=> 'required|numeric',
'admin_image'=> 'required|image|file',
];
$customMessage=[
'admin_name.required'=>'Name is Required',
'admin_name.alpha' => 'Valid Name is required',
'mobile.required'=>'Mobile is Required',
'mobile.numeric'=>'Valid Mobile is Required',
'admin_image.required'=>'Image is Required',
'admin_image.image'=>'Valid Image is Required',
];
$this->validate($request, $rules, $customMessage);
//Uploading Admin Image
// $imageName= '';
if($request->hasFile('admin_image')){
$image_tmp = $request->hasFile('admin_image');
if($image_tmp->isValid()){
//get extension
$extension = $image_tmp->getClientOriginalExtension();
//Generating image name
$imageName= rand(111,999999).'.'.$extension;
//Image Path
$imagePath= 'images/admin_images/admin_photos/'.$imageName;
//Uploading the image
Image::make($image_tmp)->save($imagePath);
}
} else{
$imageName="";
//return false;
}
$result=Admin::where('email', Auth::guard('admin')->user()->email)
->update(['name'=>$data['admin_name'], 'mobile'=>$data['mobile'], 'image'=>$imageName]);
//echo "<pre>";print_r($result);die;
//Session::flash('success_message', 'Details updated Successfully');
return redirect()->back();
}
return view('admin.admin_details');
}
Thanks, bosses
Related
I am currently saving some images in shopify via File API:
mutation fileCreate($files: [FileCreateInput!]!) {
fileCreate(files: $files) {
files {
alt
}
userErrors {
field
message
}
}
}',
[
'files' => [
'alt' => $alt_text,
'contentType' => 'IMAGE',
'originalSource' => $from_url,
]
]
It works, but unfortunately I am not able to get the url in return. It returns null as if it's still processing.
Is there any way to do it? I am saving the image name locally, but I wouldn't consider querying the api again for each single one a valid solution (too many files, huge overhead)
Thanks in advance
I have tried above code on my project and it works but i didn't get any return url but get the following response object.
[
{
alt: 'pexels-albina-white-11780519.jpg',
createdAt: '2022-07-12T06:07:59Z',
fileErrors: [],
fileStatus: 'UPLOADED',
__typename: 'GenericFile',
preview: { image: null }
}
]
Shopify didn't provide the reading files from admin file not from any api for public apps.If any idea to read file from the api admin file then Please answer for this Query.
Given the Nova class
class Article extends Resource
{
// [...]
public function fields(Request $request)
{
return [
ID::make()
->sortable(),
Image::make('Image', 'filename')
->disk('public')
->path('articles')
->prunable(),
];
}
// [...]
}
Why do I get this error when I upload an image?
SQLSTATE[HY000]: General error: 1364 Field 'filename' doesn't have a default value (SQL: insert into `article` (`id`, `updated_at`, `created_at`) values (143, 2021-06-07 12:35:36, 2021-06-07 12:35:36))
Some images can be uploaded, others don't. It's not due to their filetype, as some .png works but other don't.
This error message is misleading. The problem is the file size: when it's bigger than 3MB, it will throw this error.
I suggest to add a help message on the field itself until Laravel Nova use a clearer message, like so:
Image::make('Image', 'filename')
->disk('public')
->path('teacher_images/source')
->prunable()
->help('Size: 3MB maximum')
this is my code to upload image
$image=$request->file['image'];
if(($image)){
$image_name=hexdec(uniqid());
$ext=strtolower($image->getClientOriginalExtention());
$image_full_name=$image_name.'.'.$ext;
$upload_path='public/image/';
$image_url=$upload_path.$image_full_name;
$success=$image->move($upload_path,$image_full_name);
$data['image']=$image_url;
and then send to database
DB::table('posts')->insert($data);
return redirect()->back();
}
you should use file method not as get as array so you should write :
$image=$request->file('image');
or retrive file from FileBag array :
$image=$request->files['image'];
I have more than 8000 images in my laravel project and most of these images are in different type distentions. So what I trying to do is check and dimensions of images via getimagesize() but when i use this function in laravel controller it dosen't output anything . Please refer the code that I have tried. When I dd() the image url its working but when I pass the url to the function it just exceed the execution time and nothing happen. But if I pass a different url to the method (something almost hosted) It output the result I wants. I have gone through below question and Yes I feel that there is something with the path of the image. So could anyone please help me to solve this.
<?php
namespace App\Http\Controllers;
use App\Product;
use function asset ;
use Illuminate\Http\Request;
use ParagonIE\Sodium\File;
class testingController extends Controller
{
public function testing (){
//fetching all the products from the DB
$products = Product::all();
//looping the products and generating image path
foreach ($products as $product) {
//image path
$img = asset ( 'uploads/products_thumbnails').'/'.$product->thumbnail_file_name;
// dd($img); //this is working
//http://localhost:8000/products/1234568.jpg
$check = getimagesize ( $img);
dd($check); //this dosen't output anything
// if ( File::exists($img)){
// }
}
}
}
But if I trying something like below its working
$check = getimagesize ( 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');
dd($check);
array:6 [▼
0 => 272
1 => 92
2 => 3
3 => "width="272" height="92""
"bits" => 8
"mime" => "image/png"
]
Please note that I have almost gone through below questions.
PHP getimagesize() not working
How i get width and height of a image in Laravel Framework?
Problem is with this function
$img = asset ( 'uploads/products_thumbnails').'/'.$product->thumbnail_file_name;
Try accessing your public folder without asset function.
$img = 'uploads/products_thumbnails/'.$product->thumbnail_file_name;
In my form I have used below code:
<?php echo Html::fileInput('imageFile', '', array('id' => 'contentForm_imageFile' )) ?>
and I tried to get the file in the controller with below code:
$newsModel->imageFile=UploadedFile::getInstance(Yii::$app->request->post('imageFile'), 'imageFile');
$newsModel->imageFile->saveAs( 'uploads/'.$newsModel->file->basename.'.'.$newsModel->file->extension );
But I am getting this error when using above code to save the file:
Call to a member function saveAs() on null
since the API document of yii\web\UploadedFile says that:
getInstance() Returns an uploaded file for the given model attribute,
The file should be uploaded using
yii\widgets\ActiveField::fileInput().
So how can I get the uploaded file which is uploaded through yii\helpers\Html::fileInput?
You can get uploaded file without model using getInstanceByName()
$newsModel->imageFile = UploadedFile::getInstanceByName('imageFile');
$newsModel->imageFile->saveAs( 'uploads/'.$newsModel->file->basename.'.'.$newsModel->file->extension );
Here is an official example for your situation: http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html
Please double check your code:
First of all is there an enctype="multipart/form-data" in your form tag?
Next, see http://www.yiiframework.com/doc-2.0/yii-web-uploadedfile.html#getInstance()-detail for parameters of getInstance. It requires model instance as first parameter and field name as second parameter.
Check that your server receives
file that you send. Just var_dump($_FILES);
To form add 'enctype' => 'multipart/form-data'
In view:
<?= $form->field($model, 'imageFile')->fileInput(['id' => 'contentForm_imageFile']) ?>
Or
<?= Html::activeFileInput($model, 'imageFile', ['id' => 'contentForm_imageFile']) ?>
In Controller
$newsModel->imageFile = UploadedFile::getInstance($newsModel, 'imageFile');
$newsModel->imageFile->saveAs( 'uploads/'.$newsModel->file->basename.'.'.$newsModel->file->extension );