If I were to implement this as part of imagecache, the hook syntax would be

function hook_imagecache_presets($api) {
  $api->addPreset('frontpage-slider')->scaleAndCrop(520, 325);
  $api->addPreset('frontpage')->scale(217, '', FALSE);

and the $api would be an instance of

class imagecache_InjectedAPI_hookImagecachePresets {..}

auto-loaded with xautoload module.
(see comment #6)

Original request

In my own projects I often add a custom module with shortcut syntax for imagecache presets.
This allows to define imagecache presets like this:


function _mymodule_imagecache_presets(mymodule_imgcache_HookHandler $h) {

  // I recommend underscores instead of hyphens,
  // to avoid problems with formatters.
  $h->addPreset('video_thumbnail')->scaleAndCrop(110, 80);
  $h->addPreset('frontpage-slider')->scaleAndCrop(520, 325);
  $h->addPreset('frontpage')->scale(217, '', FALSE);
  $h->addPreset('imagesoverview-square')->scaleAndCrop(111, 111);
  $h->addPreset('map')->scale(400, '100%', FALSE);
  $h->addPreset('photo_thumb')->scale(100, 100, FALSE);

I think this method chaining syntax is far nicer to read and to work with than huge configuration arrays, and it is better for migration than using the ui. Even better than features, imo.

Below that is a huge "shortcut infrastructure" section, which is usually the same on any site. Just "mymodule" gets a different name.
I can post the "shortcut infrastructure" code, if you are interested.

#3 example_imgcache.module.txt2.56 KBdonquixote


donquixote’s picture

Did I just say, underscores are better?
Probably the presets with hyphens did already exist before, and I did not want to change the name. Whatever.
(this is all copied from an older project)

drewish’s picture

Go ahead and post it I'm sure someone will find it useful.

donquixote’s picture

2.56 KB

Can you imagine this as part of the imagecache module?
For instance, the "hook handler" (happy if you find a better name) could be injected as a parameter into every implementation of hook_imagecache_default_presets(). The return value would then be merged with the stuff from the shortcuts, so it would be 100% backwards compatible.

donquixote’s picture

We could add more methods, but I think 99% of use cases only need crop, scale and scale+crop.

If we really want to be super-generic, we could do something with magic methods. But I doubt it would be worth the trouble. People can still use the generic method that allows all kinds of arrays, that should be enough.

fizk’s picture

Issue tags: +ImageCache 3

Marking as ImageCache 3.x Todo.

donquixote’s picture

Something I want to mention:
Whenever I implement this pattern in my own contrib/custom modules, I name the parameter "$api" instead of "$h".
The class would be called class imagecache_InjectedAPI. The class exists only for use within the hook, it is not used anywhere else. The objects are destroyed once the hook has completed to run. The information collected during the hook invocation is extracted from the object, and used elsewhere.

I absolutely made this up myself, and I have no idea if anyone else has a different name for this pattern. I find "Injected API" a convincing term.

donquixote’s picture

If anyone is still interested:
This thing is now in my sandbox for Drupal 7 image styles (and other things).

Sorry, I don't think I will make a D6 version..

donquixote’s picture

Issue summary: View changes

Preferred syntax ($api instead of $h)