Moving imagecache presets to a module

Quick post to show you how to move your Drupal 6.x imagecache presets to a module. This is for imagecache-6.x-2.0-beta10 or higher.

Create your presets in Drupal. Once they're all done, go to the ImageCache list page and click the export link next to the first preset. Copy the code presented to you, and paste it into your module using a function called hook_imagecache_default_presets(). Add a return $presets at the end of the function and save the module.

To have more than one preset in your module, export another preset. This time, leave off the first line of the export ($presets = array();) and paste the rest in below the first preset, and above the return statement. Repeat this for each preset.

If your module was not already active, enable it, and you will now be able to Revert each of the imagecache presets. Now you have everything in your module, ready for some version control :)

This is what should be in your module:

<?php
/**
* Implementation of hook_imagecache_default_presets().
*/
function mymodule_imagecache_default_presets() {
 
$presets = array();
 
$presets['product_mini_thumb'] = array (
   
'presetname' => 'product_mini_thumb',
   
'actions' =>
    array (
     
0 =>
      array (
       
'weight' => '0',
       
'module' => 'imagecache',
       
'action' => 'imagecache_scale_and_crop',
       
'data' =>
        array (
         
'width' => '25',
         
'height' => '25',
        ),
      ),
    ),
  );
 
$presets['product_thumb_50x50'] = array (
   
'presetname' => 'product_thumb_50x50',
   
'actions' =>
    array (
     
0 =>
      array (
       
'weight' => '0',
       
'module' => 'imagecache',
       
'action' => 'imagecache_scale_and_crop',
       
'data' =>
        array (
         
'width' => '50',
         
'height' => '50',
        ),
      ),
    ),
  );

  return $presets;
}
?>

Strange, I had to use a non-breaking space between the words return and $presets to get them to stay on the same line. Must be a bug in the PHP formatter, I guess.

Posted in:

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <b> <i> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <blockquote> <img>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is to block spam bots and check if you're human
magnanimous-junior