• 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 4 replies - 1 through 4 (of 4 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.

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.