If you need to create your own imagecache presets from code have a look at following example:

// Preset
  $imagecachepreset = imagecache_preset_save(array('presetname' => 'MYPRESETNAME'));
  // Action
  $imagecacheaction = new stdClass ();
  $imagecacheaction->presetid = $imagecachepreset['presetid'];
  $imagecacheaction->module = 'imagecache';
  $imagecacheaction->action = 'imagecache_scale_and_crop';
  $imagecacheaction->data = array('width' => '200', 'height' => '200' );
  drupal_write_record('imagecache_action', $imagecacheaction);

Thanks to Eikaa & mavimo for this

If you call this in your _install hook the preset gets installed when your module is enabled.
Now for uninstalling this imagecache preset in this case you can do the following:

imagecache_preset_delete(imagecache_preset_by_name('MYPRESETNAME'));
Thanks to tylerwaits for this

Comments

davemybes’s picture

There is now an easier way to do it with imagecache 6.x-2.0-beta10 (and higher, not sure about beta9 or lower). Simply use the following code in your custom module. No need for install hooks. Once this code is in your module, its shown on imagecache page. All you do is export your preset using the Export link and paste that code in the module.

/**
* 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;
}

______________________________________________________________________________________________________

Countzero’s picture

The module is really well constructed. I'm using it for a project of mine, and I'm amazed at the flexibility of the code.

This last post is excellent.

broncomania’s picture

Really amazing tool! thumbs up
Thx for this code snippet. No long search just use.

cmjns’s picture

And yet another way of doing this, starting with an exported preset:

/* Load an existing preset */
// $preset = imagecache_preset_by_name('presetname');

/* Create a new preset from export */
$preset = imagecache_preset_save( array('presetname' => $preset['presetname']) );
$template = array(); /* put exported preset array here */
$preset['actions'] = $template['actions'];

foreach( $preset['actions'] as $action ){
  $action['presetid'] = $preset['presetid'];
  imagecache_action_save($action);
}

Check the code. I didn't test it.

didaka’s picture

I have tested the code and fixed it a bit.

$template = array (); /* put exported preset array here */

/* Load an existing preset */
// $preset = imagecache_preset_by_name('presetname');

/* Create a new preset from export */
$preset = imagecache_preset_save( array('presetname' => $template['presetname']) );

/* Create actions for that preset */
$preset['actions'] = $template['actions'];

foreach( $preset['actions'] as $action ){
  $action['presetid'] = $preset['presetid'];
  imagecache_action_save($action);
}