Image dimensions validation before sending
-
I have created a form with a file upload field for images.
I need to make sure uploaded images are of a certain min. size. Ex. 4000×3000 pixels.
How can you validate the size?
-
You can do it by implementing your own custom validation filter.
I tried with this code, but it seems to stop the form from uploading.
add_filter('wpcf7_validate_file*', 'cf7_pre_validate_image_strict', 20, 2);
function cf7_pre_validate_image_strict($result, $tag) {
if ($tag->name !== 'catch_image') {
return $result;
}
if (empty($_FILES['catch_image']['tmp_name'])) {
$result->invalidate($tag, "No file uploaded.");
return $result;
}
$file = $_FILES['catch_image']['tmp_name'];
if (!is_uploaded_file($file)) {
$result->invalidate($tag, "Upload failed. Please try again.");
return $result;
}
// sikker læsning (vigtig: undgå getimagesize crash)
$info = @getimagesize($file);
if ($info === false || empty($info[0]) || empty($info[1])) {
$result->invalidate($tag, "Invalid image file.");
return $result;
}
$width = (int) $info[0];
$height = (int) $info[1];
if ($width < 4000 || $height < 3000) {
$result->invalidate(
$tag,
"Image too small. Minimum 4000x3000px required."
);
}
return $result;
}Where can we see the form in question?
You can see the basic test form here:
https://specimenbaits.com/add-catch-report/
I have disabled my validation code.
This is the form field:
[file* catch_image filetypes:jpg|jpeg|heic limit:20mb]
Right now the form is set up to email and it works fine without the validation.
I will set it up to store the results and attachments in the database and send notification emails without attachment.-
This reply was modified 2 weeks, 1 day ago by
henrik.arvedsen.
-
This reply was modified 2 weeks, 1 day ago by
henrik.arvedsen.
Can you share the whole content in the Form tab panel?
<label> Your name
[text* your-name autocomplete:name] </label>
<label> Your email
[email* your-email autocomplete:email] </label>
<label> Catch date
[date* date-823] </label>
<label> Hookbait used
[text* hookbait-used] </label>
<label> Baits used
[text* baits-used] </label>
<label> Weight
[text* weight] </label>
<label> Report
[textarea* catch-report] </label>
<label Image
[file* catch_image filetypes:jpg|jpeg|heic limit:20mb]
<label>
[submit "Submit"] </label>Look carefully at these lines:
<label Image
[file* catch_image filetypes:jpg|jpeg|heic limit:20mb]Off course – I see teo errors there:
This is the right code:<label> Image
[file* catch_image filetypes:jpg|jpeg|heic limit:20mb]</label>-
This reply was modified 1 week, 6 days ago by
henrik.arvedsen.
But it still doesn’t validate the image. It just prevent it from uploading now.
Do you get any response message after you submit the form?
Yes I get this error message both when trying to send a smaller and bigger image.
“Upload failed. Please try again.”That is an error message that your custom validation raises. Why don’t you recheck around it?
$file = $_FILES['catch_image']['tmp_name']; if (!is_uploaded_file($file)) { $result->invalidate($tag, "Upload failed. Please try again."); return $result; }I have tried. But don’t see the error or any solutions.
Is that part necessary? Why did you add it?
I got the code from ChatGPT. I have removed that part now. But then the form just keeps loading and does not upload/send.
-
This reply was modified 2 weeks, 1 day ago by
You must be logged in to reply to this topic.