I am using the following code to validate the dimensions of the image in Laravel 5.8 (link to following code):
'profile_image' => 'required|dimensions:width=300,height=500'
but I am getting the error eventhough I attached exact 300x500 image, look at the following code:
list($width, $height) = getimagesize($request->profile_image);
$messages = [
'profile_image.dimensions' => "Profile image must be 300x500 resolution (current is {$width}x{$height})."
];
$request->validate([
'profile_image' => 'required|dimensions:width=300,height=500',
], $messages);
I am getting this "Profile image must be 300x500 resolution (current is 300x500)".
I also tried min_width=300,min_height=500 and max_width=300,max_height=500 but not working.
Image is in base64 format (I think it doesn't matter)
Any suggestions?
This works for me:
'profile_image' => [
'required',
Rule::dimensions()->width(300)->height(500)
]
Related
Is there any way to check if the image code inside it is valid because
some crackers(hackers) can change the extension and play with image info using some applications to trick the website that the type is image while it's not
so is there any way to check if content is an image code ?
Or is there anyway to check against this type of attacks ?
For Multiple Images
$validatedData = $request->validate([
'images' => 'required',
'images.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
Single Image Validation
$validatedData = $request->validate([
'image' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
In my form, I asked for an image to upload. Then I already validated it and it works. But the file is not stored in the uploads folder.
Here's a snippet of my ProductController:
public function store(Request $request)
{
// Validate fields
$this->validate($request, [
'product_name' => 'required',
'product_price' => 'required',
'product_desc' => 'required',
'product_img' => 'image|required'
]);
// Upload image
if($request->hasFile('image')) {
app()->make('path.public/uploads');
$image = $request->image;
$image->move('uploads', $image->getClientOriginalName());
}
/*// Save the data into database
Product::create([
'name' => $request->product_name,
'price' => $request->product_price,
'description' => $request->product_desc,
'image' => $request->image->getClientOriginalName()
]);
// Echo a session message
$request->session()->flash('msg', 'Your product has been added');
// Redirect to view page
return redirect('/products');*/
}
I already tried looking at other possible solutions but the other questions were already at storing the image in the database. I also tried checking if uploads was a directory and existed, and it is.
Can anyone please help? Thanks.
Try this: official documentation: This is how it should look in your controller:
if($request->hasFile('image')) {
$request->file('image')->store('uplodads/', 'public');
}
This would store the image in /storage/app/public/uploads by default. You can also change the public path in /config/filesystems.php. You can then access the file (if you linked the storage) with asset('storage/uploads'.$img_name).
app()->make('path.public/uploads');
I've already solved it. The variable was wrong all along. Instead of it being product_img, I placed image.
Here's the updated code:
// Validate fields
$this->validate($request, [
'product_name' => 'required',
'product_price' => 'required',
'product_desc' => 'required',
'product_img' => 'image|required'
]);
// Upload image
if($request->hasFile('product_img')) {
$image = $request->product_img;
$image->move('uploads', $image->getClientOriginalName());
}
I have used validation for image in Laravel Voyager BREAD. But it doesn't work. It takes all dimension of images.
Here is my code
{
"validation": {
"rule": "dimensions:width=100,height=100",
"messages": {
"dimensions": "This :attribute field is a must."
}
}
}
Image for more clarification:
You can try a creating a rule since this requires several arguments
use Illuminate\Validation\Rule;
Validator::make($data, [
'image' => [
'required',
Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3 / 2),
],
]);
From the docs you need to add mimes rule, and use dimensions rule like:
'rule' => 'mimes:jpeg,bmp,png|dimensions:width=340,height=960'
I am working on a Laravel 5.5 project which is giving me some issues... One of them is that when I upload some image it returns me an error: "The file must be an image" with the following code in the controller.`
public function update(\App\Property $property, Request $request)
{
$rules = [
'images.*' => 'mimes:image|max:2048',
'zone' => 'required'
];
$messages = [
'images.mimes' => 'The file must be an image', // in my file that is a translated of the original message
'images.max' => 'The images sizes must be under 2MB',
];
$validator = Validator::make($request->all(), $rules, $messages);
if($validator->fails()) {
parent::message(
$validator->errors(),
'¡Error!',
'error'
);
return back()->withInput();
}
Just in case:
I was reading some people who had the same issue but in my case I have the usual <form method="POST action="..." enctype="multipart/form-data">
I made a dd($request->files) on the request but the all the images I upload seems to be images.
Also I've tried to use the iteration method as following:
if($request->hasFile('images')) {
foreach ($images as $image)
{
$validator = Validator::make(
[ 'image' => $image ],
[ 'image' => 'mimes:jpeg,png,jpg,gif,svg|max:2048' ],
[
'image.mimes' => 'Images must have format (JPEG, PNG, JPG, GIF or SVG)',
'image.max' => 'Each image must be under 2MB'
]);
if($validator->fails()) {
parent::message(
$validator->errors(),
'¡Error!',
'error'
);
return back()->withInput();
}
}
}
But when the images are too bigger than 2MB the request is not even passing through if($request->hasFile() function.
And I want a generic error for all the images and not validate every image, is that possible? By the way in the Laravel Documentation it hasn't the previous iterating method.
I was trying to figure this out as well and it seems symfony has been updated since Laravel 5.5. On Laravel 8 I can tell you that the following combination of settings fixed the error I had yielding the same error type you had:
In your form:
make sure your form has
<form method="POST action="..." enctype="multipart/form-data">
In your validator:
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
In the event that you are using Laravel Collectives for your form, then you can implement the enctype as show below :
{!! Form::open([‘action’ =>’ProfilesController#update’,
‘method’ => ‘POST’, ‘enctype’ => ‘multipart/form-data’]) !!}
I am working on a Laravel app which allows users to upload mp3 files which are stored in Cloudinary. Locally, uploading the files works fine but in Heroku I get a validation error that the file is missing. I can't really see why I am getting the validation error. I get this error when I try uploading a file that's still within the PHP max file upload limit i.e. 8.6MB. However, if I try uploading a file greater than 8.6 MB I get a Service Unavailable 503 error with -
The server is temporarily unable to service your request due to
maintenance downtime or capacity problems. Please try again later. What could be the problem?
This is the code to the store method in my Controller
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|min:3',
'description' => 'required',
'channel' => 'required',
'podcast' => 'required|size_format'
]);
$data = [
'episode_name' => $request->title,
'episode_description' => $request->description,
'channel_id' => $request->channel,
'image' => self::DEFUALT_COVER_IMAGE,
'audio_mp3' => $this->upload->videoToCloudinary($request->podcast),
'view_count' => 0,
'status' => 0
];
$this->episodeRepository->createEpisode($data);
return redirect()->back()->with('status', 'Nice Job! ' . $request->title . ' is held for moderation.');
}