diff --git includes/common.inc includes/common.inc index 5147639..52817d1 100644 --- includes/common.inc +++ includes/common.inc @@ -3907,6 +3907,9 @@ function drupal_common_theme() { 'image' => array( 'arguments' => array('path' => NULL, 'alt' => '', 'title' => '', 'attributes' => NULL, 'getsize' => TRUE), ), + 'image_preset' => array( + 'arguments' => array('preset' => NULL, 'path' => NULL, 'alt' => '', 'title' => '', 'attributes' => NULL, 'getsize' => TRUE), + ), 'breadcrumb' => array( 'arguments' => array('breadcrumb' => NULL), ), @@ -4576,7 +4579,7 @@ function drupal_flush_all_caches() { node_types_rebuild(); // Don't clear cache_form - in-progress form submissions may break. // Ordered so clearing the page cache will always be the last action. - $core = array('cache', 'cache_filter', 'cache_registry', 'cache_page'); + $core = array('cache', 'cache_filter', 'cache_registry', 'cache_page', 'cache_image'); $cache_tables = array_merge(module_invoke_all('flush_caches'), $core); foreach ($cache_tables as $table) { cache_clear_all('*', $table, TRUE); diff --git includes/image.inc includes/image.inc index 4309ac9..5102435 100644 --- includes/image.inc +++ includes/image.inc @@ -384,5 +384,545 @@ function image_save(stdClass $image, $destination = NULL) { } /** + * Get an array of all presets and their settings. + * + * @param $reset + * If set to TRUE the internal preset cache will be cleared. + * @return + * Array of presets array($pid => array('id' => integer, 'name' => string)). + */ +function image_presets($reset = FALSE) { + static $presets = array(); + + // Clear caches if $reset is true; + if ($reset) { + $presets = array(); + cache_clear_all('image_presets', 'cache'); + } + + // Grab from cache or build the array. + if ($cache = cache_get('image_presets', 'cache')) { + $presets = $cache->data; + } + else { + $result = db_query('SELECT * FROM {image_presets} ORDER BY name'); + while ($preset = db_fetch_array($result)) { + $presets[$preset['name']] = $preset; + $presets[$preset['name']]['actions'] = image_preset_actions($preset, $reset); + } + cache_set('image_presets', $presets); + } + + return $presets; +} + +/** + * Get an array of image presets suitable for using as select list options. + * + * @param $include_empty + * If TRUE a option will be inserted in the options array. + * @return + * Array of image presets both key and value are set to preset name. + */ +function image_preset_options($include_empty = TRUE) { + $presets = image_presets(); + $options = array(); + if ($include_empty && !empty($presets)) { + $options[''] = t(''); + } + $options = array_merge($options, drupal_map_assoc(array_keys($presets))); + if (empty($options)) { + $options[''] = t('No defined presets'); + } + return $options; +} + +/** + * Load a preset by preset name or ID. May be used as a loader for menu items. + * + * @param $name + * The name of the preset. + * @param $pid + * Optional. The numeric id of a preset if the name is not known. + * @param $reset + * If TRUE the internal preset cache will be reset. + * @return + * An image preset with the format of + * array('name' => array('ipid' => int, 'name' => string, 'actions' => array())). + * If the preset name or ID is not valid, an empty array is returned. + */ +function image_preset_load($name = NULL, $pid = NULL, $reset = FALSE) { + $presets = image_presets($reset); + + // If retrieving by name. + if (isset($name) && isset($presets[$name])) { + return $presets[$name]; + } + + // If retrieving by PID. + if (isset($pid)) { + foreach ($presets as $name => $preset) { + if ($preset['ipid'] == $pid) { + return $preset; + } + } + } + + // Otherwise the preset was not found. + return FALSE; +} + +/** + * Save an image preset. + * + * @param preset + * An image preset array. + * @return + * A preset array. In the case of a new preset, 'ipid' will be populated. + */ +function image_preset_save($preset) { + if (isset($preset['ipid']) && is_numeric($preset['ipid'])) { + // Load the existing preset to make sure we account for renamed presets. + $old_preset = image_preset_load(NULL, $preset['ipid']); + image_preset_flush($old_preset); + drupal_write_record('image_presets', $preset, 'ipid'); + } + else { + drupal_write_record('image_presets', $preset); + } + + // Reset presets cache. + image_presets(TRUE); + menu_rebuild(); + + return $preset; +} + +/** + * Delete an image preset. + * + * @param preset + * An image preset array. + * @return + * TRUE on success. + */ +function image_preset_delete($preset) { + image_preset_flush($preset); + db_delete('image_actions')->condition('ipid', $preset['ipid'])->execute(); + db_delete('image_presets')->condition('ipid', $preset['ipid'])->execute(); + image_presets(TRUE); + menu_rebuild(); + return TRUE; +} + +/** + * Load all the actions for an image preset. + * + * @param $preset + * An image preset array. + * @param $reset + * If set to TRUE the internal cache image actions will be reset. + * @return + * Array of actions associated with specified preset in the format + * array('ipid' => array()) + * Returns an empty array if the specified preset has no actions. + */ +function image_preset_actions($preset, $reset = FALSE) { + static $actions; + + if ($reset || !isset($actions)) { + $actions = array(); + $definitions = image_action_definitions($reset); + $result = db_query('SELECT * FROM {image_actions} where ipid = %d ORDER BY weight ASC', $preset['ipid']); + while ($action = db_fetch_array($result)) { + $action['data'] = unserialize($action['data']); + $action = array_merge($definitions[$action['action']], $action); + $actions[$preset['ipid']][$action['iaid']] = $action; + } + } + + return isset($actions[$preset['ipid']]) ? $actions[$preset['ipid']] : array(); +} + +/** + * Flush cached media for a preset. + * + * @param $preset + * An image preset. + */ +function image_preset_flush($preset) { + $preset_directory = realpath(file_directory_path() .'/presets/'. $preset['name']); + if (is_dir($preset_directory)) { + file_unmanaged_delete_recursive($preset_directory); + } +} + +/** + * Return a complete URL to an image when using a preset. + * + * @param $preset_name + * The name of the preset to be used with this image. + * @param $path + * The path to the image. + * @return + * The complete absolute URL to a preset image. + */ +function image_preset_url($preset_name, $path) { + $path = _image_strip_file_directory($path); + return file_create_url(file_create_path() .'/presets/'. $preset_name .'/'. $path); +} + +/** + * Return a relative path to an image when using a preset. + * + * @param $preset_name + * The name of the preset to be used with this image. + * @param $path + * The path to the image. + * @param $file_system + * Optional. By default this will return the file system path, which may be + * relative to the Drupal root or an absolute path on the server. If set to + * FALSE, this will return the Drupal path instead, suitable for requesting + * the image. + * @return + * The path to an image preset image. If $file_system is TRUE the path may + * be used in an is_file() request. If $file_system is FALSE the path may + * be used in theme('image_preset') or url() requests since it contains a + * Drupal path. + */ +function image_preset_path($preset_name, $path, $file_system = TRUE) { + $path = _image_strip_file_directory($path); + if ($file_system || variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) { + return file_directory_path() . '/presets/' . $preset_name . '/' . $path; + } + else { + return 'system/files/presets/' . $preset_name . '/' . $path; + } +} + +/** + * Given an image preset and path, generate an image derivative. + * + * @param $preset_name + * The name of the preset to be used with this image. + * @param $path + * The path to the image. + * @return + * TRUE if the image is able to be generated successfully or FALSE if unable + * to create the image. This function may return NULL when the image + * generation is in progress but not complete. + */ +function image_preset_generate($preset_name, $path) { + $default_method = 'system_image_preset_request'; + $path = _image_strip_file_directory($path); + $method = variable_get('image_preset_generation_method', $default_method); + $preset = image_preset_load($preset_name); + + if (empty($preset)) { + watchdog('image', 'A derivative of the image %path could not be generated because the preset %preset does not exist.', array('%path' => $path, '%preset' => $preset_name), WATCHDOG_ERROR); + return FALSE; + } + + if (drupal_function_exists($method)) { + return $method($preset, $path); + } + else { + return $default_method($preset, $path); + } +} + +/** + * Create a new image based on an image preset. + * + * @param $preset + * An image preset array. + * @param $source + * Path of the source file. + * @param $destination + * Path of the destination file. + * @return + * TRUE if an image derivative is generated, FALSE if no image + * derivative is generated. NULL if the derivative is being generated. + */ +function image_preset_create_derivative($preset, $source, $destination) { + // Get the folder for the final location of this preset. + $directory = dirname($destination); + + // Build the destination folder tree if it doesn't already exist. + if (!file_check_directory($directory, FILE_CREATE_DIRECTORY) && !mkdir($directory, 0775, TRUE)) { + watchdog('image', 'Failed to create preset directory: %directory', array('%directory' => $directory), WATCHDOG_ERROR); + return FALSE; + } + + if (!$image = image_load($source)) { + return FALSE; + } + + foreach ($preset['actions'] as $action) { + if (!empty($action['data'])) { + // Make sure the width and height are computed first so they can be used + // in relative x/yoffsets like 'center' or 'bottom'. + if (isset($action['data']['width'])) { + $action['data']['width'] = _image_filter_percent($action['data']['width'], $image->info['width']); + } + if (isset($action['data']['height'])) { + $action['data']['height'] = _image_filter_percent($action['data']['height'], $image->info['height']); + } + if (isset($action['data']['xoffset'])) { + $action['data']['xoffset'] = _image_filter_percent($action['data']['xoffset'], $image->info['width'], $action['data']['width']); + } + if (isset($action['data']['yoffset'])) { + $action['data']['yoffset'] = _image_filter_percent($action['data']['yoffset'], $image->info['height'], $action['data']['height']); + } + } + image_action_apply($image, $action); + } + + if (!image_save($image, $destination)) { + if (file_exists($destination)) { + watchdog('image', 'Cached image file %destination already exists. There may be an issue with your rewrite configuration.', array('%destination' => $destination), WATCHDOG_ERROR); + } + return FALSE; + } + + return TRUE; +} + +/** + * Clear cached versions of a specific file in all presets. + * + * @param $path + * The Drupal file path to the original image. + */ +function image_path_flush($path) { + $path = _image_strip_file_directory($path); + foreach (image_presets() as $preset) { + if ($path = file_create_path('presets/'. $preset['name'] .'/'. $path)) { + file_unmanaged_delete($path); + } + } +} + +/** + * Load all image actions from the database. + * + * @param $reset + * If TRUE, the internal list of actions will be regenerated. + * @return + * Array of image actions. + */ +function image_actions($reset = FALSE) { + static $actions; + + if (!isset($actions) || $reset) { + $actions = array(); + + // Add database image actions. + $result = db_query('SELECT * FROM {image_actions}', NULL, array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $action) { + $action['data'] = unserialize($action['data']); + $definition = image_action_definition_load($action['action'], $reset); + // Do not load actions whose definition cannot be found. + if ($definition) { + $action = array_merge($definition, $action); + $actions[$action['iaid']] = $action; + } + } + } + + return $actions; +} + +/** + * Pull in actions exposed by other modules using hook_image_actions(). + * + * @param $toolkit + * Name of the image toolkit to use. Toolkit set by image_get_toolkit() + * will be used if none is specified. + * @param $reset + * If TRUE, regenerate the internal cache of action definitions. + * @return + * An array of actions to be used when transforming images. + */ +function image_action_definitions($toolkit = NULL, $reset = FALSE) { + static $actions; + + if (!isset($toolkit)) { + $toolkit = image_get_toolkit(); + } + + if (!isset($actions) || $reset) { + if (!$reset && ($cache = cache_get('image_actions')) && !empty($cache->data)) { + $actions = $cache->data; + } + else { + $actions = array(); + foreach (module_implements('image_actions') as $module) { + foreach (module_invoke($module, 'image_actions') as $key => $action) { + // Ensure the current toolkit supports the action. + $action['module'] = $module; + $action['action'] = $key; + $action['data'] = isset($action['data']) ? $action['data'] : array(); + $actions[$key] = $action; + }; + } + uasort($actions, '_image_actions_definitions_sort'); + cache_set('image_actions', $actions); + } + } + + return $actions; +} + +/** + * Load the definition for an action. + * + * The action definition is a set of default values that applies to an action + * regardless of user settings. This definition consists of an array containing + * at least the following values: + * - action: The unique name for the action being performed. Usually prefixed + * with the name of the module providing the action. + * - module: The module providing the action. + * - description: A description of the action. + * + * @param $action + * The name of the action definition to load. + * @param $reset + * If TRUE, regenerate the internal cache of action definitions. + * @return + * An array containing at least the following values. + * array( + * 'action' => Unique name of the action being performed + * 'module' => Name of module provoding the action + * 'description' => A description of the action + * ) + */ +function image_action_definition_load($action, $reset = FALSE) { + static $definition_cache; + + if (!isset($definition_cache[$action]) || $reset) { + $definitions = image_action_definitions($reset); + $definition = (isset($definitions[$action])) ? $definitions[$action] : array(); + $definition_cache[$action] = $definition; + } + + return isset($definition_cache[$action]) ? $definition_cache[$action] : FALSE; +} + +/** + * Load a single image action. + * + * @param $iaid + * The image action ID. + * @return + * An image action array. + */ +function image_action_load($iaid) { + $actions = image_actions(); + return isset($actions[$iaid]) ? $actions[$iaid] : FALSE; +} + +/** + * Save an image action. + * + * @param $action + * An image action array. + * @return + * An image action array. In the case of a new action 'iaid' will be set. + */ +function image_action_save($action) { + if (!empty($action['iaid'])) { + drupal_write_record('image_actions', $action, 'iaid'); + } + else { + drupal_write_record('image_actions', $action); + } + $preset = image_preset_load(NULL, $action['ipid']); + image_preset_flush($preset); + image_presets(TRUE); + return $action; +} + +/** + * Delete an image action. + * + * @param $action + * An image action array. + */ +function image_action_delete($action) { + db_delete('image_actions')->condition('iaid', $action['iaid'])->execute(); + $preset = image_preset_load(NULL, $action['ipid']); + image_preset_flush($preset); + image_presets(TRUE); +} + +/** + * Given an image object and action, perform the action on the file. + * + * @param $image + * An image object. + * @param $action + * An image action array. + * @return + * TRUE on success. FALSE if unable to perform action on image. + */ +function image_action_apply(&$image, $action) { + if (drupal_function_exists($action['function'])) { + return call_user_func($action['function'], $image, $action['data']); + } + return FALSE; +} + +/** + * Accept a percentage and return it in pixels. + */ +function _image_filter_percent($value, $current_pixels) { + if (strpos($value, '%') !== FALSE) { + $value = str_replace('%', '', $value) * 0.01 * $current_pixels; + } + return $value; +} + +/** + * Accept a keyword (center, top, left, etc) and return it as an offset in pixels. + */ +function _image_filter_keyword($value, $current_pixels, $new_pixels) { + switch ($value) { + case 'top': + case 'left': + $value = 0; + break; + case 'bottom': + case 'right': + $value = $current_pixels - $new_pixels; + break; + case 'center': + $value = $current_pixels/2 - $new_pixels/2; + break; + } + return $value; +} + +/** + * Remove a possible leading file directory path from the given path. + */ +function _image_strip_file_directory($path) { + $dirpath = file_directory_path(); + $dirlen = strlen($dirpath); + if (substr($path, 0, $dirlen + 1) == $dirpath .'/') { + $path = substr($path, $dirlen + 1); + } + return $path; +} + +/** + * Internal function for sorting image action definitions through uasort(). + */ +function _image_actions_definitions_sort($a, $b) { + return strcasecmp($a['name'], $b['name']); +} + + +/** * @} End of "defgroup image". */ diff --git includes/theme.inc includes/theme.inc index f19bdbb..e4a0f8c 100644 --- includes/theme.inc +++ includes/theme.inc @@ -1325,11 +1325,44 @@ function theme_links($links, $attributes = array('class' => 'links')) { * A string containing the image tag. */ function theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) { - if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) { - $attributes = drupal_attributes($attributes); - $url = (url($path) == $path) ? $path : (base_path() . $path); - return '' . check_plain($alt) . ''; + if ($getsize && (is_file($path))) { + list($width, $height, $type, $image_attributes) = @getimagesize($path); } + + $attributes = drupal_attributes($attributes); + $url = (url($path) == $path) ? $path : (base_path() . $path); + return '' . check_plain($alt) . ''; +} + +/** + * Return a themed image using a specific image preset. + * + * @param $preset + * The name of the preset to be used to alter the original image. + * @param $path + * The path of the image file relative to the Drupal files directory. + * This function does not work with images outside the files directory nor + * with remotely hosted images. + * @param $alt + * The alternative text for text-based browsers. + * @param $title + * The title text is displayed when the image is hovered in some popular browsers. + * @param $attributes + * Associative array of attributes to be placed in the img tag. + * @param $getsize + * If set to TRUE, the image's dimension are fetched and added as width/height attributes. + * @return + * A string containing the image tag. + */ +function theme_image_preset($preset, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) { + $real_path = image_preset_path($preset, $path); + $url_path = image_preset_path($preset, $path, FALSE); + + if (!file_exists($real_path)) { + $url_path = image_preset_generate($preset, $path); + } + + return theme('image', $url_path, $alt, $title, $attributes, $getsize); } /** diff --git modules/system/image.actions.inc modules/system/image.actions.inc new file mode 100644 index 0000000..b33b50e --- /dev/null +++ modules/system/image.actions.inc @@ -0,0 +1,302 @@ + array( + 'name' => t('Resize'), + 'description' => t('Resize an image to an exact set of dimensions, ignoring aspect ratio.'), + 'function' => 'system_image_resize_image', + 'form' => 'system_image_resize_form', + 'summary' => 'system_image_resize_summary', + ), + 'system_image_scale' => array( + 'name' => t('Scale'), + 'description' => t('Resize an image maintaining the original aspect-ratio (only one value necessary).'), + 'function' => 'system_image_scale_image', + 'form' => 'system_image_scale_form', + 'summary' => 'system_image_scale_summary', + ), + 'system_image_scale_and_crop' => array( + 'name' => t('Scale and Crop'), + 'description' => t('Resize an image maintaining the original aspect-ratio, then crop the center of the image to specific dimensions.'), + 'function' => 'system_image_scale_and_crop_image', + 'form' => 'system_image_resize_form', + 'summary' => 'system_image_resize_summary', + ), + 'system_image_crop' => array( + 'name' => t('Crop'), + 'description' => t('Crop an image to the rectangle specified by the given offsets and dimensions.'), + 'function' => 'system_image_crop_image', + 'form' => 'system_image_crop_form', + 'summary' => 'system_image_crop_summary', + ), + 'system_image_desaturate' => array( + 'name' => t('Desaturate'), + 'description' => t('Convert an image to grey scale.'), + 'function' => 'system_image_desaturate_image', + 'form' => 'system_image_desaturate_form', + 'summary' => 'system_image_desaturate_summary', + ), + 'system_image_rotate' => array( + 'name' => t('Rotate'), + 'description' => t('Rotate an image.'), + 'function' => 'system_image_rotate_image', + 'form' => 'system_image_rotate_form', + 'summary' => 'system_image_rotate_summary', + ), + ); + + return $actions; +} + +/** + * Image generation callback. Given a preset and image, request a derivative. + * + * This function is the default image generation method. It generates individual + * images by returning a path for an image that can be used in an tag. + * When the browser requests the image at system/image/[preset_name]/[path] the + * image is generated if it does not already exist and then served to the browser. + * This allows each image to have it's own PHP instance (and memory limit) for + * generation of the new images. + * + * @param $preset + * An image preset array. + * @param $path + * The local path of the image to be generated, excluding the files directory. + * @return + * TRUE if image derivative already exists. Callback path for image generation + * if derivative does not already exist. Suitale for use in an tag. + * @see system_image_preset_generate() + * @see image_preset_generate() + */ +function system_image_preset_request($preset, $path) { + $destination = image_preset_path($preset['name'], $path); + $image_url = image_preset_url($preset['name'], $path); + + // If the image already exists return true. + if (file_exists($destination)) { + return TRUE; + } + + // Generate a callback path for an image. + $token = drupal_get_token($path); + $url = url('system/image/' . $preset['name'] . '/' . $path, array('absolute' => TRUE, 'query' => array('token' => $token))); + return $url; +} + +/** + * Menu callback. Given a preset and image path, generate a derivative and + * serve transfer it to the requesting agent via file_transfer(). + */ +function system_image_preset_generate() { + $args = func_get_args(); + $preset_name = array_shift($args); + $preset = image_preset_load($preset_name); + $path = implode('/', $args); + $source = file_create_path($path); + $destination = image_preset_path($preset['name'], $path); + + if (!$preset || !isset($_GET['token'])) { + drupal_access_denied(); + exit(); + } + + if ($_GET['token'] != drupal_get_token($path)) { + drupal_access_denied(); + exit; + } + + $cid = md5($destination); + // Image generation is already in progress. + if (cache_get($cid, 'cache_image')) { + print 'IN PROGRESS'; + exit; + } + + cache_set($cid, $destination, 'cache_image'); + + if (image_preset_create_derivative($preset, $source, $destination)) { + $image = image_load($destination); + cache_clear_all($cid, 'cache_image'); + file_transfer($image->source, array('Content-type: '. $image->info['mime_type'])); + } + else { + cache_clear_all($cid, 'cache_image'); + print 'FALSE'; + } + + exit; +} + +/** + * Access callback for system/image. Ensure this request is made by Drupal. + */ +function system_image_preset_generate_access() { + $args = func_get_args(); + $preset_name = array_shift($args); + $path = implode('/', $args); + + return isset($_GET['token']) && drupal_valid_token($_GET['token'], $path); +} + +/** + * Default implementation image resize action. + * + * @param $image + * An image object. + * @param $data + * An array of attributes to use when performing the resize action. + * array('width' => int, 'height' => int) + * @return + * TRUE on success. FALSE on failure to resize image. + * @see image_resize() + */ +function system_image_resize_image(&$image, $data) { + if (!image_resize($image, $data['width'], $data['height'])) { + watchdog('image', 'Image resize failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; +} + +/** + * Default implementation of image scale action. + * + * @param $image + * An image object. + * @param $data + * An array of attributes to use when performing the scale action. + * array('width' => int, 'height' => int, 'upscale' => bool) + * @return + * TRUE on success. FALSE on failure to scale image. + * @see image_scale() + */ +function system_image_scale_image(&$image, $data) { + // Set impossibly large values if the width and height aren't set. + $data['width'] = $data['width'] ? $data['width'] : 9999999; + $data['height'] = $data['height'] ? $data['height'] : 9999999; + if (!image_scale($image, $data['width'], $data['height'], $data['upscale'])) { + watchdog('image', 'Image scale failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; +} + +/** + * Default implementation of image crop action. + * + * @param $image + * An image object. + * @param $data + * An array containting attributes to use when preforming the crop action. + * array('xoffset' => int, 'yoffset' => int, 'width' => int, 'height' => int) + * @return + * TRUE on success. FALSE on failure to crop image. + * @see image_crop() + */ +function system_image_crop_image(&$image, $data) { + if (!image_crop($image, $data['xoffset'], $data['yoffset'], $data['width'], $data['height'])) { + watchdog('image', 'Image crop failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; +} + +/** + * Default implementation of image scale and crop action. + * + * @param $image + * An image object. + * @param $data + * An array of attributes to use when performing the scale and cropt action. + * array('width' => int, 'height' => int) + * @return + * TRUE on success. FALSE on failure to scale and crop image. + * @see image_scale_and_crop() + */ +function system_image_scale_and_crop_image(&$image, $data) { + if (!image_scale_and_crop($image, $data['width'], $data['height'])) { + watchdog('image', t('Image scale and crop failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, TRUE))), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; +} + +/** + * Default implementation of image desaturate action. + * + * @param $image + * An image object. + * @param $data + * An array of attributes to use when performing the desaturate action. + * @return + * TRUE on success. FALSE on failure to desaturate image. + * @see image_desaturate() + */ +function system_image_desaturate_image(&$image, $data) { + if (!image_desaturate($image)) { + watchdog('image', 'Image desaturate failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, TRUE)), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; +} + +/** + * Default implementation of image rotate action. + * + * @param $image + * An image object. + * @param $data + * An array of attributes to use when performing the rotate action. + * array('degrees' => int, 'random' => bool, 'bgcolor' => string) + * @return + * TRUE on success. FALSE on failure to rotate image. + * @see image_rotate(). + */ +function system_image_rotate_image(&$image, $data) { + // Set sane default values. + $data['degrees'] = $data['degrees'] ? $data['degrees'] : 0; + $data['random'] = $data['random'] ? $data['random'] : FALSE; + $data['bgcolor'] = $data['bgcolor'] ? $data['bgcolor'] : '#FFFFFF'; + + // Manipulate the bgcolor if we need to randomize, and convert to proper colors. + $data['bgcolor'] = '0x'. str_replace('#', '', $data['bgcolor']); + + if (!empty($data['random'])) { + $degrees = abs((float)$data['degrees']); + $data['degrees'] = rand(-1 * $degrees, $degrees); + } + + if (!image_rotate($image, $data['degrees'], $data['bgcolor'])) { + watchdog('image', t('Image rotate failed. image: %image, data: %data.', array('%path' => $image, '%data' => print_r($data, TRUE))), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; +} diff --git modules/system/system.info modules/system/system.info index a5d3b83..55722da 100644 --- modules/system/system.info +++ modules/system/system.info @@ -8,6 +8,7 @@ files[] = system.module files[] = system.admin.inc files[] = system.queue.inc files[] = image.gd.inc +files[] = image.actions.inc files[] = system.install files[] = system.test required = TRUE diff --git modules/system/system.install modules/system/system.install index 2ddc214..8bdddab 100644 --- modules/system/system.install +++ modules/system/system.install @@ -606,6 +606,8 @@ function system_schema() { 'primary key' => array('cid'), ); + $schema['cache_image'] = $schema['cache']; + $schema['cache_image']['description'] = 'Cache table used to store information about in progress image manipulations.'; $schema['cache_form'] = $schema['cache']; $schema['cache_form']['description'] = 'Cache table for the form system to store recently built forms and their storage data, to be used in subsequent page requests.'; $schema['cache_page'] = $schema['cache']; @@ -748,6 +750,63 @@ function system_schema() { 'nid' => array('nid'), ), ); + + $schema['image_presets'] = array( + 'fields' => array( + 'ipid' => array( + 'description' => t('The primary identifier for an image preset.'), + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE), + 'name' => array( + 'description' => t('The primary identifier for a node.'), + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE), + ), + 'primary key' => array('ipid'), + 'indexes' => array( + 'name' => array('name'), + ), + ); + + $schema['image_actions'] = array( + 'fields' => array( + 'iaid' => array( + 'description' => t('The primary identifier for an image action.'), + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE), + 'ipid' => array( + 'description' => t('The primary identifier for an image preset.'), + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0), + 'weight' => array( + 'description' => t('The weight of the action in the preset.'), + 'type' => 'int', + 'unsigned' => FALSE, + 'not null' => TRUE, + 'default' => 0), + 'action' => array( + 'description' => t('The unique ID of the action to be executed.'), + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE), + 'data' => array( + 'description' => t('The configuration data for the action.'), + 'type' => 'text', + 'not null' => TRUE, + 'size' => 'big', + 'serialize' => TRUE), + ), + 'primary key' => array('iaid'), + 'indexes' => array( + 'ipid' => array('ipid'), + ), + ); + $schema['menu_router'] = array( 'description' => 'Maps paths to various callbacks (access, page and title)', 'fields' => array( @@ -3560,6 +3619,122 @@ function system_update_7027() { } /** + * Add tables for image presets and actions. + */ +function system_update_7028() { + $ret = array(); + $schema = array(); + + $schema['cache_image'] = array( + 'description' => 'Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.', + 'fields' => array( + 'cid' => array( + 'description' => 'Primary Key: Unique cache ID.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + 'data' => array( + 'description' => 'A collection of data to cache.', + 'type' => 'blob', + 'not null' => FALSE, + 'size' => 'big', + ), + 'expire' => array( + 'description' => 'A Unix timestamp indicating when the cache entry should expire, or 0 for never.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + 'created' => array( + 'description' => 'A Unix timestamp indicating when the cache entry was created.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + 'headers' => array( + 'description' => 'Any custom HTTP headers to be added to cached data.', + 'type' => 'text', + 'not null' => FALSE, + ), + 'serialized' => array( + 'description' => 'A flag to indicate whether content is serialized (1) or not (0).', + 'type' => 'int', + 'size' => 'small', + 'not null' => TRUE, + 'default' => 0, + ), + ), + 'indexes' => array( + 'expire' => array('expire'), + ), + 'primary key' => array('cid'), + ); + + $schema['image_presets'] = array( + 'fields' => array( + 'ipid' => array( + 'description' => t('The primary identifier for an image preset.'), + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE), + 'name' => array( + 'description' => t('The primary identifier for a node.'), + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE), + ), + 'primary key' => array('ipid'), + 'indexes' => array( + 'name' => array('name'), + ), + ); + + $schema['image_actions'] = array( + 'fields' => array( + 'iaid' => array( + 'description' => t('The primary identifier for an image action.'), + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE), + 'ipid' => array( + 'description' => t('The primary identifier for an image preset.'), + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0), + 'weight' => array( + 'description' => t('The weight of the action in the preset.'), + 'type' => 'int', + 'unsigned' => FALSE, + 'not null' => TRUE, + 'default' => 0), + 'action' => array( + 'description' => t('The unique ID of the action to be executed.'), + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE), + 'data' => array( + 'description' => t('The configuration data for the action.'), + 'type' => 'text', + 'not null' => TRUE, + 'size' => 'big', + 'serialize' => TRUE), + ), + 'primary key' => array('iaid'), + 'indexes' => array( + 'ipid' => array('ipid'), + ), + ); + + db_create_table($ret, 'cache_image', $schema['cache_image']); + db_create_table($ret, 'image_presets', $schema['image_presets']); + db_create_table($ret, 'image_actions', $schema['image_actions']); + return $ret; +} + +/** * @} End of "defgroup updates-6.x-to-7.x" * The next series of updates should start at 8000. */ diff --git modules/system/system.module modules/system/system.module index d81e171..1355587 100644 --- modules/system/system.module +++ modules/system/system.module @@ -476,6 +476,12 @@ function system_menu() { 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); + $items['system/image'] = array( + 'title' => 'Generate image preset', + 'page callback' => 'system_image_preset_generate', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); $items['admin'] = array( 'title' => 'Administer', 'access arguments' => array('access administration pages'), @@ -2513,3 +2519,46 @@ function system_image_toolkits() { ), ); } + +/** + * Implementation of hook_file_download(). + * + * Control the access to files underneath the system/files/presets directory. + */ +function system_file_download($filepath) { + if (strpos($filepath, 'presets/') === 0) { + $args = explode('/', $filepath); + array_shift($args); // Toss the "presets" item. + $preset_name = array_shift($args); + $original_path = implode('/', $args); + + // Check that the file exists and is an image. + if ($info = image_get_info(file_create_path($filepath))) { + // Check the permissions of the original image to grant access to this image. + $headers = module_invoke_all('file_download', $original_path); + if (!in_array(-1, $headers)) { + return array( + 'Content-Type: ' . $info['mime_type'], + 'Content-Length: ' . $info['file_size'], + ); + } + } + return -1; + } +} + +/** + * Implementation of hook_file_move(). + */ +function system_file_move($file, $source) { + // Delete any image derivatives at the original image path. + image_path_flush($file->filepath); +} + +/** + * Implementation of hook_file_delete(). + */ +function system_file_delete($file) { + // Delete any image derivatives of this image. + image_path_flush($file->filepath); +} diff --git modules/user/user.admin.inc modules/user/user.admin.inc index 19f861b..bac10ee 100644 --- modules/user/user.admin.inc +++ modules/user/user.admin.inc @@ -371,7 +371,7 @@ function user_admin_settings() { $form['personalization']['pictures']['user_picture_file_size'] = array( '#type' => 'textfield', '#title' => t('Picture maximum file size'), - '#default_value' => variable_get('user_picture_file_size', '30'), + '#default_value' => variable_get('user_picture_file_size', '800'), '#size' => 15, '#maxlength' => 10, '#description' => t('Maximum file size for pictures, in kB.'), diff --git modules/user/user.module modules/user/user.module index 4c3e98d..9c4f633 100644 --- modules/user/user.module +++ modules/user/user.module @@ -627,8 +627,8 @@ function user_validate_picture(&$form, &$form_state) { // If required, validate the uploaded picture. $validators = array( 'file_validate_is_image' => array(), - 'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')), - 'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024), + 'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '1024x1024')), + 'file_validate_size' => array(variable_get('user_picture_file_size', '800') * 1024), ); // Save the file as a temporary file. @@ -1201,7 +1201,12 @@ function template_preprocess_user_picture(&$variables) { } if (isset($filepath)) { $alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous')))); - $variables['picture'] = theme('image', $filepath, $alt, $alt, '', FALSE); + if ($preset = variable_get('user_picture_preset', '')) { + $variables['picture'] = theme('image_preset', $preset, $filepath, $alt, $alt, NULL, FALSE); + } + else { + $variables['picture'] = theme('image', $filepath, $alt, $alt, NULL, FALSE); + } if (!empty($account->uid) && user_access('access user profiles')) { $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE); $variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes); @@ -1911,7 +1916,7 @@ function user_edit_form(&$form_state, $uid, $edit, $register = FALSE) { '#type' => 'file', '#title' => t('Upload picture'), '#size' => 48, - '#description' => t('Your virtual face or picture. Maximum dimensions are %dimensions pixels and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) . ' ' . variable_get('user_picture_guidelines', ''), + '#description' => t('Your virtual face or picture. Maximum dimensions are %dimensions pixels and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '1024x1024'), '%size' => variable_get('user_picture_file_size', '800'))) . ' ' . variable_get('user_picture_guidelines', ''), ); $form['#validate'][] = 'user_validate_picture'; } @@ -2012,6 +2017,9 @@ function _user_cancel($edit, $account, $method) { db_delete('authmap') ->condition('uid', $account->uid) ->execute(); + if ($account->picture) { + file_delete($account->picture); + } drupal_set_message(t('%name has been deleted.', array('%name' => $account->name))); watchdog('user', 'Deleted user: %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE); break; diff --git modules/user/user.test modules/user/user.test index 9528c25..62f52d3 100644 --- modules/user/user.test +++ modules/user/user.test @@ -562,8 +562,14 @@ class UserPictureTestCase extends DrupalWebTestCase { // user's profile page. $text = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $test_dim)); $this->assertRaw($text, t('Image was resized.')); + if ($preset = variable_get('user_picture_preset', '')) { + $url_path = image_preset_path($preset, $pic_path, FALSE); + } + else { + $url_path = $pic_path; + } $alt = t("@user's picture", array('@user' => $this->user->name)); - $this->assertRaw(theme('image', $pic_path, $alt, $alt, '', FALSE), t("Image is displayed in user's edit page")); + $this->assertRaw(theme('image', $url_path, $alt, $alt, NULL, FALSE), t("Image is displayed in user's edit page")); // Check if file is located in proper directory. $this->assertTrue(is_file($pic_path), t("File is located in proper directory")); @@ -696,8 +702,14 @@ class UserPictureTestCase extends DrupalWebTestCase { $pic_path = $this->saveUserPicture($image); // Check if image is displayed in user's profile page. + if ($preset = variable_get('user_picture_preset', '')) { + $url_path = image_preset_path($preset, $pic_path, FALSE); + } + else { + $url_path = $pic_path; + } $this->drupalGet('user'); - $this->assertRaw($pic_path, t("Image is displayed in user's profile page")); + $this->assertRaw($url_path, t("Image is displayed in user's profile page")); // Check if file is located in proper directory. $this->assertTrue(is_file($pic_path), t('File is located in proper directory')); diff --git profiles/default/default.profile profiles/default/default.profile index c4e0f51..5327126 100644 --- profiles/default/default.profile +++ profiles/default/default.profile @@ -196,6 +196,25 @@ function default_profile_tasks(&$task, $url) { // Don't display date and author information for page nodes by default. variable_set('node_submitted_page', FALSE); + // Create an image preset. + $preset = array('name' => 'thumbnail_square'); + $preset = image_preset_save($preset); + $action = array( + 'ipid' => $preset['ipid'], + 'action' => 'system_image_scale_and_crop', + 'data' => array('width' => '85', 'height' => '85'), + ); + image_action_save($action); + + // Enable user picture support and set the default to a square thumbnail option. + variable_set('user_pictures', '1'); + variable_set('user_picture_preset', 'thumbnail_square'); + + $theme_settings = theme_get_settings(); + $theme_settings['toggle_node_user_picture'] = '1'; + $theme_settings['toggle_comment_user_picture'] = '1'; + variable_set('theme_settings', $theme_settings); + // Create a default vocabulary named "Tags", enabled for the 'article' content type. $description = st('Use tags to group articles on similar topics into categories.'); $help = st('Enter a comma-separated list of words.');