7.x image.api.php hook_image_default_styles()

Provide module-based image styles for reuse throughout Drupal.

This hook allows your module to provide image styles. This may be useful if you require images to fit within exact dimensions. Note that you should attempt to re-use the default styles provided by Image module whenever possible, rather than creating image styles that are specific to your module. Image provides the styles "thumbnail", "medium", and "large".

You may use this hook to more easily manage your site's changes by moving existing image styles from the database to a custom module. Note however that moving image styles to code instead storing them in the database has a negligible effect on performance, since custom image styles are loaded from the database all at once. Even if all styles are pulled from modules, Image module will still perform the same queries to check the database for any custom styles.

Return value

An array of image styles, keyed by the style name.

See also

image_image_default_styles()

Related topics

1 function implements hook_image_default_styles()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

image_image_default_styles in modules/image/image.module
Implements hook_image_default_styles().
1 invocation of hook_image_default_styles()
image_styles in modules/image/image.module
Gets an array of all styles and their settings.

File

modules/image/image.api.php, line 176
Hooks related to image styles and effects.

Code

function hook_image_default_styles() {
  $styles = array();

  $styles['mymodule_preview'] = array(
    'label' => 'My module preview',
    'effects' => array(
      array(
        'name' => 'image_scale',
        'data' => array('width' => 400, 'height' => 400, 'upscale' => 1),
        'weight' => 0,
      ),
      array(
        'name' => 'image_desaturate',
        'data' => array(),
        'weight' => 1,
      ),
    ),
  );

  return $styles;
}

Comments

yakoub’s picture

i think effects array should be indexed by name so that hook_image_styles_alter can be used with ease :

'effects' => array(
  'image_scale' => array(
    'data' => array(
      ....
    ),
    'weight' => array(
      ....
    ),
)
aaronbauman’s picture

If you follow yakoub's idea, your default image style will not work.
Your default image styles must be indexed numerically.

symeon.mattes’s picture

Hi,
I have some images of random size and I would like to resize them in such a way that the final image size cannot exceed the size of 730x400.

For instance if the image is 1600x600 the image has to be resized based on the width, i.e. become 730x273, while if the image is 600x1600 it has to be resized based on the height, i.e. 150x400.

Is there any way to do it?

balsama’s picture

Note that implementing this hook will throw PHP notices for undefined indexes on the /admin/config/media/image-styles page unless you include "name" and "storage" in the definition array:

function mymodule_image_styles_alter(&$styles) {
  $styles['myimagestyle'] = array(
    'label' => 'My Image Style',
    'effects' => array(
      array(
        'name' => 'image_scale',
        'data' => array(
          'width' => 500,
          'height' => 300,
          'upscale' => 1,
        ),
        'weight' => 0,
      ),
    ),
    'name' => 'myimagestyle', // You need this and the next line to avoid PHP notices
    'storage' => IMAGE_STORAGE_NORMAL,
  );

  return $styles;
}