| 1 |
<?php
|
| 2 |
// $Id: imagefield_file.inc,v 1.17 2009/04/01 19:47:22 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 |
* Simple utility function to check if a file is an image.
|
| 29 |
*/
|
| 30 |
function imagefield_file_is_image($file) {
|
| 31 |
$file = (object)$file;
|
| 32 |
return in_array($file->filemime, array('image/jpg', 'image/pjpeg', 'image/jpeg', 'image/png', 'image/gif'));
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Given a file, return the path the image thumbnail used while editing.
|
| 37 |
*/
|
| 38 |
function imagefield_file_admin_thumb_path($file, $create_thumb = TRUE) {
|
| 39 |
$file = (object)$file;
|
| 40 |
$short_path = preg_replace('/^' . preg_quote(file_directory_path(), '/') . '/', '', $file->filepath);
|
| 41 |
$filepath = file_directory_path() . '/imagefield_thumbs' . $short_path;
|
| 42 |
|
| 43 |
if ($create_thumb) {
|
| 44 |
imagefield_create_admin_thumb($file->filepath, $filepath);
|
| 45 |
}
|
| 46 |
|
| 47 |
return $filepath;
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Create a thumbnail to be shown while editing an image.
|
| 52 |
*/
|
| 53 |
function imagefield_create_admin_thumb($source, $destination) {
|
| 54 |
if (!is_file($source)) {
|
| 55 |
return FALSE;
|
| 56 |
}
|
| 57 |
|
| 58 |
$info = image_get_info($source);
|
| 59 |
$size = explode('x', variable_get('imagefield_thumb_size', '100x100'));
|
| 60 |
|
| 61 |
// Check if the destination image needs to be regenerated to match a new size.
|
| 62 |
if (is_file($destination)) {
|
| 63 |
$thumb_info = image_get_info($destination);
|
| 64 |
if ($thumb_info['width'] != $size[0] && $thumb_info['height'] != $size[1] && ($info['width'] > $size[0] || $info['height'] > $size[1])) {
|
| 65 |
unlink($destination);
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
return;
|
| 69 |
}
|
| 70 |
}
|
| 71 |
|
| 72 |
// Ensure the destination directory exists and is writable.
|
| 73 |
$directories = explode('/', $destination);
|
| 74 |
array_pop($directories); // Remove the file itself.
|
| 75 |
// Get the file system directory.
|
| 76 |
$file_system = file_directory_path();
|
| 77 |
foreach ($directories as $directory) {
|
| 78 |
$full_path = isset($full_path) ? $full_path . '/' . $directory : $directory;
|
| 79 |
// Don't check directories outside the file system path.
|
| 80 |
if (strpos($full_path, $file_system) === 0) {
|
| 81 |
field_file_check_directory($full_path, FILE_CREATE_DIRECTORY);
|
| 82 |
}
|
| 83 |
}
|
| 84 |
|
| 85 |
// Create the thumbnail.
|
| 86 |
if ($info['width'] <= $size[0] && $info['height'] <= $size[1]) {
|
| 87 |
file_copy($source, $destination);
|
| 88 |
}
|
| 89 |
elseif (image_get_toolkit() && @image_scale($source, $destination, $size[0], $size[1])) {
|
| 90 |
// Set permissions. This is done for us when using file_copy().
|
| 91 |
@chmod($destination, 0664);
|
| 92 |
}
|
| 93 |
else {
|
| 94 |
drupal_set_message(t('An image thumbnail was not able to be created.'), 'error');
|
| 95 |
}
|
| 96 |
}
|