[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/imagefield/ -> imagefield_file.inc (source)

   1  <?php
   2  // $Id: imagefield_file.inc,v 1.20 2010/12/10 17:33:11 quicksketch Exp $
   3  
   4  /**
   5   * @file
   6   * hook_file and imagefield file functions.
   7   */
   8  
   9  /**
  10   * Implementation of hook_file_insert().
  11   */
  12  function imagefield_file_insert($file) {
  13    // Currently empty. Thumbnails are now generated on preview.
  14  }
  15  
  16  /**
  17   * Implementation of hook_file_delete().
  18   *
  19   * Delete the admin thumbnail when the original is deleted.
  20   */
  21  function imagefield_file_delete($file) {
  22    if (imagefield_file_is_image($file)) {
  23      file_delete(imagefield_file_admin_thumb_path($file, FALSE));
  24    }
  25  }
  26  
  27  /**
  28   * Implementation of hook_file_references().
  29   */
  30  function imagefield_file_references($file) {
  31    $file = (object) $file;
  32    $count = 0;
  33  
  34    // We currently only check if images are in use when deleting from content
  35    // type default images. All other reference counting is left to FileField.
  36    if (isset($file->imagefield_type_name)) {
  37      $content_types = content_types();
  38      foreach ($content_types as $type_name => $content_type) {
  39        if ($type_name != $file->imagefield_type_name) {
  40          foreach ($content_type['fields'] as $field_name => $field) {
  41            if ($field['widget']['default_image'] && $field['widget']['default_image']['fid'] == $file->fid) {
  42              $count++;
  43            }
  44          }
  45        }
  46      }
  47    }
  48    return $count ? array('imagefield' => $count) : NULL;
  49  }
  50  
  51  /**
  52   * Simple utility function to check if a file is an image.
  53   */
  54  function imagefield_file_is_image($file) {
  55    $file = (object)$file;
  56    return in_array($file->filemime, array('image/jpg', 'image/pjpeg', 'image/jpeg', 'image/png', 'image/gif'));
  57  }
  58  
  59  /**
  60   * Given a file, return the path the image thumbnail used while editing.
  61   */
  62  function imagefield_file_admin_thumb_path($file, $create_thumb = TRUE) {
  63    $file = (object)$file;
  64    $short_path = preg_replace('/^' . preg_quote(file_directory_path(), '/') . '/', '', $file->filepath);
  65    $filepath = file_directory_path() . '/imagefield_thumbs' . $short_path;
  66  
  67    if ($create_thumb) {
  68      imagefield_create_admin_thumb($file->filepath, $filepath);
  69    }
  70  
  71    return $filepath;
  72  }
  73  
  74  /**
  75   * Create a thumbnail to be shown while editing an image.
  76   */
  77  function imagefield_create_admin_thumb($source, $destination) {
  78    if (!is_file($source)) {
  79      return FALSE;
  80    }
  81  
  82    $info = image_get_info($source);
  83    $size = explode('x', variable_get('imagefield_thumb_size', '100x100'));
  84  
  85    // Check if the destination image needs to be regenerated to match a new size.
  86    if (is_file($destination)) {
  87      $thumb_info = image_get_info($destination);
  88      if ($thumb_info['width'] != $size[0] && $thumb_info['height'] != $size[1] && ($info['width'] > $size[0] || $info['height'] > $size[1])) {
  89        unlink($destination);
  90      }
  91      else {
  92        return;
  93      }
  94    }
  95  
  96    // Ensure the destination directory exists and is writable.
  97    $directories = explode('/', $destination);
  98    array_pop($directories); // Remove the file itself.
  99    // Get the file system directory.
 100    $file_system = file_directory_path();
 101    foreach ($directories as $directory) {
 102      $full_path = isset($full_path) ? $full_path . '/' . $directory : $directory;
 103      // Don't check directories outside the file system path.
 104      if (strpos($full_path, $file_system) === 0) {
 105        field_file_check_directory($full_path, FILE_CREATE_DIRECTORY);
 106      }
 107    }
 108  
 109    // Create the thumbnail.
 110    if ($info['width'] <= $size[0] && $info['height'] <= $size[1]) {
 111      file_copy($source, $destination);
 112    }
 113    // Use ImageAPI, if installed.
 114    elseif (module_exists('imageapi') && imageapi_default_toolkit()) {
 115      $res = imageapi_image_open($source);
 116      imageapi_image_scale($res, $size[0], $size[1]);
 117      imageapi_image_close($res, $destination);
 118    }
 119    elseif (image_get_toolkit() && image_scale($source, $destination, $size[0], $size[1])) {
 120      // Set permissions. This is done for us when using file_copy().
 121      @chmod($destination, 0664);
 122    }
 123    else {
 124      drupal_set_message(t('An image thumbnail was not able to be created.'), 'error');
 125    }
 126  }


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