2009-07-21 07:09:47 +00:00
< ? php
/**
* @ file
* Administration pages for image settings .
*/
/**
* Menu callback ; Listing of all current image styles .
*/
function image_style_list () {
$page = array ();
$styles = image_styles ();
$page [ 'image_style_list' ] = array (
2009-10-09 01:00:08 +00:00
'#markup' => theme ( 'image_style_list' , array ( 'styles' => $styles )),
2009-09-05 15:05:05 +00:00
'#attached' => array (
2010-07-30 02:47:28 +00:00
'css' => array ( drupal_get_path ( 'module' , 'image' ) . '/image.admin.css' => array ()),
2009-09-05 15:05:05 +00:00
),
2009-07-21 07:09:47 +00:00
);
return $page ;
}
/**
* Form builder ; Edit an image style name and effects order .
*
* @ param $form_state
* An associative array containing the current state of the form .
* @ param $style
* An image style array .
* @ ingroup forms
* @ see image_style_form_submit ()
* @ see image_style_name_validate ()
*/
2009-09-18 00:12:48 +00:00
function image_style_form ( $form , & $form_state , $style ) {
2009-08-23 02:19:02 +00:00
$title = t ( 'Edit %name style' , array ( '%name' => $style [ 'name' ]));
drupal_set_title ( $title , PASS_THROUGH );
2009-07-21 07:09:47 +00:00
$form_state [ 'image_style' ] = $style ;
2009-09-18 00:12:48 +00:00
$form [ '#tree' ] = TRUE ;
2010-07-30 02:47:28 +00:00
$form [ '#attached' ][ 'css' ][ drupal_get_path ( 'module' , 'image' ) . '/image.admin.css' ] = array ();
2009-07-21 07:09:47 +00:00
2009-10-16 00:52:46 +00:00
// Show the thumbnail preview.
$form [ 'preview' ] = array (
'#type' => 'item' ,
'#title' => t ( 'Preview' ),
'#markup' => theme ( 'image_style_preview' , array ( 'style' => $style )),
2009-07-21 07:09:47 +00:00
);
2011-12-02 15:06:52 +00:00
$form [ 'name' ] = array (
'#type' => 'textfield' ,
'#size' => '64' ,
'#title' => t ( 'Image style name' ),
'#default_value' => $style [ 'name' ],
'#description' => t ( 'The name is used in URLs for generated images. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).' ),
'#element_validate' => array ( 'image_style_name_validate' ),
'#required' => TRUE ,
);
2009-10-16 00:52:46 +00:00
2009-07-21 07:09:47 +00:00
// Build the list of existing image effects for this image style.
$form [ 'effects' ] = array (
'#theme' => 'image_style_effects' ,
);
2012-03-23 22:21:49 +00:00
if ( ! empty ( $style [ 'effects' ])) {
foreach ( $style [ 'effects' ] as $key => $effect ) {
$form [ 'effects' ][ $key ][ '#weight' ] = isset ( $form_state [ 'input' ][ 'effects' ]) ? $form_state [ 'input' ][ 'effects' ][ $key ][ 'weight' ] : NULL ;
$form [ 'effects' ][ $key ][ 'label' ] = array (
'#markup' => $effect [ 'label' ],
);
$form [ 'effects' ][ $key ][ 'summary' ] = array (
'#markup' => isset ( $effect [ 'summary theme' ]) ? theme ( $effect [ 'summary theme' ], array ( 'data' => $effect [ 'data' ])) : '' ,
);
$form [ 'effects' ][ $key ][ 'weight' ] = array (
'#type' => 'weight' ,
'#title' => t ( 'Weight for @title' , array ( '@title' => $effect [ 'label' ])),
'#title_display' => 'invisible' ,
'#default_value' => $effect [ 'weight' ],
);
2010-04-22 21:43:59 +00:00
2012-03-23 22:21:49 +00:00
$form [ 'effects' ][ $key ][ 'configure' ] = array (
'#type' => 'link' ,
'#title' => t ( 'edit' ),
'#href' => 'admin/config/media/image-styles/edit/' . $style [ 'name' ] . '/effects/' . $key ,
'#access' => isset ( $effect [ 'form callback' ]),
);
$form [ 'effects' ][ $key ][ 'remove' ] = array (
'#type' => 'link' ,
'#title' => t ( 'delete' ),
'#href' => 'admin/config/media/image-styles/edit/' . $style [ 'name' ] . '/effects/' . $key . '/delete' ,
);
}
2009-07-21 07:09:47 +00:00
}
// Build the new image effect addition form and add it to the effect list.
2010-09-24 21:36:22 +00:00
$new_effect_options = array ();
2009-07-21 07:09:47 +00:00
foreach ( image_effect_definitions () as $effect => $definition ) {
$new_effect_options [ $effect ] = check_plain ( $definition [ 'label' ]);
}
$form [ 'effects' ][ 'new' ] = array (
'#tree' => FALSE ,
'#weight' => isset ( $form_state [ 'input' ][ 'weight' ]) ? $form_state [ 'input' ][ 'weight' ] : NULL ,
);
$form [ 'effects' ][ 'new' ][ 'new' ] = array (
'#type' => 'select' ,
2010-10-20 01:31:07 +00:00
'#title' => t ( 'Effect' ),
'#title_display' => 'invisible' ,
2009-07-21 07:09:47 +00:00
'#options' => $new_effect_options ,
2010-09-24 21:36:22 +00:00
'#empty_option' => t ( 'Select a new effect' ),
2009-07-21 07:09:47 +00:00
);
$form [ 'effects' ][ 'new' ][ 'weight' ] = array (
'#type' => 'weight' ,
2010-10-20 01:31:07 +00:00
'#title' => t ( 'Weight for new effect' ),
'#title_display' => 'invisible' ,
2009-07-21 07:09:47 +00:00
'#default_value' => count ( $form [ 'effects' ]) - 1 ,
);
$form [ 'effects' ][ 'new' ][ 'add' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Add' ),
'#validate' => array ( 'image_style_form_add_validate' ),
'#submit' => array ( 'image_style_form_submit' , 'image_style_form_add_submit' ),
);
2012-01-08 01:29:22 +00:00
2009-10-16 00:52:46 +00:00
// Show the Override or Submit button for this style.
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
$form [ 'actions' ][ 'submit' ] = array (
2009-07-21 07:09:47 +00:00
'#type' => 'submit' ,
'#value' => t ( 'Update style' ),
);
return $form ;
}
/**
* Validate handler for adding a new image effect to an image style .
*/
function image_style_form_add_validate ( $form , & $form_state ) {
if ( ! $form_state [ 'values' ][ 'new' ]) {
form_error ( $form [ 'effects' ][ 'new' ][ 'new' ], t ( 'Select an effect to add.' ));
}
}
/**
* Submit handler for adding a new image effect to an image style .
*/
function image_style_form_add_submit ( $form , & $form_state ) {
$style = $form_state [ 'image_style' ];
// Check if this field has any configuration options.
$effect = image_effect_definition_load ( $form_state [ 'values' ][ 'new' ]);
2012-02-16 21:08:44 +00:00
2009-07-21 07:09:47 +00:00
// Load the configuration form for this option.
if ( isset ( $effect [ 'form callback' ])) {
2009-08-20 10:48:03 +00:00
$path = 'admin/config/media/image-styles/edit/' . $form_state [ 'image_style' ][ 'name' ] . '/add/' . $form_state [ 'values' ][ 'new' ];
2009-10-15 16:18:46 +00:00
$form_state [ 'redirect' ] = array ( $path , array ( 'query' => array ( 'weight' => $form_state [ 'values' ][ 'weight' ])));
2009-07-21 07:09:47 +00:00
}
// If there's no form, immediately add the image effect.
else {
2012-01-07 14:15:26 +00:00
$effect = array (
2012-01-07 14:24:24 +00:00
'name' => $effect [ 'name' ],
'data' => array (),
2012-01-07 14:15:26 +00:00
'weight' => $form_state [ 'values' ][ 'weight' ],
2012-01-08 01:29:22 +00:00
);
2012-01-07 14:15:26 +00:00
image_effect_save ( $style [ 'name' ], $effect );
2009-07-21 07:09:47 +00:00
drupal_set_message ( t ( 'The image effect was successfully applied.' ));
}
}
/**
* Submit handler for saving an image style .
*/
function image_style_form_submit ( $form , & $form_state ) {
$style = $form_state [ 'image_style' ];
// Update image effect weights.
if ( ! empty ( $form_state [ 'values' ][ 'effects' ])) {
foreach ( $form_state [ 'values' ][ 'effects' ] as $ieid => $effect_data ) {
if ( isset ( $style [ 'effects' ][ $ieid ])) {
2012-01-07 14:07:20 +00:00
$effect = array (
'name' => $style [ 'effects' ][ $ieid ][ 'name' ],
'data' => $style [ 'effects' ][ $ieid ][ 'data' ],
'weight' => $effect_data [ 'weight' ],
2012-02-16 20:36:07 +00:00
'ieid' => $ieid ,
2012-01-08 01:29:22 +00:00
);
2012-01-07 14:07:20 +00:00
$style [ 'effects' ][ $ieid ] = $effect ;
2009-07-21 07:09:47 +00:00
}
}
}
2012-01-07 23:20:55 +00:00
// Update the image style name if it has changed. We also need to delete the
2012-01-08 01:29:22 +00:00
// old style, because there is no concept of rename at the moment, just
// create and delete.
2012-02-17 02:04:42 +00:00
// @todo The config API does not yet support the concept of a rename, but
// hooks need to be able to change old style name references to the new
// name, so first save the new style, then delete the old, so that the
// delete function can receive the name of a fully saved style to update
// references to.
2012-01-07 23:20:55 +00:00
if ( isset ( $form_state [ 'values' ][ 'name' ]) && $style [ 'name' ] != $form_state [ 'values' ][ 'name' ]) {
2012-02-17 02:04:42 +00:00
$old_style = $style ;
2012-01-07 23:20:55 +00:00
$style [ 'name' ] = $form_state [ 'values' ][ 'name' ];
}
2009-07-21 07:09:47 +00:00
image_style_save ( $style );
2012-02-17 02:04:42 +00:00
if ( isset ( $old_style )) {
image_style_delete ( $old_style , $style [ 'name' ]);
}
2009-07-21 07:09:47 +00:00
if ( $form_state [ 'values' ][ 'op' ] == t ( 'Update style' )) {
2011-02-21 20:07:51 +00:00
drupal_set_message ( t ( 'Changes to the style have been saved.' ));
2009-07-21 07:09:47 +00:00
}
2009-08-20 10:48:03 +00:00
$form_state [ 'redirect' ] = 'admin/config/media/image-styles/edit/' . $style [ 'name' ];
2009-07-21 07:09:47 +00:00
}
/**
* Form builder ; Form for adding a new image style .
*
* @ ingroup forms
* @ see image_style_add_form_submit ()
* @ see image_style_name_validate ()
*/
2009-09-18 00:12:48 +00:00
function image_style_add_form ( $form , & $form_state ) {
2009-07-21 07:09:47 +00:00
$form [ 'name' ] = array (
'#type' => 'textfield' ,
'#size' => '64' ,
'#title' => t ( 'Style name' ),
'#default_value' => '' ,
'#description' => t ( 'The name is used in URLs for generated images. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).' ),
'#element_validate' => array ( 'image_style_name_validate' ),
2009-08-13 02:03:29 +00:00
'#required' => TRUE ,
2009-07-21 07:09:47 +00:00
);
$form [ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Create new style' ),
);
return $form ;
}
/**
* Submit handler for adding a new image style .
*/
function image_style_add_form_submit ( $form , & $form_state ) {
$style = array ( 'name' => $form_state [ 'values' ][ 'name' ]);
$style = image_style_save ( $style );
drupal_set_message ( t ( 'Style %name was created.' , array ( '%name' => $style [ 'name' ])));
2009-08-20 10:48:03 +00:00
$form_state [ 'redirect' ] = 'admin/config/media/image-styles/edit/' . $style [ 'name' ];
2009-07-21 07:09:47 +00:00
}
/**
* Element validate function to ensure unique , URL safe style names .
*/
function image_style_name_validate ( $element , $form_state ) {
// Check for duplicates.
$styles = image_styles ();
2012-01-02 13:00:06 +00:00
if ( isset ( $styles [ $element [ '#value' ]]) && ( ! isset ( $form_state [ 'image_style' ][ 'name' ]) || $styles [ $element [ '#value' ]][ 'name' ] != $form_state [ 'image_style' ][ 'name' ])) {
2009-07-21 07:09:47 +00:00
form_set_error ( $element [ '#name' ], t ( 'The image style name %name is already in use.' , array ( '%name' => $element [ '#value' ])));
}
// Check for illegal characters in image style names.
if ( preg_match ( '/[^0-9a-z_\-]/' , $element [ '#value' ])) {
form_set_error ( $element [ '#name' ], t ( 'Please only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for style names.' ));
}
}
/**
* Form builder ; Form for deleting an image style .
*
* @ param $style
* An image style array .
*
* @ ingroup forms
* @ see image_style_delete_form_submit ()
*/
2009-09-18 00:12:48 +00:00
function image_style_delete_form ( $form , $form_state , $style ) {
2009-07-21 07:09:47 +00:00
$form_state [ 'image_style' ] = $style ;
$replacement_styles = array_diff_key ( image_style_options (), array ( $style [ 'name' ] => '' ));
$form [ 'replacement' ] = array (
'#title' => t ( 'Replacement style' ),
'#type' => 'select' ,
'#options' => $replacement_styles ,
2010-09-24 21:36:22 +00:00
'#empty_option' => t ( 'No replacement, just delete' ),
2009-07-21 07:09:47 +00:00
);
return confirm_form (
$form ,
t ( 'Optionally select a style before deleting %style' , array ( '%style' => $style [ 'name' ])),
2009-08-20 10:48:03 +00:00
'admin/config/media/image-styles' ,
2009-07-21 07:09:47 +00:00
t ( 'If this style is in use on the site, you may select another style to replace it. All images that have been generated for this style will be permanently deleted.' ),
t ( 'Delete' ), t ( 'Cancel' )
);
}
/**
* Submit handler to delete an image style .
*/
function image_style_delete_form_submit ( $form , & $form_state ) {
$style = $form_state [ 'image_style' ];
image_style_delete ( $style , $form_state [ 'values' ][ 'replacement' ]);
drupal_set_message ( t ( 'Style %name was deleted.' , array ( '%name' => $style [ 'name' ])));
2009-08-20 10:48:03 +00:00
$form_state [ 'redirect' ] = 'admin/config/media/image-styles' ;
2009-07-21 07:09:47 +00:00
}
/**
* Form builder ; Form for adding and editing image effects .
*
* This form is used universally for editing all image effects . Each effect adds
* its own custom section to the form by calling the form function specified in
* hook_image_effects () .
*
* @ param $form_state
* An associative array containing the current state of the form .
* @ param $style
* An image style array .
* @ param $effect
* An image effect array .
*
* @ ingroup forms
* @ see hook_image_effects ()
* @ see image_effects ()
* @ see image_resize_form ()
* @ see image_scale_form ()
* @ see image_rotate_form ()
* @ see image_crop_form ()
* @ see image_effect_form_submit ()
*/
2009-09-18 00:12:48 +00:00
function image_effect_form ( $form , & $form_state , $style , $effect ) {
2009-08-23 02:19:02 +00:00
if ( ! empty ( $effect [ 'data' ])) {
$title = t ( 'Edit %label effect' , array ( '%label' => $effect [ 'label' ]));
}
else {
$title = t ( 'Add %label effect' , array ( '%label' => $effect [ 'label' ]));
}
drupal_set_title ( $title , PASS_THROUGH );
2009-07-21 07:09:47 +00:00
$form_state [ 'image_style' ] = $style ;
$form_state [ 'image_effect' ] = $effect ;
// If no configuration for this image effect, return to the image style page.
if ( ! isset ( $effect [ 'form callback' ])) {
2009-08-20 10:48:03 +00:00
drupal_goto ( 'admin/config/media/image-styles/edit/' . $style [ 'name' ]);
2009-07-21 07:09:47 +00:00
}
2009-09-18 00:12:48 +00:00
$form [ '#tree' ] = TRUE ;
2010-07-30 02:47:28 +00:00
$form [ '#attached' ][ 'css' ][ drupal_get_path ( 'module' , 'image' ) . '/image.admin.css' ] = array ();
2011-12-15 17:33:38 +00:00
$form [ 'data' ] = call_user_func ( $effect [ 'form callback' ], $effect [ 'data' ]);
2009-07-21 07:09:47 +00:00
// Check the URL for a weight, then the image effect, otherwise use default.
$form [ 'weight' ] = array (
'#type' => 'hidden' ,
'#value' => isset ( $_GET [ 'weight' ]) ? intval ( $_GET [ 'weight' ]) : ( isset ( $effect [ 'weight' ]) ? $effect [ 'weight' ] : count ( $style [ 'effects' ])),
);
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#tree' => FALSE , '#type' => 'actions' );
2010-01-03 21:01:04 +00:00
$form [ 'actions' ][ 'submit' ] = array (
2009-07-21 07:09:47 +00:00
'#type' => 'submit' ,
'#value' => isset ( $effect [ 'ieid' ]) ? t ( 'Update effect' ) : t ( 'Add effect' ),
);
2010-01-03 21:01:04 +00:00
$form [ 'actions' ][ 'cancel' ] = array (
2009-11-08 13:23:41 +00:00
'#type' => 'link' ,
'#title' => t ( 'Cancel' ),
'#href' => 'admin/config/media/image-styles/edit/' . $style [ 'name' ],
2009-07-21 07:09:47 +00:00
);
return $form ;
}
/**
* Submit handler for updating an image effect .
*/
function image_effect_form_submit ( $form , & $form_state ) {
2012-01-07 09:43:37 +00:00
$effect = array (
'name' => $form_state [ 'image_effect' ][ 'name' ],
'data' => $form_state [ 'values' ][ 'data' ],
'weight' => $form_state [ 'values' ][ 'weight' ],
);
2012-01-07 14:15:26 +00:00
image_effect_save ( $form_state [ 'image_style' ][ 'name' ], $effect );
2009-07-21 07:09:47 +00:00
drupal_set_message ( t ( 'The image effect was successfully applied.' ));
2012-01-07 14:07:20 +00:00
$form_state [ 'redirect' ] = 'admin/config/media/image-styles/edit/' . $form_state [ 'image_style' ][ 'name' ];
2009-07-21 07:09:47 +00:00
}
/**
* Form builder ; Form for deleting an image effect .
*
* @ param $style
* Name of the image style from which the image effect will be removed .
* @ param $effect
* Name of the image effect to remove .
* @ ingroup forms
* @ see image_effect_delete_form_submit ()
*/
2009-09-18 00:12:48 +00:00
function image_effect_delete_form ( $form , & $form_state , $style , $effect ) {
2009-07-21 07:09:47 +00:00
$form_state [ 'image_style' ] = $style ;
$form_state [ 'image_effect' ] = $effect ;
$question = t ( 'Are you sure you want to delete the @effect effect from the %style style?' , array ( '%style' => $style [ 'name' ], '@effect' => $effect [ 'label' ]));
2009-08-20 10:48:03 +00:00
return confirm_form ( $form , $question , 'admin/config/media/image-styles/edit/' . $style [ 'name' ], '' , t ( 'Delete' ));
2009-07-21 07:09:47 +00:00
}
/**
* Submit handler to delete an image effect .
*/
function image_effect_delete_form_submit ( $form , & $form_state ) {
$style = $form_state [ 'image_style' ];
$effect = $form_state [ 'image_effect' ];
2012-01-07 15:36:41 +00:00
image_effect_delete ( $style [ 'name' ], $effect );
2009-07-21 07:09:47 +00:00
drupal_set_message ( t ( 'The image effect %name has been deleted.' , array ( '%name' => $effect [ 'label' ])));
2009-08-20 10:48:03 +00:00
$form_state [ 'redirect' ] = 'admin/config/media/image-styles/edit/' . $style [ 'name' ];
2009-07-21 07:09:47 +00:00
}
/**
* Element validate handler to ensure a hexadecimal color value .
*/
function image_effect_color_validate ( $element , & $form_state ) {
if ( $element [ '#value' ] != '' ) {
$hex_value = preg_replace ( '/^#/' , '' , $element [ '#value' ]);
if ( ! preg_match ( '/^#[0-9A-F]{3}([0-9A-F]{3})?$/' , $element [ '#value' ])) {
form_error ( $element , t ( '!name must be a hexadecimal color value.' , array ( '!name' => $element [ '#title' ])));
}
}
}
/**
* Element validate handler to ensure that either a height or a width is
* specified .
*/
function image_effect_scale_validate ( $element , & $form_state ) {
if ( empty ( $element [ 'width' ][ '#value' ]) && empty ( $element [ 'height' ][ '#value' ])) {
form_error ( $element , t ( 'Width and height can not both be blank.' ));
}
}
/**
* Form structure for the image resize form .
*
* Note that this is not a complete form , it only contains the portion of the
* form for configuring the resize options . Therefore it does not not need to
* include metadata about the effect , nor a submit button .
*
* @ param $data
* The current configuration for this resize effect .
*/
function image_resize_form ( $data ) {
$form [ 'width' ] = array (
2012-04-19 11:31:47 +00:00
'#type' => 'number' ,
2009-07-21 07:09:47 +00:00
'#title' => t ( 'Width' ),
'#default_value' => isset ( $data [ 'width' ]) ? $data [ 'width' ] : '' ,
'#field_suffix' => ' ' . t ( 'pixels' ),
'#required' => TRUE ,
'#size' => 10 ,
2012-04-19 11:31:47 +00:00
'#min' => 1 ,
2009-07-21 07:09:47 +00:00
);
$form [ 'height' ] = array (
2012-04-19 11:31:47 +00:00
'#type' => 'number' ,
2009-07-21 07:09:47 +00:00
'#title' => t ( 'Height' ),
'#default_value' => isset ( $data [ 'height' ]) ? $data [ 'height' ] : '' ,
'#field_suffix' => ' ' . t ( 'pixels' ),
'#required' => TRUE ,
'#size' => 10 ,
2012-04-19 11:31:47 +00:00
'#min' => 1 ,
2009-07-21 07:09:47 +00:00
);
return $form ;
}
/**
* Form structure for the image scale form .
*
* Note that this is not a complete form , it only contains the portion of the
* form for configuring the scale options . Therefore it does not not need to
* include metadata about the effect , nor a submit button .
*
* @ param $data
* The current configuration for this scale effect .
*/
function image_scale_form ( $data ) {
$form = image_resize_form ( $data );
$form [ '#element_validate' ] = array ( 'image_effect_scale_validate' );
$form [ 'width' ][ '#required' ] = FALSE ;
$form [ 'height' ][ '#required' ] = FALSE ;
$form [ 'upscale' ] = array (
'#type' => 'checkbox' ,
'#default_value' => ( isset ( $data [ 'upscale' ])) ? $data [ 'upscale' ] : 0 ,
'#title' => t ( 'Allow Upscaling' ),
'#description' => t ( 'Let scale make images larger than their original size' ),
);
return $form ;
}
/**
* Form structure for the image crop form .
*
* Note that this is not a complete form , it only contains the portion of the
* form for configuring the crop options . Therefore it does not not need to
* include metadata about the effect , nor a submit button .
*
* @ param $data
* The current configuration for this crop effect .
*/
function image_crop_form ( $data ) {
$data += array (
'width' => '' ,
'height' => '' ,
'anchor' => 'center-center' ,
);
$form = image_resize_form ( $data );
$form [ 'anchor' ] = array (
'#type' => 'radios' ,
'#title' => t ( 'Anchor' ),
'#options' => array (
'left-top' => t ( 'Top' ) . ' ' . t ( 'Left' ),
'center-top' => t ( 'Top' ) . ' ' . t ( 'Center' ),
'right-top' => t ( 'Top' ) . ' ' . t ( 'Right' ),
'left-center' => t ( 'Center' ) . ' ' . t ( 'Left' ),
'center-center' => t ( 'Center' ),
'right-center' => t ( 'Center' ) . ' ' . t ( 'Right' ),
'left-bottom' => t ( 'Bottom' ) . ' ' . t ( 'Left' ),
'center-bottom' => t ( 'Bottom' ) . ' ' . t ( 'Center' ),
'right-bottom' => t ( 'Bottom' ) . ' ' . t ( 'Right' ),
),
'#theme' => 'image_anchor' ,
'#default_value' => $data [ 'anchor' ],
'#description' => t ( 'The part of the image that will be retained during the crop.' ),
);
return $form ;
}
/**
* Form structure for the image rotate form .
*
* Note that this is not a complete form , it only contains the portion of the
* form for configuring the rotate options . Therefore it does not not need to
* include metadata about the effect , nor a submit button .
*
* @ param $data
* The current configuration for this rotate effect .
*/
function image_rotate_form ( $data ) {
$form [ 'degrees' ] = array (
2012-04-19 11:31:47 +00:00
'#type' => 'number' ,
2009-07-21 07:09:47 +00:00
'#default_value' => ( isset ( $data [ 'degrees' ])) ? $data [ 'degrees' ] : 0 ,
'#title' => t ( 'Rotation angle' ),
'#description' => t ( 'The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.' ),
'#field_suffix' => '°' ,
'#required' => TRUE ,
'#size' => 6 ,
'#maxlength' => 4 ,
);
$form [ 'bgcolor' ] = array (
'#type' => 'textfield' ,
'#default_value' => ( isset ( $data [ 'bgcolor' ])) ? $data [ 'bgcolor' ] : '#FFFFFF' ,
'#title' => t ( 'Background color' ),
'#description' => t ( 'The background color to use for exposed areas of the image. Use web-style hex colors (#FFFFFF for white, #000000 for black). Leave blank for transparency on image types that support it.' ),
'#size' => 7 ,
'#maxlength' => 7 ,
'#element_validate' => array ( 'image_effect_color_validate' ),
);
$form [ 'random' ] = array (
'#type' => 'checkbox' ,
'#default_value' => ( isset ( $data [ 'random' ])) ? $data [ 'random' ] : 0 ,
'#title' => t ( 'Randomize' ),
'#description' => t ( 'Randomize the rotation angle for each image. The angle specified above is used as a maximum.' ),
);
return $form ;
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for the page containing the list of image styles .
2009-07-21 07:09:47 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
* - styles : An array of all the image styles returned by image_get_styles () .
*
2009-07-21 07:09:47 +00:00
* @ see image_get_styles ()
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_image_style_list ( $variables ) {
$styles = $variables [ 'styles' ];
2011-12-02 14:12:20 +00:00
$header = array ( t ( 'Style name' ), array ( 'data' => t ( 'Operations' ), 'colspan' => 3 ));
2009-07-21 07:09:47 +00:00
$rows = array ();
2011-12-02 14:12:20 +00:00
$link_attributes = array (
'attributes' => array (
'class' => array ( 'image-style-link' ),
),
);
2009-07-21 07:09:47 +00:00
foreach ( $styles as $style ) {
$row = array ();
2009-08-20 10:48:03 +00:00
$row [] = l ( $style [ 'name' ], 'admin/config/media/image-styles/edit/' . $style [ 'name' ]);
2011-12-02 14:12:20 +00:00
$row [] = l ( t ( 'edit' ), 'admin/config/media/image-styles/edit/' . $style [ 'name' ], $link_attributes );
$row [] = l ( t ( 'delete' ), 'admin/config/media/image-styles/delete/' . $style [ 'name' ], $link_attributes );
2009-07-21 07:09:47 +00:00
$rows [] = $row ;
}
if ( empty ( $rows )) {
$rows [] = array ( array (
'colspan' => 4 ,
2009-08-20 10:48:03 +00:00
'data' => t ( 'There are currently no styles. <a href="!url">Add a new one</a>.' , array ( '!url' => url ( 'admin/config/media/image-styles/add' ))),
2009-07-21 07:09:47 +00:00
));
}
2009-10-09 01:00:08 +00:00
return theme ( 'table' , array ( 'header' => $header , 'rows' => $rows ));
2009-07-21 07:09:47 +00:00
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a listing of the effects within a specific image style .
2009-07-21 07:09:47 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
2010-04-13 15:23:03 +00:00
* - form : A render element representing the form .
2009-10-09 01:00:08 +00:00
*
2009-07-21 07:09:47 +00:00
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_image_style_effects ( $variables ) {
$form = $variables [ 'form' ];
2009-07-21 07:09:47 +00:00
$rows = array ();
foreach ( element_children ( $form ) as $key ) {
2010-11-21 07:24:53 +00:00
$row = array ();
$form [ $key ][ 'weight' ][ '#attributes' ][ 'class' ] = array ( 'image-effect-order-weight' );
2011-12-03 15:06:48 +00:00
if ( $key != 'new' ) {
2009-07-21 07:09:47 +00:00
$summary = drupal_render ( $form [ $key ][ 'summary' ]);
$row [] = drupal_render ( $form [ $key ][ 'label' ]) . ( empty ( $summary ) ? '' : ' ' . $summary );
$row [] = drupal_render ( $form [ $key ][ 'weight' ]);
$row [] = drupal_render ( $form [ $key ][ 'configure' ]);
$row [] = drupal_render ( $form [ $key ][ 'remove' ]);
}
else {
// Add the row for adding a new image effect.
$row [] = '<div class="image-style-new">' . drupal_render ( $form [ 'new' ][ 'new' ]) . drupal_render ( $form [ 'new' ][ 'add' ]) . '</div>' ;
$row [] = drupal_render ( $form [ 'new' ][ 'weight' ]);
$row [] = array ( 'data' => '' , 'colspan' => 2 );
}
2009-10-16 00:52:46 +00:00
if ( ! isset ( $form [ $key ][ '#access' ]) || $form [ $key ][ '#access' ]) {
$rows [] = array (
'data' => $row ,
2010-11-21 07:24:53 +00:00
'class' => ! empty ( $form [ $key ][ 'weight' ][ '#access' ]) || $key == 'new' ? array ( 'draggable' ) : array (),
2009-10-16 00:52:46 +00:00
);
}
2009-07-21 07:09:47 +00:00
}
$header = array (
t ( 'Effect' ),
t ( 'Weight' ),
array ( 'data' => t ( 'Operations' ), 'colspan' => 2 ),
);
2012-01-02 12:48:19 +00:00
if ( count ( $rows ) == 1 && ( ! isset ( $form [ 'new' ][ '#access' ]) || $form [ 'new' ][ '#access' ])) {
2009-07-21 07:09:47 +00:00
array_unshift ( $rows , array ( array (
'data' => t ( 'There are currently no effects in this style. Add one by selecting an option below.' ),
'colspan' => 4 ,
)));
}
2009-10-09 01:00:08 +00:00
$output = theme ( 'table' , array ( 'header' => $header , 'rows' => $rows , 'attributes' => array ( 'id' => 'image-style-effects' )));
2009-07-21 07:09:47 +00:00
drupal_add_tabledrag ( 'image-style-effects' , 'order' , 'sibling' , 'image-effect-order-weight' );
return $output ;
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a preview of an image style .
2009-07-21 07:09:47 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
* - style : The image style array being previewed .
*
2009-07-21 07:09:47 +00:00
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_image_style_preview ( $variables ) {
$style = $variables [ 'style' ];
2009-07-21 07:09:47 +00:00
$sample_image = variable_get ( 'image_style_preview_image' , drupal_get_path ( 'module' , 'image' ) . '/sample.png' );
$sample_width = 160 ;
$sample_height = 160 ;
// Set up original file information.
$original_path = $sample_image ;
$original_image = image_get_info ( $original_path );
if ( $original_image [ 'width' ] > $original_image [ 'height' ]) {
$original_width = min ( $original_image [ 'width' ], $sample_width );
$original_height = round ( $original_width / $original_image [ 'width' ] * $original_image [ 'height' ]);
}
else {
$original_height = min ( $original_image [ 'height' ], $sample_height );
$original_width = round ( $original_height / $original_image [ 'height' ] * $original_image [ 'width' ]);
}
$original_attributes = array_intersect_key ( $original_image , array ( 'width' => '' , 'height' => '' ));
$original_attributes [ 'style' ] = 'width: ' . $original_width . 'px; height: ' . $original_height . 'px;' ;
// Set up preview file information.
$preview_file = image_style_path ( $style [ 'name' ], $original_path );
2009-08-17 19:14:42 +00:00
if ( ! file_exists ( $preview_file )) {
image_style_create_derivative ( $style , $original_path , $preview_file );
2009-07-21 07:09:47 +00:00
}
2009-08-17 19:14:42 +00:00
$preview_image = image_get_info ( $preview_file );
2009-07-21 07:09:47 +00:00
if ( $preview_image [ 'width' ] > $preview_image [ 'height' ]) {
$preview_width = min ( $preview_image [ 'width' ], $sample_width );
$preview_height = round ( $preview_width / $preview_image [ 'width' ] * $preview_image [ 'height' ]);
}
else {
$preview_height = min ( $preview_image [ 'height' ], $sample_height );
$preview_width = round ( $preview_height / $preview_image [ 'height' ] * $preview_image [ 'width' ]);
}
$preview_attributes = array_intersect_key ( $preview_image , array ( 'width' => '' , 'height' => '' ));
$preview_attributes [ 'style' ] = 'width: ' . $preview_width . 'px; height: ' . $preview_height . 'px;' ;
// In the previews, timestamps are added to prevent caching of images.
2010-10-21 20:50:43 +00:00
$output = '<div class="image-style-preview preview clearfix">' ;
2009-07-21 07:09:47 +00:00
// Build the preview of the original image.
2010-10-21 20:50:43 +00:00
$original_url = file_create_url ( $original_path );
2009-07-21 07:09:47 +00:00
$output .= '<div class="preview-image-wrapper">' ;
2010-10-21 20:50:43 +00:00
$output .= t ( 'original' ) . ' (' . l ( t ( 'view actual size' ), $original_url ) . ')' ;
2009-07-21 07:09:47 +00:00
$output .= '<div class="preview-image original-image" style="' . $original_attributes [ 'style' ] . '">' ;
2011-11-25 03:09:40 +00:00
$output .= '<a href="' . $original_url . '">' . theme ( 'image' , array ( 'uri' => $original_path , 'alt' => t ( 'Sample original image' ), 'title' => '' , 'attributes' => $original_attributes )) . '</a>' ;
2009-07-21 07:09:47 +00:00
$output .= '<div class="height" style="height: ' . $original_height . 'px"><span>' . $original_image [ 'height' ] . 'px</span></div>' ;
$output .= '<div class="width" style="width: ' . $original_width . 'px"><span>' . $original_image [ 'width' ] . 'px</span></div>' ;
$output .= '</div>' ; // End preview-image.
$output .= '</div>' ; // End preview-image-wrapper.
// Build the preview of the image style.
2010-10-21 20:50:43 +00:00
$preview_url = file_create_url ( $preview_file ) . '?cache_bypass=' . REQUEST_TIME ;
2009-07-21 07:09:47 +00:00
$output .= '<div class="preview-image-wrapper">' ;
$output .= check_plain ( $style [ 'name' ]) . ' (' . l ( t ( 'view actual size' ), file_create_url ( $preview_file ) . '?' . time ()) . ')' ;
$output .= '<div class="preview-image modified-image" style="' . $preview_attributes [ 'style' ] . '">' ;
2011-11-25 03:09:40 +00:00
$output .= '<a href="' . file_create_url ( $preview_file ) . '?' . time () . '">' . theme ( 'image' , array ( 'uri' => $preview_url , 'alt' => t ( 'Sample modified image' ), 'title' => '' , 'attributes' => $preview_attributes )) . '</a>' ;
2009-07-21 07:09:47 +00:00
$output .= '<div class="height" style="height: ' . $preview_height . 'px"><span>' . $preview_image [ 'height' ] . 'px</span></div>' ;
$output .= '<div class="width" style="width: ' . $preview_width . 'px"><span>' . $preview_image [ 'width' ] . 'px</span></div>' ;
$output .= '</div>' ; // End preview-image.
$output .= '</div>' ; // End preview-image-wrapper.
$output .= '</div>' ; // End image-style-preview.
return $output ;
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a 3 x3 grid of checkboxes for image anchors .
2009-07-21 07:09:47 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
2010-04-13 15:23:03 +00:00
* - element : A render element containing radio buttons .
2009-10-09 01:00:08 +00:00
*
2009-07-21 07:09:47 +00:00
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_image_anchor ( $variables ) {
$element = $variables [ 'element' ];
2009-07-21 07:09:47 +00:00
$rows = array ();
$row = array ();
foreach ( element_children ( $element ) as $n => $key ) {
$element [ $key ][ '#attributes' ][ 'title' ] = $element [ $key ][ '#title' ];
unset ( $element [ $key ][ '#title' ]);
$row [] = drupal_render ( $element [ $key ]);
if ( $n % 3 == 3 - 1 ) {
$rows [] = $row ;
$row = array ();
}
}
2009-10-09 01:00:08 +00:00
return theme ( 'table' , array ( 'header' => array (), 'rows' => $rows , 'attributes' => array ( 'class' => array ( 'image-anchor' ))));
2009-07-21 07:09:47 +00:00
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a summary of an image resize effect .
2009-07-21 07:09:47 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
* - data : The current configuration for this resize effect .
*
2009-07-21 07:09:47 +00:00
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_image_resize_summary ( $variables ) {
$data = $variables [ 'data' ];
2009-07-21 07:09:47 +00:00
if ( $data [ 'width' ] && $data [ 'height' ]) {
return check_plain ( $data [ 'width' ]) . 'x' . check_plain ( $data [ 'height' ]);
}
else {
return ( $data [ 'width' ]) ? t ( 'width @width' , array ( '@width' => $data [ 'width' ])) : t ( 'height @height' , array ( '@height' => $data [ 'height' ]));
}
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a summary of an image scale effect .
2009-07-21 07:09:47 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
* - data : The current configuration for this scale effect .
*
2009-07-21 07:09:47 +00:00
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_image_scale_summary ( $variables ) {
$data = $variables [ 'data' ];
return theme ( 'image_resize_summary' , array ( 'data' => $data )) . ' ' . ( $data [ 'upscale' ] ? '(' . t ( 'upscaling allowed' ) . ')' : '' );
2009-07-21 07:09:47 +00:00
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a summary of an image crop effect .
2009-07-21 07:09:47 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
* - data : The current configuration for this crop effect .
*
2009-07-21 07:09:47 +00:00
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_image_crop_summary ( $variables ) {
return theme ( 'image_resize_summary' , $variables );
2009-07-21 07:09:47 +00:00
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a summary of an image rotate effect .
2009-07-21 07:09:47 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
* - data : The current configuration for this rotate effect .
*
2009-07-21 07:09:47 +00:00
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_image_rotate_summary ( $variables ) {
$data = $variables [ 'data' ];
2009-07-21 07:09:47 +00:00
return ( $data [ 'random' ]) ? t ( 'random between -@degrees° and @degrees°' , array ( '@degrees' => str_replace ( '-' , '' , $data [ 'degrees' ]))) : t ( '@degrees°' , array ( '@degrees' => $data [ 'degrees' ]));
}