[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/audio/ -> audio_image.inc (source)

   1  <?php
   2  // $Id: audio_image.inc,v 1.10 2008/10/02 18:03:44 drewish Exp $
   3  
   4  /**
   5   * Audio Image Functions.
   6   */
   7  
   8  /**
   9   * Returns an array of all the different image types with "clean" descriptions.
  10   *
  11   * @param $index
  12   *   Optional index into the array.
  13   * @return
  14   *   If $index is specified a string will be returned. If $index isn't
  15   *   specified an array of strings will be returned.
  16   */
  17  function audio_image_type_clean_array($index = NULL) {
  18    $ret = array(
  19      0x00 => 'other',
  20      0x01 => 'file_icon',
  21      0x02 => 'other_file_icon',
  22      0x03 => 'cover_front',
  23      0x04 => 'cover_back',
  24      0x05 => 'leaflet',
  25      0x06 => 'media',
  26      0x07 => 'lead_artist',
  27      0x08 => 'artist',
  28      0x09 => 'conductor',
  29      0x0A => 'band_orch',
  30      0x0B => 'comp',
  31      0x0C => 'lyricist',
  32      0x0D => 'rec_loc',
  33      0x0E => 'recording',
  34      0x0F => 'performance',
  35      0x10 => 'screen_cap',
  36      0x11 => 'fish',
  37      0x12 => 'illust',
  38      0x13 => 'band_logo',
  39      0x14 => 'pub_logo'
  40    );
  41    return is_null($index) ? $ret : $ret[$index];
  42  }
  43  
  44  /**
  45   * Returns an array of all the different image types with "dirty" descriptions.
  46   *
  47   * @param $index
  48   *   Optional index into the array.
  49   * @return
  50   *   If $index is specified a string will be returned. If $index isn't
  51   *   specified an array of strings will be returned.
  52   */
  53  function audio_image_type_dirty_array($index = NULL) {
  54    $ret = array(
  55      0x00 => t('Other'),
  56      0x01 => t("32x32 pixels 'file icon' (PNG only)"),
  57      0x02 => t('Other file icon'),
  58      0x03 => t('Cover (front)'),
  59      0x04 => t('Cover (back)'),
  60      0x05 => t('Leaflet page'),
  61      0x06 => t('Media (e.g. label side of CD)'),
  62      0x07 => t('Lead artist/lead performer/soloist'),
  63      0x08 => t('Artist/performer'),
  64      0x09 => t('Conductor'),
  65      0x0A => t('Band/Orchestra'),
  66      0x0B => t('Composer'),
  67      0x0C => t('Lyricist/text writer'),
  68      0x0D => t('Recording Location'),
  69      0x0E => t('During recording'),
  70      0x0F => t('During performance'),
  71      0x10 => t('Movie/video screen capture'),
  72      0x11 => t('A bright coloured fish'),
  73      0x12 => t('Illustration'),
  74      0x13 => t('Band/artist logotype'),
  75      0x14 => t('Publisher/Studio logotype'),
  76    );
  77    return is_null($index) ? $ret : $ret[$index];
  78  }
  79  
  80  /**
  81   * Creates a temporary audio image from a variable.
  82   *
  83   * The image is cropped to a square and then resized to the image size setting.
  84   *
  85   * @param $basename
  86   *   Name of the audio file this image accompanies.
  87   * @param $data
  88   *   The raw image data.
  89   * @param $mimetype
  90   *   The MIME type of the image.
  91   * @param $pictype
  92   *   Integer pictype indexes from audio_image_type_clean_array() or
  93   *   audio_image_type_dirty_array().
  94   * @return
  95   *   A file object with image info or FALSE on error.
  96   */
  97  function audio_image_save_data($basename, $data, $mimetype, $pictype) {
  98    global $user;
  99  
 100    // Gotta have a name to save to.
 101    $filepath = _audio_image_filename($basename, $mimetype, $pictype, TRUE);
 102    if (!$filepath) {
 103      return FALSE;
 104    }
 105  
 106    // Save the data.
 107    $filepath = file_save_data($data, $filepath, FILE_EXISTS_RENAME);
 108    if (!$filepath) {
 109      return FALSE;
 110    }
 111  
 112    // Make sure it's a valid image.
 113    $image = image_get_info($filepath);
 114    if (!$image) {
 115      file_delete($filepath);
 116      return FALSE;
 117    }
 118  
 119    // Resize the image
 120    $size = variable_get('audio_image_size', 170);
 121    if (image_scale_and_crop($filepath, $filepath, $size, $size)) {
 122      // Changing the image dimensions will affect the file size. Clear out
 123      // PHP's cached value so we can find the new size.
 124      clearstatcache();
 125      $image = image_get_info($filepath);
 126    }
 127  
 128    // Store the file in the database so it can be removed by cron if it's not
 129    // used.
 130    $file = new stdClass();
 131    $file->filepath = $filepath;
 132    $file->filename = basename($file->filepath);
 133    $file->filemime = $mimetype;
 134    $file->filesize = $image['file_size'];
 135    $file->uid = $user->uid;
 136    $file->status = FILE_STATUS_TEMPORARY;
 137    $file->timestamp = time();
 138  
 139    drupal_write_record('files', $file);
 140  
 141    $file->pictype = $pictype;
 142    $file->height = $image['height'];
 143    $file->width = $image['width'];
 144  
 145    return $file;
 146  }
 147  
 148  /**
 149   * If the file is an image it will be resized to meet the audio image size
 150   * guidelines.
 151   *
 152   * @param $file
 153   *   A Drupal file object. This function may resize the file affecting its size.
 154   * @return
 155   *   An array. If the file is an image and did not meet the requirements, it
 156   *   will contain an error message.
 157   */
 158  function audio_image_validate_size($file) {
 159    $errors = array();
 160  
 161    // Check first that the file is an image.
 162    if ($info = image_get_info($file->filepath)) {
 163      $size = variable_get('audio_image_size', 170);
 164      if ($info['width'] > $size || $info['height'] > $size) {
 165        // Try to resize the image to fit the dimensions.
 166        if (image_get_toolkit() && image_scale_and_crop($file->filepath, $file->filepath, $size, $size)) {
 167          drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %height x %width pixels.', array('%height' => $size, '%width' => $size)));
 168  
 169          // Clear the cached filesize and refresh the image information.
 170          clearstatcache();
 171          $info = image_get_info($file->filepath);
 172          $file->filesize = $info['file_size'];
 173        }
 174        else {
 175          $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
 176        }
 177      }
 178    }
 179  
 180    return $errors;
 181  }
 182  
 183  
 184  /**
 185   * Creates the image's filename in the form directory/prefix_imagetype.ext
 186   *
 187   * @param $prefix
 188   *   The prefix of the filename, probably should be the nid.
 189   * @param $mimetype
 190   *   The image's mime type. jpeg, png and gif are the only formats allowed.
 191   * @param $pictype
 192   *   Integer specifying the picture type.
 193   * @param $in_tempdir
 194   *   Boolean indicating if the file be in the temp directory.
 195   * @return
 196   *   Full filepath or null in case of an error.
 197   */
 198  function _audio_image_filename($prefix, $mimetype, $pictype = 0x03, $in_tempdir = FALSE) {
 199    $directory = $in_tempdir ? file_directory_temp() : audio_get_directory() . '/images';
 200    file_check_directory($directory, TRUE);
 201  
 202    //get the clean image type
 203    $image_type = audio_image_type_clean_array($pictype);
 204  
 205    switch ($mimetype) {
 206      case 'image/jpeg':
 207        $ext = '.jpg';
 208        break;
 209      case 'image/png':
 210        $ext = '.png';
 211        break;
 212      case 'image/gif':
 213        $ext = '.gif';
 214        break;
 215      default:
 216        // don't return a filename for any other extensions
 217        return;
 218    }
 219  
 220    // combine the path and file extension from the mimetype
 221    return $directory .'/'. $prefix .'_'. $image_type . $ext;
 222  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7