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.

Chapter 11<br />

For full details of these and other functions available in the Image API<br />

refer to http://api.drupal.org/api/group/image/7<br />

Let's take a look at how to we might integrate some of these functions with our<br />

Twitpic module. First, let's make create a hook_menu() implementation that we<br />

can use to trigger our code.<br />

/**<br />

* Implement hook_menu().<br />

*/<br />

function twitpic_menu() {<br />

$items = array();<br />

$items['twitpic/munge/%'] = array(<br />

'title' => 'Munge a Twitpic image',<br />

'description' => 'Displays a Twitpic image, munged in various ways<br />

(cropped, rotated, etc).',<br />

'page callback' => 'twitpic_image_munge',<br />

'access arguments' => array('access_content'),<br />

'type' => MENU_CALLBACK,<br />

);<br />

}<br />

return $items;<br />

When the URL twitpic/munge is requested, this hook will call the function<br />

twitpic_image_munge(), which takes two arguments. The first is the ID of<br />

the Twitpic image we want to manipulate, and the second is an operation to be<br />

performed. The allowed operations are rotate, scale, and desaturate. These<br />

arguments will be automatically passed to twitpic_image_munge() when<br />

appended to the URL, as discussed in Chapter 5, Building an Admin Interface.<br />

Let's look at the callback function now:<br />

/**<br />

* Munge an image from Twitpic by applying a resize, crop or<br />

* rotate to it.<br />

*<br />

* @param $id<br />

* The image's ID, as extracted from its original URL.<br />

* @param $operation<br />

* An operation to perform on the image. Can be 'rotate',<br />

* 'scale', or 'desaturate'.<br />

[ 327 ]

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

Saved successfully!

Ooh no, something went wrong!