• 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 4 replies - 16 through 19 (of 19 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Ask ChatGPT for support then.

    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    I have tried that for five days as well. It can not solve the problem. It says there is a problem with the way CF7 is handling uploads.

    Amal

    (@amal1373)

    @takayukister

    $size = getimagesize($_FILES[‘image’][‘tmp_name’]);
    getimagesize() returns empty.
    The file exists in $_FILES[‘image’][‘tmp_name’], but image metadata cannot be read from it during validation.
    $_FILES:
    (
    [name] => test.jpg
    [full_path] => test.jpg
    [type] => image/jpeg
    [tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpr5jkh2
    [error] => 0
    [size] => 611211
    )

    Thread Starter henrik.arvedsen

    (@henrikarvedsen)

    I got it working with this code:

    add_filter('wpcf7_validate_file*', 'validate_image_dimensions_cf7', 20, 2);
    add_filter('wpcf7_validate_file', 'validate_image_dimensions_cf7', 20, 2);

    function validate_image_dimensions_cf7($result, $tag) {
    $name = $tag->name;

    // Dit præcise felt-navn
    if ($name !== 'catch-image') {
    return $result;
    }

    error_log("CF7 Dimension Validation started for field: " . $name);

    $submission = WPCF7_Submission::get_instance();
    if (!$submission) {
    error_log("No submission instance found");
    return $result;
    }

    $uploaded_files = $submission->uploaded_files();
    if (empty($uploaded_files[$name]) || empty($uploaded_files[$name][0])) {
    error_log("No uploaded file in submission for " . $name);
    $result->invalidate($tag, "Filen blev ikke modtaget korrekt.");
    return $result;
    }

    $file_path = $uploaded_files[$name][0];

    if (!file_exists($file_path)) {
    error_log("File not found at path: " . $file_path);
    $result->invalidate($tag, "Filen blev ikke modtaget korrekt på serveren.");
    return $result;
    }

    error_log("File found at: " . $file_path);

    $info = @getimagesize($file_path);
    if ($info === false || empty($info[0]) || empty($info[1])) {
    $result->invalidate($tag, "Filen er ikke et gyldigt billede.");
    return $result;
    }

    $width = (int) $info[0];
    $height = (int) $info[1];

    error_log("Billede dimensioner: {$width}×{$height} px");

    if ($width < 3000 || $height < 3000) {
    $result->invalidate(
    $tag,
    sprintf("Billedet er for lille (%d×%d px). Minimumskrav: 3000×3000 pixels.", $width, $height)
    );
    error_log("Invalidation triggered - image too small");
    } else {
    error_log("Image passed validation");
    }

    return $result;
    }
Viewing 4 replies - 16 through 19 (of 19 total)

You must be logged in to reply to this topic.