Jump to content

Hook to run on creating image variations


tpr
 Share

Recommended Posts

I'm unable to find out how to hook the process of creating image variations.

I would like to use resmush api to optimize images. I'm using the service successfully on adding images using

 $this->addHookBefore('InputfieldFile::fileAdded', $this, 'optimizeImage_InputfieldFile');

and I also added a new optimize() method to image fields that can be used like this:

$page->image->size(100,100)->optimize()->url

But it would be nice if it could be fully automatic. That is:

  • optimize on upload - DONE
  • optimize on request  - DONE
  • optimize automatically if new image size is requested (eg. changing from size(100,100) to size(480,200)) - FAIL

ImageSizer::resize seemed the right one but it returns only "true" and I can get only the image file name in the cache dir.

What I would need is the final image path and final image url.

Any thoughts on this?

  • Like 2
Link to comment
Share on other sites

Sure! There are some minor issues I'm aware of that needs to be eliminated, plus creating some (very) basic admin settings to it.

Mass optimizing could be an issue with many images but that's something I will ignore in the first round.

  • Like 2
Link to comment
Share on other sites

Two WiP images:

post-3156-0-21624100-1444154850_thumb.pn

post-3156-0-23436800-1444154863_thumb.gi

I planned to add only auto-optimize. But if you don't start using the module when starting the development then many images will remain unoptimized, requiring manual optimization. So there's a bulk optimize feature which works, though it can be really slow. I may take the effort and add a "stop" button but I don't want to overcomplicate this thing.

Currently it overwrites original files, which is dangerous, I know. Should it create a backup of the images? Any suggestions on how?

I also plan to add an exclude (or include?) list feature, just to make sure. Which one would you recommend (if any)?

  • Like 3
Link to comment
Share on other sites

I'd really be interested to see what you come up with as a solution for re-processing everything. If you were worried about your code overwriting your images, you could just have your code omit the compression of the original file. Then you could just compress variations. This wouldn't work if you used your original images as assets though. 

Maybe an option to only compress variations?

Link to comment
Share on other sites

Maybe an option to only compress variations?

Yes, that could be an optional checkbox, though it won't help me too much but perhaps more useful to others.

I thought about duplicating the original file ("filename.jpg.original" or something). This would almost duplicate the storage needed but original images would be kept.

Link to comment
Share on other sites

I thought about duplicating the original file ("filename.jpg.original" or something). This would almost duplicate the storage needed but original images would be kept.

I am sure that would be fine. Have you looked into Horst's "Page Image Manipulator", that thing has tons of options. 

https://processwire.com/talk/topic/4264-page-image-manipulator-1/

https://processwire.com/talk/topic/9982-page-image-manipulator-2/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...