tpr Posted October 2, 2015 Share Posted October 2, 2015 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? 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 2, 2015 Share Posted October 2, 2015 $this->addHookAfter('Pageimage::size', $this, function(HookEvent $event){ $thumb = $event->return; // Replace thumb file with optimized one. }); 2 Link to comment Share on other sites More sharing options...
tpr Posted October 2, 2015 Author Share Posted October 2, 2015 Great, thanks! Seems to work fine. $thumb = $event->return; It was the turning point, so it catches the output for further process? Hadn't thought about this. Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 2, 2015 Share Posted October 2, 2015 Exactly, you could even replace a whole function with hooks. You can read more about this in the docs about hooks. Link to comment Share on other sites More sharing options...
bernhard Posted October 2, 2015 Share Posted October 2, 2015 hi tpr, do you plan to publish your work as a module? could be quite helpful to many others i think 2 Link to comment Share on other sites More sharing options...
tpr Posted October 2, 2015 Author Share Posted October 2, 2015 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. 2 Link to comment Share on other sites More sharing options...
tpr Posted October 6, 2015 Author Share Posted October 6, 2015 Two WiP images: 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)? 3 Link to comment Share on other sites More sharing options...
MuchDev Posted October 6, 2015 Share Posted October 6, 2015 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 More sharing options...
tpr Posted October 7, 2015 Author Share Posted October 7, 2015 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 More sharing options...
MuchDev Posted October 7, 2015 Share Posted October 7, 2015 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now