You are here

function ad_image_validate_size in Advertisement 7

Same name and namespace in other branches
  1. 5.2 image/ad_image.module \ad_image_validate_size()
  2. 5 image/ad_image.module \ad_image_validate_size()
  3. 6.3 image/ad_image.module \ad_image_validate_size()
  4. 6 image/ad_image.module \ad_image_validate_size()
  5. 6.2 image/ad_image.module \ad_image_validate_size()

Validate that the size of the uploaded image is within the defined limits.

2 calls to ad_image_validate_size()
ad_image_load_image in image/ad_image.module
Returns image object from given ad node.
_ad_image_node_form in image/ad_image.module
Adapi helper function for displaying a node form.

File

image/ad_image.module, line 397
Enhances the ad module to support banner ads.

Code

function ad_image_validate_size($file, $nid) {
  $size = NULL;
  $error = FALSE;
  $edit = isset($_POST['edit']) ? $_POST['edit'] : array();
  if (is_object($file)) {

    // TODO: Detect if new terms have been set, and if so validate against
    // them, not the old ones.  See what's in $edit['taxonomy'].
    $node = node_load($nid);
    $terms = module_invoke('taxonomy', 'node_get_terms', $node);
    if (count($terms) == 0) {

      // We need at least a single (NULL) term to be ensure we still get the
      // default image size.
      $terms[] = NULL;
    }
    foreach ($terms as $tid => $term) {
      list($size->width, $size->height) = getimagesize($file->filepath);
      $size->bytes = strlen(join('', file($file->filepath)));
      if ($format = ad_image_format_load($tid)) {
        if ($size->width < $format->min_width) {
          drupal_set_message(t('The image %name is only %current pixels wide, which is less than the minimum of %minimum pixels allowed in the %group ad group.', array(
            '%name' => $file->filename,
            '%current' => $size->width,
            '%minimum' => $format->min_width,
            '%group' => isset($term->name) ? $term->name : t('default'),
          )), 'error');
          $error = TRUE;
        }
        else {
          if ($format->max_width && $size->width > $format->max_width) {
            drupal_set_message(t('The image %name is %current pixels wide, which is more than the maximum of %maximum pixels allowed in the %group ad group.', array(
              '%name' => $file->filename,
              '%current' => $size->width,
              '%maximum' => $format->max_width,
              '%group' => isset($term->name) ? $term->name : t('default'),
            )), 'error');
            $error = TRUE;
          }
        }
        if ($size->height < $format->min_height) {
          drupal_set_message(t('The image %name is only %current pixels high, which is less than the minimum of %minimum pixels allowed in the %group ad group.', array(
            '%name' => $file->filename,
            '%current' => $size->height,
            '%minimum' => $format->min_height,
            '%group' => isset($term->name) ? $term->name : t('default'),
          )), 'error');
          $error = TRUE;
        }
        else {
          if ($format->max_height && $size->height > $format->max_height) {
            drupal_set_message(t('The image %name is %current pixels high, which is more than the maximum of %maximum pixels allowed in the %group ad group.', array(
              '%name' => $file->filename,
              '%current' => $size->height,
              '%maximum' => $format->max_height,
              '%group' => isset($term->name) ? $term->name : t('default'),
            )), 'error');
            $error = TRUE;
          }
        }
        if ($format->max_size && $size->bytes > $format->max_size) {
          drupal_set_message(t('The image %name is %current bytes in size, which is more than the maximum of %maximum bytes allowed in the %group ad group.', array(
            '%name' => $file->filename,
            '%current' => $size->bytes,
            '%maximum' => $format->max_size,
            '%group' => isset($term->name) ? $term->name : t('default'),
          )), 'error');
          $error = TRUE;
        }
      }
    }
  }
  else {
    $error = TRUE;
    drupal_set_message('Please report error: $file is not an object, bug #146147.');
  }
  if ($error) {
    return FALSE;
  }
  else {
    return $size;
  }
}