updated core to 7.58 (right after the site was hacked)
This commit is contained in:
19
sites/all/modules/examples/image_example/image_example.info
Normal file
19
sites/all/modules/examples/image_example/image_example.info
Normal file
@ -0,0 +1,19 @@
|
||||
name = Image Example
|
||||
description = Example implementation of image.module hooks.
|
||||
package = Example modules
|
||||
core = 7.x
|
||||
; Since someone might install our module through Composer, we want to be sure
|
||||
; that the Drupal Composer facade knows we're specifying a core module rather
|
||||
; than a project. We do this by namespacing the dependency name with drupal:.
|
||||
dependencies[] = drupal:image
|
||||
; Since the namespacing feature is new as of Drupal 7.40, we have to require at
|
||||
; least that version of core.
|
||||
dependencies[] = drupal:system (>= 7.40)
|
||||
files[] = image_example.test
|
||||
|
||||
; Information added by Drupal.org packaging script on 2017-01-10
|
||||
version = "7.x-1.x-dev"
|
||||
core = "7.x"
|
||||
project = "examples"
|
||||
datestamp = "1484076787"
|
||||
|
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Install, update, and uninstall functions for the image_example module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
function image_example_install() {
|
||||
// Set a variable containing the name of the style to use when the module
|
||||
// outputs an image.
|
||||
variable_set('image_example_style_name', 'image_example_style');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_uninstall().
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
function image_example_uninstall() {
|
||||
variable_del('image_example_style_name');
|
||||
variable_del('image_example_image_fid');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_enable().
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
function image_example_enable() {
|
||||
// There is currently no way to manually flush an image style which causes
|
||||
// problems when installing a new module that implements
|
||||
// hook_image_styles_alter(). If the new module modifies an image style that
|
||||
// modification will not be applied to any images that have already been
|
||||
// generated unless the styles are flushed. This is one way around that.
|
||||
$styles = image_styles();
|
||||
foreach ($styles as $style) {
|
||||
image_style_flush($style);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_disable().
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
function image_example_disable() {
|
||||
// Solves the same problem as image_example_enable().
|
||||
image_example_enable();
|
||||
}
|
378
sites/all/modules/examples/image_example/image_example.module
Normal file
378
sites/all/modules/examples/image_example/image_example.module
Normal file
@ -0,0 +1,378 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Module file for image_example
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup image_example Example: Image
|
||||
* @ingroup examples
|
||||
* @{
|
||||
* Demonstrates the basic use of image API.
|
||||
*
|
||||
* This module demonstrates the use of Drupal 7's new image styles and effects
|
||||
* including the following topics.
|
||||
* - Define default image styles in code. Useful for modules that want to ship
|
||||
* with predefined image styles and for site developers who want their image
|
||||
* style configurations to be in version control.
|
||||
* hook_image_default_styles().
|
||||
* - Define new image effects. Demonstrates how a module can add additional
|
||||
* effects to the options available when creating image styles.
|
||||
* hook_image_effect_info().
|
||||
* - Alter existing image styles. Demonstrates the use of
|
||||
* hook_image_styles_alter() to modify existing image effects, especially
|
||||
* those defined by other modules in hook_image_default_styles() without
|
||||
* having to override the styles.
|
||||
* - Demonstrates the use 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 field of type #managed_file that allows the user
|
||||
* to upload an image and choose a style to use when displaying that image.
|
||||
* - Demonstrates the use of theme_image_style() to display images using an
|
||||
* image style.
|
||||
*
|
||||
* @see hook_image_default_styles().
|
||||
* @see hook_image_effect_info().
|
||||
* @see hook_image_style_save().
|
||||
* @see hook_image_style_delete().
|
||||
* @see theme_image_style().
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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' => 'drupal_get_form',
|
||||
'page arguments' => array('image_example_style_form'),
|
||||
'access arguments' => array('access content'),
|
||||
'file' => 'image_example.pages.inc',
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function image_example_help($path) {
|
||||
switch ($path) {
|
||||
case 'image_example/styles':
|
||||
$output = '<p>' . t('Use this form to upload an image and choose an Image Style to use when displaying the image. This demonstrates basic use of the Drupal 7 Image styles & effects system.') . '</p>';
|
||||
$output .= '<p>' . t('Image styles can be added/edited using the !link.', array('!link' => l(t('Image styles UI'), 'admin/config/media/image-styles'))) . '</p>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_image_default_styles().
|
||||
*
|
||||
* hook_image_default_styles() declares to Drupal any image styles that are
|
||||
* provided by the module. An image style is a collection of image effects that
|
||||
* are performed in a specified order, manipulating the image and generating a
|
||||
* new derivative image.
|
||||
*
|
||||
* This hook can be used to declare image styles that your module depends on or
|
||||
* allow you to define image styles in code and gain the benefits of using
|
||||
* a version control system.
|
||||
*/
|
||||
function image_example_image_default_styles() {
|
||||
// This hook returns an array, each component of which describes an image
|
||||
// style. The array keys are the machine-readable image style names and
|
||||
// to avoid namespace conflicts should begin with the name of the
|
||||
// implementing module. e.g.) 'mymodule_stylename'. Styles names should
|
||||
// use only alpha-numeric characters, underscores (_), and hyphens (-).
|
||||
$styles = array();
|
||||
$styles['image_example_style'] = array();
|
||||
|
||||
// Each style array consists of an 'effects' array that is made up of
|
||||
// sub-arrays which define the individual image effects that are combined
|
||||
// together to create the image style.
|
||||
$styles['image_example_style']['effects'] = array(
|
||||
array(
|
||||
// Name of the image effect. See image_image_effect_info() in
|
||||
// modules/image/image.effects.inc for a list of image effects available
|
||||
// in Drupal 7 core.
|
||||
'name' => 'image_scale',
|
||||
// Arguments to pass to the effect callback function.
|
||||
// The arguments that an effect accepts are documented with each
|
||||
// individual image_EFFECT_NAME_effect function. See image_scale_effect()
|
||||
// for an example.
|
||||
'data' => array(
|
||||
'width' => 100,
|
||||
'height' => 100,
|
||||
'upscale' => 1,
|
||||
),
|
||||
// The order in which image effects should be applied when using this
|
||||
// style.
|
||||
'weight' => 0,
|
||||
),
|
||||
// Add a second effect to this image style. Effects are executed in order
|
||||
// and are cumulative. When applying an image style to an image the result
|
||||
// will be the combination of all effects associated with that style.
|
||||
array(
|
||||
'name' => 'image_example_colorize',
|
||||
'data' => array(
|
||||
'color' => '#FFFF66',
|
||||
),
|
||||
'weight' => 1,
|
||||
),
|
||||
);
|
||||
|
||||
return $styles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_image_style_save().
|
||||
*
|
||||
* Allows modules to respond to updates to an image style's
|
||||
* settings.
|
||||
*/
|
||||
function image_example_image_style_save($style) {
|
||||
// The $style parameter is an image style array with one notable exception.
|
||||
// When a user has chosen to replace a deleted style with another style the
|
||||
// $style['name'] property contains the name of the replacement style and
|
||||
// $style['old_name'] contains the name of the style being deleted.
|
||||
//
|
||||
// Here we update a variable that contains the name of the image style that
|
||||
// the block provided by this module uses when formatting images to use the
|
||||
// new user chosen style name.
|
||||
if (isset($style['old_name']) && $style['old_name'] == variable_get('image_example_style_name', '')) {
|
||||
variable_set('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) {
|
||||
// See information about $style paramater in documentation for
|
||||
// image_example_style_save().
|
||||
//
|
||||
// Update the modules variable that contains the name of the image style
|
||||
// being deleted to the name of the replacement style.
|
||||
if (isset($style['old_name']) && $style['old_name'] == variable_get('image_example_style_name', '')) {
|
||||
variable_set('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) {
|
||||
// Empty any caches populated by our module that could contain stale data
|
||||
// after the style has been flushed. Stale data occurs because the module may
|
||||
// have cached content with a reference to the derivative image which is
|
||||
// being deleted.
|
||||
cache_clear_all('*', 'image_example', TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 overriden by the user. Altering styles that have been
|
||||
* overriden by the user could have an adverse affect 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 immediatly be re-applied.
|
||||
*/
|
||||
function image_example_image_styles_alter(&$styles) {
|
||||
// The $styles paramater is an array of image style arrays keyed by style
|
||||
// name. You can check to see if a style has been overriden by checking the
|
||||
// $styles['stylename']['storage'] property.
|
||||
// Verify that the effect has not been overriden.
|
||||
if ($styles['thumbnail']['storage'] == IMAGE_STORAGE_DEFAULT) {
|
||||
// Add an additional colorize effect to the system provided thumbnail
|
||||
// effect.
|
||||
$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();
|
||||
|
||||
// The array is keyed on the machine-readable effect name.
|
||||
$effects['image_example_colorize'] = array(
|
||||
// Human readable name of the effect.
|
||||
'label' => t('Colorize'),
|
||||
// (optional) Brief description of the effect that will be shown when
|
||||
// adding or configuring this image effect.
|
||||
'help' => t('The colorize effect will first remove all color from the source image and then tint the image using the color specified.'),
|
||||
// Name of function called to perform this effect.
|
||||
'effect callback' => 'image_example_colorize_effect',
|
||||
// (optional) Name of function that provides a $form array with options for
|
||||
// configuring the effect. Note that you only need to return the fields
|
||||
// specific to your module. Submit buttons will be added automatically, and
|
||||
// configuration options will be serailized and added to the 'data' element
|
||||
// of the effect. The function will recieve the $effect['data'] array as
|
||||
// its only parameter.
|
||||
'form callback' => 'image_example_colorize_form',
|
||||
// (optional) Name of a theme function that will output a summary of this
|
||||
// effects configuation. Used when displaying list of effects associated
|
||||
// with an image style. In this example the function
|
||||
// theme_image_example_colorize_summary will be called via the theme()
|
||||
// function. Your module must also implement hook_theme() in order for this
|
||||
// function to work correctly. See image_example_theme() and
|
||||
// theme_image_example_colorize_summary().
|
||||
'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();
|
||||
// You do not need to worry about handling saving/updating/deleting of the
|
||||
// data collected. The image module will automatically serialize and store
|
||||
// all data associated with an effect.
|
||||
$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) {
|
||||
// Image manipulation should be done to the $image->resource, which will be
|
||||
// automatically saved as a new image once all effects have been applied.
|
||||
// If your effect makes changes to the $image->resource that relate to any
|
||||
// information stored in the $image->info array (width, height, etc.) you
|
||||
// should update that information as well. See modules/system/image.gd.inc
|
||||
// for examples of functions that perform image manipulations.
|
||||
//
|
||||
// Not all GD installations are created equal. It is a good idea to check for
|
||||
// the existence of image manipulation functions before using them.
|
||||
// PHP installations using non-bundled GD do not have imagefilter(). More
|
||||
// information about image manipulation functions is available in the PHP
|
||||
// manual. http://www.php.net/manual/en/book.image.php
|
||||
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('%file' => $image->source));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Verify that Drupal is using the PHP GD library for image manipulations
|
||||
// since this effect depends on functions in the GD library.
|
||||
if ($image->toolkit != 'gd') {
|
||||
watchdog('image', 'Image colorize failed on %path. Using non GD toolkit.', array('%path' => $image->source), WATCHDOG_ERROR);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Convert short #FFF syntax to full #FFFFFF syntax.
|
||||
if (strlen($data['color']) == 4) {
|
||||
$c = $data['color'];
|
||||
$data['color'] = $c[0] . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3];
|
||||
}
|
||||
|
||||
// Convert #FFFFFF syntax to hexadecimal colors.
|
||||
$data['color'] = hexdec(str_replace('#', '0x', $data['color']));
|
||||
|
||||
// Convert the hexadecimal color value to a color index value.
|
||||
$rgb = array();
|
||||
for ($i = 16; $i >= 0; $i -= 8) {
|
||||
$rgb[] = (($data['color'] >> $i) & 0xFF);
|
||||
}
|
||||
|
||||
// First desaturate the image, and then apply the new color.
|
||||
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".
|
||||
*/
|
166
sites/all/modules/examples/image_example/image_example.pages.inc
Normal file
166
sites/all/modules/examples/image_example/image_example.pages.inc
Normal file
@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Page/form showing image styles in use.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Form for uploading and displaying an image using selected style.
|
||||
*
|
||||
* This page provides a form that allows the user to upload an image and choose
|
||||
* a style from the list of defined image styles to use when displaying the
|
||||
* the image. This serves as an example of integrating image styles with your
|
||||
* module and as a way to demonstrate that the styles and effects defined by
|
||||
* this module are available via Drupal's image handling system.
|
||||
*
|
||||
* @see theme_image_style()
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
function image_example_style_form($form, &$form_state) {
|
||||
// If there is already an uploaded image display the image here.
|
||||
if ($image_fid = variable_get('image_example_image_fid', FALSE)) {
|
||||
$image = file_load($image_fid);
|
||||
$style = variable_get('image_example_style_name', 'thumbnail');
|
||||
$form['image'] = array(
|
||||
'#markup' => theme('image_example_image', array('image' => $image, 'style' => $style)),
|
||||
);
|
||||
}
|
||||
|
||||
// Use the #managed_file FAPI element to upload an image file.
|
||||
$form['image_example_image_fid'] = array(
|
||||
'#title' => t('Image'),
|
||||
'#type' => 'managed_file',
|
||||
'#description' => t('The uploaded image will be displayed on this page using the image style chosen below.'),
|
||||
'#default_value' => variable_get('image_example_image_fid', ''),
|
||||
'#upload_location' => 'public://image_example_images/',
|
||||
);
|
||||
|
||||
// Provide a select field for choosing an image style to use when displaying
|
||||
// the image.
|
||||
$form['image_example_style_name'] = array(
|
||||
'#title' => t('Image style'),
|
||||
'#type' => 'select',
|
||||
'#description' => t('Choose an image style to use when displaying this image.'),
|
||||
// The image_style_options() function returns an array of all available
|
||||
// image styles both the key and the value of the array are the image
|
||||
// style's name. The function takes on paramater, a boolean flag
|
||||
// signifying whether or not the array should include a <none> option.
|
||||
'#options' => image_style_options(TRUE),
|
||||
'#default_value' => variable_get('image_example_style_name', ''),
|
||||
);
|
||||
|
||||
// Submit Button.
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Save'),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the user supplied an image with the form..
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
function image_example_style_form_validate($form, &$form_state) {
|
||||
if (!isset($form_state['values']['image_example_image_fid']) || !is_numeric($form_state['values']['image_example_image_fid'])) {
|
||||
form_set_error('image_example_image_fid', t('Please select an image to upload.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Form Builder; Display a form for uploading an image.
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
function image_example_style_form_submit($form, &$form_state) {
|
||||
// When using the #managed_file form element the file is automatically
|
||||
// uploaded an saved to the {file} table. The value of the corresponding
|
||||
// form element is set to the {file}.fid of the new file.
|
||||
//
|
||||
// If fid is not 0 we have a valid file.
|
||||
if ($form_state['values']['image_example_image_fid'] != 0) {
|
||||
// The new file's status is set to 0 or temporary and in order to ensure
|
||||
// that the file is not removed after 6 hours we need to change it's status
|
||||
// to 1. Save the ID of the uploaded image for later use.
|
||||
$file = file_load($form_state['values']['image_example_image_fid']);
|
||||
$file->status = FILE_STATUS_PERMANENT;
|
||||
file_save($file);
|
||||
|
||||
// When a module is managing a file, it must manage the usage count.
|
||||
// Here we increment the usage count with file_usage_add().
|
||||
file_usage_add($file, 'image_example', 'sample_image', 1);
|
||||
|
||||
// Save the fid of the file so that the module can reference it later.
|
||||
variable_set('image_example_image_fid', $file->fid);
|
||||
drupal_set_message(t('The image @image_name was uploaded and saved with an ID of @fid and will be displayed using the style @style.',
|
||||
array(
|
||||
'@image_name' => $file->filename,
|
||||
'@fid' => $file->fid,
|
||||
'@style' => $form_state['values']['image_example_style_name'],
|
||||
)
|
||||
));
|
||||
}
|
||||
// If the file was removed we need to remove the module's reference to the
|
||||
// removed file's fid, and remove the file.
|
||||
elseif ($form_state['values']['image_example_image_fid'] == 0) {
|
||||
// Retrieve the old file's id.
|
||||
$fid = variable_get('image_example_image_fid', FALSE);
|
||||
$file = $fid ? file_load($fid) : FALSE;
|
||||
if ($file) {
|
||||
// When a module is managing a file, it must manage the usage count.
|
||||
// Here we decrement the usage count with file_usage_delete().
|
||||
file_usage_delete($file, 'image_example', 'sample_image', 1);
|
||||
|
||||
// The file_delete() function takes a file object and checks to see if
|
||||
// the file is being used by any other modules. If it is the delete
|
||||
// operation is cancelled, otherwise the file is deleted.
|
||||
file_delete($file);
|
||||
}
|
||||
|
||||
// Either way the module needs to update it's reference since even if the
|
||||
// file is in use by another module and not deleted we no longer want to
|
||||
// use it.
|
||||
variable_set('image_example_image_fid', FALSE);
|
||||
drupal_set_message(t('The image @image_name was removed.', array('@image_name' => $file->filename)));
|
||||
}
|
||||
|
||||
// Save the name of the image style chosen by the user.
|
||||
variable_set('image_example_style_name', $form_state['values']['image_example_style_name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Theme function displays an image rendered using the specified style.
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
function theme_image_example_image($variables) {
|
||||
$image = $variables['image'];
|
||||
$style = $variables['style'];
|
||||
|
||||
// theme_image_style() is the primary method for displaying images using
|
||||
// one of the defined styles. The $variables array passed to the theme
|
||||
// contains the following two important values:
|
||||
// - 'style_name': the name of the image style to use when displaying the
|
||||
// image.
|
||||
// - 'path': the $file->uri of the image to display.
|
||||
//
|
||||
// When given a style and an image path the function will first determine
|
||||
// if a derivative image already exists, in which case the existing image
|
||||
// will be displayed. If the derivative image does not already exist the
|
||||
// function returns an <img> tag with a specially crafted callback URL
|
||||
// as the src attribute for the tag. When accessed, the callback URL will
|
||||
// generate the derivative image and serve it to the browser.
|
||||
$output = theme('image_style',
|
||||
array(
|
||||
'style_name' => $style,
|
||||
'path' => $image->uri,
|
||||
'getsize' => FALSE,
|
||||
)
|
||||
);
|
||||
$output .= '<p>' . t('This image is being displayed using the image style %style_name.', array('%style_name' => $style)) . '</p>';
|
||||
return $output;
|
||||
}
|
111
sites/all/modules/examples/image_example/image_example.test
Normal file
111
sites/all/modules/examples/image_example/image_example.test
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Test case for testing the image example module.
|
||||
*
|
||||
* This file contains the tests cases to check if the module is performing as
|
||||
* expected.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Functional tests for the Image Example module.
|
||||
*
|
||||
* @ingroup image_example
|
||||
*/
|
||||
class ImageExampleTestCase extends DrupalWebTestCase {
|
||||
protected $webUser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Image example functionality',
|
||||
'description' => 'Test functionality of the Image Example module.',
|
||||
'group' => 'Examples',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable modules and create user with specific permissions.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp('image_example');
|
||||
// Create user with permission to administer image styles.
|
||||
$this->webUser = $this->drupalCreateUser(array('administer image styles', 'administer blocks'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test implementations of image API hooks.
|
||||
*/
|
||||
public function testImageExample() {
|
||||
// Login the admin user.
|
||||
$this->drupalLogin($this->webUser);
|
||||
|
||||
// Verify that the default style added by
|
||||
// image_example_image_default_styles() is in the list of image styles.
|
||||
$image_styles = image_styles();
|
||||
$this->assertTrue(isset($image_styles['image_example_style']), 'The default style image_example_style is in the list of image styles.');
|
||||
|
||||
// Verify that the effect added to the default 'thumbnail' style by
|
||||
// image_example_image_styles_alter() is present.
|
||||
$this->assertTrue((isset($image_styles['thumbnail']['effects'][1]['name']) && $image_styles['thumbnail']['effects'][1]['name'] == 'image_example_colorize'), 'Effect added to the thumbnail style via hook_image_styles_alter() is present.');
|
||||
|
||||
// Create a new image style and add the effect provided by
|
||||
// image_example_effect_info().
|
||||
$new_style = array('name' => drupal_strtolower($this->randomName()));
|
||||
$new_style = image_style_save($new_style);
|
||||
$this->assertTrue(isset($new_style['isid']), format_string('Image style @style_name created.', array('@style_name' => $new_style['name'])));
|
||||
|
||||
$edit = array(
|
||||
'new' => 'image_example_colorize',
|
||||
);
|
||||
$this->drupalPost('admin/config/media/image-styles/edit/' . $new_style['name'], $edit, t('Add'));
|
||||
|
||||
// Verify the 'color' field provided by image_example_colorize_form()
|
||||
// appears on the effect configuration page. And that we can fill it out.
|
||||
$this->assertField('data[color]', 'Color field provided by image_example_effect_colorize_form is present on effect configuration page.');
|
||||
$edit = array(
|
||||
'data[color]' => '#000000',
|
||||
);
|
||||
$this->drupalPost(NULL, $edit, t('Add effect'));
|
||||
$this->assertText(t('The image effect was successfully applied.'), format_string('Colorize effect added to @style_name.', array('@style_name' => $new_style['name'])));
|
||||
|
||||
// Set the variable 'image_example_style_name' to the name of our new style
|
||||
// then rename the style and ensure the variable name is changed.
|
||||
// @todo Enable this block once http://drupal.org/node/713872 is fixed.
|
||||
if (defined('bug_713872_fixed')) {
|
||||
$style = image_style_load($new_style['name']);
|
||||
variable_set('image_example_style_name', $style['name']);
|
||||
$style['name'] = drupal_strtolower($this->randomName());
|
||||
$style = image_style_save($style);
|
||||
$variable = variable_get('image_example_style_name', '');
|
||||
$this->assertTrue(($variable == $style['name']), 'Variable image_example_style_name successfully updated when renaming image style.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for image block provided by module.
|
||||
*/
|
||||
public function testImageExamplePage() {
|
||||
// Login the admin user.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->drupalCreateNode(array('promote' => 1));
|
||||
|
||||
// Upload an image to the image page.
|
||||
$images = $this->drupalGetTestFiles('image');
|
||||
$edit = array(
|
||||
'files[image_example_image_fid]' => drupal_realpath($images[0]->uri),
|
||||
'image_example_style_name' => 'image_example_style',
|
||||
);
|
||||
$this->drupalPost('image_example/styles', $edit, t('Save'));
|
||||
$this->assertText(t('The image @image_name was uploaded', array('@image_name' => $images[0]->filename)), 'Image uploaded to image block.');
|
||||
|
||||
// Verify the image is displayed.
|
||||
$this->drupalGet('image_example/styles');
|
||||
$fid = variable_get('image_example_image_fid', FALSE);
|
||||
$image = isset($fid) ? file_load($fid) : NULL;
|
||||
$this->assertRaw(file_uri_target($image->uri), 'Image is displayed');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user