Skip Navigation

[Résolu] Problem with validation of image size when editing post

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I am trying to use the CRED API validation hook to verify an image size, but the custom field value always includes the "-150x150" dimensions in its URL. This breaks my image size validation code.

Solution: Remove the -150x150 dimensions from the URL using str_replace, so you can check the original image dimensions:

$orig = str_replace('-150x150', '', $fields['_featured_image']['value']);
            $check = getimagesize( $orig );

Relevant Documentation:
http://php.net/manual/en/function.str-replace.php

This support ticket is created Il y a 6 années et 12 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par toolset-dave Il y a 6 années et 12 mois.

Assisté par: Christian Cox.

Auteur
Publications
#597392

Hi, I have a validation of featured image size in CRED content forms (for creation and editing CPT). There is a problem when I want to save some changes of CPT in CRED Edit form. I get an error that the image has another dimensions than it´s required (300x200px). If I hover over the image I see that it gives "-150x150.png" after name of image. But the image uploaded with CRED form for creating new CPT is correct, even if I check it in Media.

function validate_featured_image_size( $field_data, $form_data ){
 
    $form_id = array( 292,488 );
 
    $target_width = 300;
    $target_height = 200;
 
    if ( in_array( $form_data['id'], $form_id ) ) {
 
        list( $fields,$errors ) = $field_data;
 
 		if (empty($fields['_featured_image']['value']))
        {
            $errors['_featured_image'] = 'Prosíme, nahrajte logo';
        }
		else {
			$check = getimagesize( $fields['_featured_image']['value'] );
			if ( $check !== false ) {
 
				$width = $check[0];
				$height = $check[1];
 
				if ( $width !== $target_width || $height !== $target_height ) {
					$errors['_featured_image'] = "Obrázek nemá uvedené rozměry";
				}
        	}
		}
		
        $field_data = array($fields,$errors);
    }
 
    return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );
#597471

Hi, if you remove "-150x150" from the filename using str_replace, is the problem resolved?

function validate_featured_image_size( $field_data, $form_data ){
  
    $form_id = array( 292,488 );
  
    $target_width = 300;
    $target_height = 200;
  
    if ( in_array( $form_data['id'], $form_id ) ) {
  
        list( $fields,$errors ) = $field_data;
  
        if (empty($fields['_featured_image']['value']))
        {
            $errors['_featured_image'] = 'Prosíme, nahrajte logo';
        }
        else {
            $orig = str_replace('-150x150', '', $fields['_featured_image']['value']);
            $check = getimagesize( $orig );
            if ( $check !== false ) {
  
                $width = $check[0];
                $height = $check[1];
  
                if ( $width !== $target_width || $height !== $target_height ) {
                    $errors['_featured_image'] = "Obrázek nemá uvedené rozměry";
                }
            }
        }
         
        $field_data = array($fields,$errors);
    }
  
    return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );
#597487

Thank you, Christian, it fixed the problem.