18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Working with Files and Images<br />

Creating image effects<br />

Note that this example is not compatible with pluggable image libraries,<br />

and will only work with GD. For more information about making<br />

your image functions compatible with multiple libraries, see the API<br />

documentation on image_toolkit_invoke() at:<br />

http://api.drupal.org/api/function/image_toolkit_<br />

invoke/7<br />

We will begin with the assumption that you have created a standard module .info<br />

file with all the information needed for a module named watermark. The first step in<br />

creating a new image effect is implementing hook_image_effect_info().<br />

/**<br />

* Implements hook_image_effect_info().<br />

*/<br />

function watermark_image_effect_info() {<br />

$effects = array();<br />

$effects['watermark'] = array(<br />

'label' => t('Watermark'),<br />

'help' => t('Add a watermark to an image.'),<br />

'effect callback' => 'watermark_effect',<br />

'form callback' => 'watermark_form',<br />

'summary theme' => 'watermark_summary',<br />

);<br />

return $effects;<br />

}<br />

This kind of hook should look pretty familiar by now. It is very similar to<br />

hook_menu(), which was discussed in Chapter 5, Building an Admin Interface. We<br />

create an associative array of the effects that we are defining, and return it to the<br />

caller. The array is keyed by a unique identifier, in our case, watermark. This array<br />

contains the following information about our image effect:<br />

• label: The label that will be used in the effects drop-down on the styles<br />

page. This text will also be used after you have added your effect, if you<br />

have not defined a summary theme below.<br />

• help: A text description of what your effect does.<br />

• effect callback: A function that will be called to actually execute your<br />

effect on an image. This function name indicates where your image<br />

manipulation code will go.<br />

[ 334 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!