Custom image sharpening

Using the imageapi and imagecache 5.x-2.0 it is fairly easy to integrate new image filters.

Firstly, ensure that you have a custom module that you can edit, or create a new one using the two files given below:

File: extra_filters.info

; $Id:
name = Image unsharp mask
description = Misc. custom filters; including unsharp mask, ...
package = Other
version = 0.1

File: extra_filters.module

<?php

/**
* Implementation of hook_imagecache_actions.
*
* @return array
*   An array of information on the actions implemented by a module. The array contains a
*   sub-array for each action node type, with the machine-readable action name as the key.
*   Each sub-array has up to 3 attributes. Possible attributes:
*
*     "name": the human-readable name of the action. Required.
*     "description": a brief description of the action. Required.
*     "file": the name of the include file the action can be found
*             in relative to the implementing module's path.
*/
function extra_filters_imagecache_actions() {
 
$actions = array(
   
'extra_filters_unsharp_mask' => array(
     
'name' => 'Unsharp mask',
     
'description' => 'Unsharp mask using amount, radius and threshold.',
     
// using external files to reduce the server load and clutter in the module
     
'file' => 'extra_filters_unsharp_mask.inc',
    ),
  );

  return
$actions;
}
// don't add the ? > below to the real file
?>

Then define up the form fields and filter:

Go to http://vikjavev.no/computing/ump.php to get a copy of the UnsharpMask function and paste it into the following file.

File: extra_filters_unsharp_mask.inc

<?php
/**
* Form to create the new filter parameters
*/
function extra_filters_unsharp_mask_form($action) {
 
$form['amount'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Amount'),
   
'#default_value' => $action['amount'],
   
'#description' => t('Amount to sharpen: (typically 50 - 200)'),
  );
 
$form['radius'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Radius'),
   
'#default_value' => $action['radius'],
   
'#description' => t('Radius: (typically 0.5 - 1)'),
  );
 
$form['threshold'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Threshold'),
   
'#default_value' => $action['threshold'],
   
'#description' => t('Threshold: (typically 0 - 5)'),
  );
  return
$form;
}

/**
* This lets the user see what parameters were selected for the action
*/
function theme_extra_filters_unsharp_mask($element) {
 
$data = $element['#value'];
  return
'amount: '. $data['amount'] .', radius: '. $data['radius'] .', threshold: '. $data['threshold'];
}

/**
* Prep the filter
*/
function extra_filters_unsharp_mask_image(&$image, $data) {
 
$amount = $data['amount'] ? $data['amount'] : 100;
 
$radius = $data['radius'] ? $data['radius'] : 0.75;
 
$threshold = $data['threshold'] ? $data['threshold'] : 2.5;
 
$image->res = UnsharpMask($image->res, $amount, $radius, $threshold);
  return
true;
}

Paste the UnsharpMask function here.

// don't add the ? > below to the real file
?>

Now, enable the new module via the admin interface, and the new filter should be listed alongside the core filters.

If you have any issues, use the forum topic http://drupal.org/node/281193 to discuss these.

Nice work

beautifulmind - June 13, 2008 - 06:23

It is very nice work.

Unfortunately, I can't find the filter option any where(!)
Can you please help me out?

Beautifulmind
Know more

Same problem here

vincent.barbot - July 10, 2008 - 14:27

Cant see the new action in imagecache.

 
 

Drupal is a registered trademark of Dries Buytaert.