I'm the author of the fast gallery project. I've noticed that a lot of users are having problems setting up imagecache namespaces properly. Is there a way to do programatically? If so, how? Is there an API for this?

Comments

drewish’s picture

it's not documented but imagecache_action_save() and imagecache_preset_save() should do what you want. it'd be really helpful as you puzzle your way through them document those functions and contribute a patch back.

drewish’s picture

Status: Active » Fixed
drewish’s picture

rapsli’s picture

Status: Fixed » Active

Thanks. This did the job, but there seems to be some really weird problem. Here is my preset and the actions:

  $preset_thumb = array (
    'presetname' => 'fast_gallery_thumb',
  );

  imagecache_preset_save($preset_thumb);

  $preset_id = db_last_insert_id('imagecache_preset','presetid');
  variable_set("fast_gallery_ic_preset_thumb",$preset_id);


  $ar_action_1 = array (
    'action' => 'imagecache_scale',
    'weight' => -10,
    'presetid' => $preset_id,
    'data' => array (
      'width' => 150,
      'height' => '',
      'upscale' => 0,
    ),    
  );

  imagecache_action_save($ar_action_1);
  $ar_action_1 = array (
    'action' => 'imagecache_crop',
    'presetid' => $preset_id,
    'weight' => 0,
    'data' => array (
      'width' => 100,
      'height' => 100,
      'xoffset' => 'center',
      'yoffset' => 'center',
    ),
  );
  imagecache_action_save($ar_action_1);

They are properly being imported, but when I use them, the crop action is not being applied, only the scale action. When I go into the imagecache ui and just save the preset again it works just fine.

Don't really know where the problem lies.

dopry’s picture

Status: Active » Postponed (maintainer needs more info)

no clue... how about sprinkling some drupal_set_message around to figure out what is going on or... I can't troubleshoot this for you... you're gonna have to do some of that yourself.

roblinton’s picture

Hi,

Chances are you've solved this already, but just in case...

When the actions are saved with imagecache_action_save() the cached version of the presets is cleared with imagecache_presets() - which is passed true to force a reload from the db. But the actions for each preset are accessed via imagecache_preset_actions() which further stores its return value in a static var. Consequently the actions aren't refreshed after the first one is stored in a static return var in imagecache_preset_actions(), even though they're stored in the db.

It works through the form because imagecache_ui_preset_load() clears the caches and forces a reload before anything is stored the static var in imagecache_preset_actions().

imagecache_preset_save() returns the preset with the presetid set in the array so you can do away with your db_last_insert_id stuff. Then you can fix the caching by calling:

  imagecache_preset_actions($preset, true);
  imagecache_presets(true);

After all your updates are made. That's imagecache_preset_actions() for each preset you set dynamically, and imagecache_presets(true) once after everything's in the db.

Cheers.

dopry’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

also the ID should be automatically added to the $preset array, you don't need to db_last_insert_id() IIRC.