uploads image custom validation using the Drupal 7

uploads IMAGES with custom validation using the Drupal 7 Forms API

IF we want to Handle the IMage For Upload_logo 
Submit BUtton
function module_image_form() {
   
  // Set the form's enctype to allow it to handle file uploads
  $form['#attributes']['enctype'] = "multipart/form-data";
     
  $form['logo'] = array(
    '#type' => 'file',
    '#title' => t('Logo'),
  );
   
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
   
  return $form;
   
}
Validation FUnction call
function module_image_form_validate($form, &$form_state) {
  // Set this to the name of the form's file field
  $field = 'logo';
  // The directory the image will be saved to (file_directory_path() 
  // returns the path to your default files directory).
  $directory = file_directory_path() . '/images';
  // Drupal will attempt to resize the image if it is larger than 
  // the following maximum dimensions (width x height)
  $max_dimensions = '800x600';
   
  // We don't care about the minimum dimensions
  $min_dimensions = 0;
  // file_check_directory() ensures the destination directory is valid and 
  // will attempt to create it if it doesn't already exist.
  if (file_check_directory($directory, FILE_CREATE_DIRECTORY, $field)) {
    // Specify the validators. The first is Drupal's built-in function for validating
    // the image's dimensions. The second is our custom validator to exclude GIFs.
    $validators = array(
      'file_validate_image_resolution' => array($max_dimensions, $min_dimensions),
      'module_validate_image_type' => array(),
    );
    // Move the file to its final location if the validators pass, else
    // return to the form and display the errors.
    if ($file = file_save_upload($field, $validators, $directory)) {
      // Set the file's status to permanent, which will prevent Drupal's file 
      // garbage collection from deleting it.
      file_set_status($file, FILE_STATUS_PERMANENT);
      // We add our final file object to the form's storage array, so that it gets passed
      // through to the form's submit handler, where we can act on it.
      $form_state['storage']['file'] = $file;
    }
  }
}
Image Validaiton
function module_validate_image_type($file) {
  $errors = array();
  $info = image_get_info($file->filepath);
  if ($info['mime_type'] == 'image/gif') {
    $errors[] = t('Only JPEG and PNG images are allowed.');
  }
  return $errors;
}
Finally, 
function module_sample_form_submit($form, &$form_state) {
  // print_r($form_state['storage']['file']) to view the uploaded file's details
}

0 comments:

Post a Comment

Don't Forget to comment

About Me

My photo

HI i am mohammed  a Embedded Software Developer ,SEO specialist,Electronic engineer   Read more

ads