You are here

function imagefield_create_admin_thumb in ImageField 6.3

Create a thumbnail to be shown while editing an image.

1 call to imagefield_create_admin_thumb()
imagefield_file_admin_thumb_path in ./imagefield_file.inc
Given a file, return the path the image thumbnail used while editing.

File

./imagefield_file.inc, line 76
hook_file and imagefield file functions.

Code

function imagefield_create_admin_thumb($source, $destination) {
  if (!is_file($source)) {
    return FALSE;
  }
  $info = image_get_info($source);
  $size = explode('x', variable_get('imagefield_thumb_size', '100x100'));

  // Check if the destination image needs to be regenerated to match a new size.
  if (is_file($destination)) {
    $thumb_info = image_get_info($destination);
    if ($thumb_info['width'] != $size[0] && $thumb_info['height'] != $size[1] && ($info['width'] > $size[0] || $info['height'] > $size[1])) {
      unlink($destination);
    }
    else {
      return;
    }
  }

  // Ensure the destination directory exists and is writable.
  $directories = explode('/', $destination);
  array_pop($directories);

  // Remove the file itself.
  // Get the file system directory.
  $file_system = file_directory_path();
  foreach ($directories as $directory) {
    $full_path = isset($full_path) ? $full_path . '/' . $directory : $directory;

    // Don't check directories outside the file system path.
    if (strpos($full_path, $file_system) === 0) {
      field_file_check_directory($full_path, FILE_CREATE_DIRECTORY);
    }
  }

  // Create the thumbnail.
  if ($info['width'] <= $size[0] && $info['height'] <= $size[1]) {
    file_copy($source, $destination);
  }
  elseif (module_exists('imageapi') && imageapi_default_toolkit()) {
    $res = imageapi_image_open($source);
    imageapi_image_scale($res, $size[0], $size[1]);
    imageapi_image_close($res, $destination);
  }
  elseif (image_get_toolkit() && image_scale($source, $destination, $size[0], $size[1])) {

    // Set permissions. This is done for us when using file_copy().
    @chmod($destination, 0664);
  }
  else {
    drupal_set_message(t('An image thumbnail was not able to be created.'), 'error');
  }
}