Hi there,

Today I have been trying to implement pngquant myself using hook_file_insert() and image effects, then using the Image Optimize Effect contrib (also based on image effects) and at last using ImageAPI Optimize.

hook_file_insert() is not called when an image style styled image (ie. thumbnail, medium, etc.) is inserted.

Image effect callback functions are first all being called and then applied to the image, this makes it harder to run image optimizers that should be run after all regular image handlers have been called. The Image Optimize Effect contrib (v.7.1) solves this by temporarily creating an image using GD, this does not work with ImageMagick (which delivers higher quality).

So I like (amongst other things) the way ImageAPI Optimize acts like an image toolkit and allows site admins to select any other available image toolkit for image processing. This way ImageAPI Optimize can run optimization scripts after a styled image has been created, but now the optimization settings are done site wide and not per image style which would be great for lossy optimization.

This could be 'easily' implemented since ImageAPI Optimize image process callback functions receive the $dst (destination?) variable which contains an absolute path to the image, ie. D:\drupal\sites\SITENAME\files\styles\IMAGESTYLE\public\modules\image\sample.png, from which we can derive the image style's machine name with which we can load the image style settings using image_styles(). Depending on the optimization settings for the current image style we can customly optimize all images for this style.

Now we need to add an optimization image effect with a settings page just like the default ImageAPI Optimize settings page.

Is there any interest for certain functionality or is an add-on module (ImageAPI Optimize Image Effect?) maybe preferred?

Comments

jcisio’s picture

I think it is desirable to enable (with just on/off options) the optimization per image style.

I don't think we want to customize the tool per style though. What we could do is enable a controlled image quality per style (a "reduce quality to xx%" option, which is independent the optimization process). I know that the Image Style Quality already does it, but it is sub optimal because it resets the variable cache each time an image is generated). This module would be the best place to implement that.

jcisio’s picture

Assigned: Unassigned » jcisio

I'm interested in this feature. I'm going to do it.

dillix’s picture

Hi! Is there any progress with this feature?

Steven Jones’s picture

Had some thoughts about this, at the moment, we're an image toolkit, right? And we have to do the nasty eval to be able to copy all the procedural functions from the existing toolkit to a new one. Basically, we'd really like to just extend a class, and implement a single different method: save, and we're done.

Well, actually I think we can.

image_save which is called by image_style_create_derivative calls through to image_toolkit_invoke to do the saving.

Now the code for image_toolkit_invoke is:


function image_toolkit_invoke($method, stdClass $image, array $params = array()) {
  $function = 'image_' . $image->toolkit . '_' . $method;
  if (function_exists($function)) {
    array_unshift($params, $image);
    return call_user_func_array($function, $params);
  }
  watchdog('image', 'The selected image handling toolkit %toolkit can not correctly process %function.', array('%toolkit' => $image->toolkit, '%function' => $function), WATCHDOG_ERROR);
  return FALSE;
}

note how the function name to call, is built up from a property on the $image object. This is the exact same image object that is passed into an image style effect callback as the first parameter...so if you were to have an image effect for imageapi_optimize it could actually change the $image->toolkit string so that a different function was called when the image was eventually saved.

This would allow:

  • Per image-style optimisation on/off
  • Per image-style optimisation settings, maybe for quality or tools used
  • Removal of the weird duplication of functions in an eval

Realise that that would be quite a large change for the module, but potentially quite a nice combination of new features and cleanup.

Steven Jones’s picture

Status: Active » Needs work

Started work on what I've outlined in #4 over in a sandbox:
https://www.drupal.org/sandbox/darthsteven/2614582

Currently, you need to use the old admin UI to set up the imageapi optimize stuff, and then switch the toolkit back to your default, and then add the image effect to your styles, but so, it works!

  • Steven Jones committed 31b4439 on 8.x-2.x
    Issue #2164195: Optimization settings per image style using image style...
Steven Jones’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Needs work » Fixed

This is basically done in the 7.x-2.x branch now.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.