So let's say you want to have the first action in a preset to be a custom action that checks to see whether or not the image has an original size of X and if so, create a red border around it (or whatever.) This is easily done, but what if you don't want any of the additional normal actions to run if that condition is true in the custom action? I'm thinking of it as something like a break command in PHP, but imagecache would check to see if $image->breakflag = 'true' or something like that and instruct it to finish up without processing the additional actions. In the custom action you would simply add $image->breakflag = "true" in a conditional and be done with it. Any opinions?

elizzle

Comments

dman’s picture

Not a break flag
But you can easily do a switch statement.

Create a preset chain that does what you want to perform if true.
create a custom action that inspects the parameters, and calls the first preset as a subroutine if desired. And nothing (or something else) if not.

Syntax for invoking presets from another preset is pretty simple (though could be a little simpler). In fact that's the way half the core imagecache actions work.

That's what (for example) aspect switcher does.
the php code would be something like

  if ($a=$b) {
    $preset = imagecache_preset_by_name($preset_name);
    // Run the preset actions ourself. Cannot invoke a preset from the top as it handles filenames, not image objects.
    foreach ($preset['actions'] as $sub_action) {
     _imagecache_apply_action($sub_action, $image);
   }
  }
return $image;

... there is actually a bit more to it last time I tried, though that may have been fixed since.
See canvasactions_aspect_image() for the version with error-handling and co-ordinate conversions.
I guess we could add a utility call that would allow you to just invoke_preset($presetname, $image) so you could have 3 lines of php not 10

spelcheck’s picture

Status: Active » Closed (works as designed)

dman, THANK YOU.

demonrage’s picture

nice code, but how to check the image type (gif.jpeg or png) with custom php code, cause i want image cache to pass gif files,
as u know image cahce destroys animated gifs and display them as still images .
thx

dman’s picture

@demonrage
What have you tried so far?
There will be several clues to this available in the $image object that customactions can use. You can inspect the filename at least.
IIRC the MIME type is there too

demonrage’s picture

ok dman,

i'm really don't know where to start from, is there any documentation or readme file that i can start from ??

dman’s picture

There is stand-alone documentation displayed as help text within the form if you enable a custom action, including a full example of how to write your own process.
If you can't find that yet, I don't know what to suggest.

If you want to do more than the example, you should probably add some debugging code of your own that displays the available attributes of the $image object, and add your own logic based on that. If you are unable to add or work with PHP debuggging info, then PHP customactions is probably not the tool for you.