• Resolved henrik.arvedsen

    (@henrikarvedsen)


    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?

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    You can do it by implementing your own custom validation filter.

    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    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;
    }
    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Where can we see the form in question?

    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    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.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    See Editing form template

    Can you share the whole content in the Form tab panel?

    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    <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>
    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Look carefully at these lines:

    <label Image
    [file* catch_image filetypes:jpg|jpeg|heic limit:20mb]
    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    Off course – I see teo errors there:
    This is the right code:

    <label> Image
    [file* catch_image filetypes:jpg|jpeg|heic limit:20mb]</label>
    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    But it still doesn’t validate the image. It just prevent it from uploading now.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Do you get any response message after you submit the form?

    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    Yes I get this error message both when trying to send a smaller and bigger image.
    “Upload failed. Please try again.”

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    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;
    }
    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    I have tried. But don’t see the error or any solutions.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Is that part necessary? Why did you add it?

    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    I got the code from ChatGPT. I have removed that part now. But then the form just keeps loading and does not upload/send.

Viewing 15 replies - 1 through 15 (of 19 total)

You must be logged in to reply to this topic.