- <?php
- * @file
- * Hooks implementations for the Image Example module.
- */
-
- * @defgroup image_example Example: Image
- * @ingroup examples
- * @{
- * This example demonstrates the basic usage of the image API.
- *
- * This module demonstrates the use of image styles and effects, including the
- * following topics.
- * - Demonstrate how a module can add additional effects to the options
- * available when creating image styles.
- * - Demonstrate the usage of hook_image_styles_alter() to modify existing
- * image effects without having to override the styles.
- * - Demonstrate the usage of hook_image_style_save() and
- * hook_image_style_delete() to update module specific variables when an
- * image style is either re-named or deleted.
- * - Generate a form with a #managed_file field which allows users to upload an
- * image and choose a style to display that image.
- * - Demonstrate the usage of theme_image_style() to display images using an
- * image style.
- *
- * @see hook_image_effect_info().
- * @see hook_image_style_save().
- * @see hook_image_style_delete().
- * @see theme_image_style().
- */
-
-
- * Implements hook_config_info().
- */
- function image_example_config_info() {
- $prefixes['image_example.settings'] = array(
- 'label' => t('Image Example settings'),
- 'group' => t('Configuration'),
- );
- $prefixes['image.style.image_example_style'] = array(
- 'label' => t('Image example custom image style'),
- 'group' => t('Configuration'),
- );
- return $prefixes;
- }
-
- * Implements hook_menu().
- *
- * Provide a menu item and a page to demonstrate features of this example
- * module.
- */
- function image_example_menu() {
- $items = array();
- $items['image_example/styles'] = array(
- 'title' => 'Image Example',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array('image_example_style_form'),
- 'access arguments' => array('access content'),
- 'file' => 'image_example.pages.inc',
- );
- return $items;
- }
-
- * Implements hook_image_style_save().
- *
- * Allows modules to respond to updates to an image style's
- * settings.
- */
- function image_example_image_style_save($style) {
-
-
-
-
-
-
-
-
- if (isset($style['old_name']) && $style['old_name'] == config_get('image_example.settings', 'image_example_style_name')) {
- config_set('image_example.settings', 'image_example_style_name', $style['name']);
- }
- }
-
- * Implements hook_image_style_delete().
- *
- * This hook allows modules to respond to image styles being deleted.
- *
- * @see image_example_style_save()
- */
- function image_example_image_style_delete($style) {
-
-
-
-
-
- if (isset($style['old_name']) && $style['old_name'] == config_get('image_example.settings', 'image_example_style_name')) {
- config_set('image_example.settings', 'image_example_style_name', $style['name']);
- }
- }
-
- * Implements hook_image_style_flush().
- *
- * This hook allows modules to respond when a style is being flushed. Styles
- * are flushed any time a style is updated, an effect associated with the style
- * is updated, a new effect is added to the style, or an existing effect is
- * removed.
- *
- * Flushing removes all images generated using this style from the host. Once a
- * style has been flushed derivative images will need to be regenerated. New
- * images will be generated automatically as needed but it is worth noting that
- * on a busy site with lots of images this could have an impact on performance.
- *
- * Note: This function does not currently have any effect as the example module
- * does not use any caches. It is demonstrated here for completeness sake only.
- */
- function image_example_style_flush($style) {
-
-
-
-
- cache_flush('image_example');
- }
-
- * Implements hook_image_styles_alter().
- *
- * Allows your module to modify, add, or remove image styles provided
- * by other modules. The best use of this hook is to modify default styles that
- * have not been overridden by the user. Altering styles that have been
- * overridden by the user could have an adverse effect on the user experience.
- * If you add an effect to a style through this hook and the user attempts to
- * remove the effect it will immediately be re-applied.
- */
- function image_example_image_styles_alter(&$styles) {
-
-
-
-
- if ($styles['thumbnail']['storage'] == IMAGE_STORAGE_DEFAULT) {
-
-
- $styles['thumbnail']['effects'][] = array(
- 'label' => t('Colorize #FFFF66'),
- 'name' => 'image_example_colorize',
- 'effect callback' => 'image_example_colorize_effect',
- 'data' => array(
- 'color' => '#FFFF66',
- ),
- 'weight' => 1,
- );
- }
- }
-
- * Implements hook_image_effect_info().
- *
- * This hook allows your module to define additional image manipulation effects
- * that can be used with image styles.
- */
- function image_example_image_effect_info() {
- $effects = array();
-
-
- $effects['image_example_colorize'] = array(
-
- 'label' => t('Colorize'),
-
-
- 'help' => t('The colorize effect will first remove all color from the source image and then tint the image using the color specified.'),
-
- 'effect callback' => 'image_example_colorize_effect',
-
-
-
-
-
-
- 'form callback' => 'image_example_colorize_form',
-
-
-
-
-
-
-
- 'summary theme' => 'image_example_colorize_summary',
- );
-
- return $effects;
- }
-
- * Form Builder; Configuration settings for colorize effect.
- *
- * Create a $form array with the fields necessary for configuring the
- * image_example_colorize effect.
- *
- * Note that this is not a complete form, it only contains the portion of the
- * form for configuring the colorize options. Therefore it does not not need to
- * include metadata about the effect, nor a submit button.
- *
- * @param array $data
- * The current configuration for this colorize effect.
- */
- function image_example_colorize_form($data) {
- $form = array();
-
-
-
- $form['color'] = array(
- '#type' => 'textfield',
- '#title' => t('Color'),
- '#description' => t('The color to use when colorizing the image. Use web-style hex colors. e.g.) #FF6633.'),
- '#default_value' => isset($data['color']) ? $data['color'] : '',
- '#size' => 7,
- '#max_length' => 7,
- '#required' => TRUE,
- );
- return $form;
- }
-
- * Image effect callback; Colorize an image resource.
- *
- * @param object $image
- * An image object returned by image_load().
- * @param array $data
- * An array of attributes to use when performing the colorize effect with the
- * following items:
- * - "color": The web-style hex color to use when colorizing the image.
- *
- * @return bool
- * TRUE on success. FALSE on failure to colorize image.
- */
- function image_example_colorize_effect(&$image, $data) {
-
-
-
-
-
-
-
-
-
-
-
-
- if (!function_exists('imagefilter')) {
- watchdog('image', 'The image %image could not be colorized because the imagefilter() function is not available in this PHP installation.', array('%image' => $image->source));
- return FALSE;
- }
-
-
-
- if ($image->toolkit != 'gd') {
- watchdog('image', 'Image colorize failed on %path. Using non GD toolkit.', array('%path' => $image->source), WATCHDOG_ERROR);
- return FALSE;
- }
-
-
- if (strlen($data['color']) == 4) {
- $c = $data['color'];
- $data['color'] = $c[0] . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3];
- }
-
-
- $data['color'] = hexdec(str_replace('#', '0x', $data['color']));
-
-
- $rgb = array();
- for ($i = 16; $i >= 0; $i -= 8) {
- $rgb[] = (($data['color'] >> $i) & 0xFF);
- }
-
-
- imagefilter($image->resource, IMG_FILTER_GRAYSCALE);
- imagefilter($image->resource, IMG_FILTER_COLORIZE, $rgb[0], $rgb[1], $rgb[2]);
-
- return TRUE;
- }
-
- * Implements hook_theme().
- */
- function image_example_theme() {
- return array(
- 'image_example_colorize_summary' => array(
- 'variables' => array('data' => NULL),
- ),
- 'image_example_image' => array(
- 'variables' => array('image' => NULL, 'style' => NULL),
- 'file' => 'image_example.pages.inc',
- ),
- );
- }
-
- * Formats a summary of an image colorize effect.
- *
- * @param array $variables
- * An associative array containing:
- * - data: The current configuration for this colorize effect.
- */
- function theme_image_example_colorize_summary($variables) {
- $data = $variables['data'];
- return t('as color #@color.', array('@color' => $data['color']));
- }
- * @} End of "defgroup image_example".
- */