Jump to content

Auto Smush


matjazp
 Share

Recommended Posts

Can't test right now, but looking at the source code - could you move this line: $this->configData = $this->wire('modules')->getModuleConfigData($this); in AutoSmush.module, line 1217, up a few lines, to be the first line in ready() method? I guess I changed something from v 1.1.1 to 1.1.2 and didn't test (I can't remember what was it).

Link to comment
Share on other sites

1 hour ago, matjazp said:

$this->configData = $this->wire('modules')->getModuleConfigData($this);

I was getting errors on backend with this as first line in ready() method.

I just placed the below code at the bottom of the ready() method just like 1.1.1 and it works:

// optimize images in auto mode on resize
		if(isset($this->configData['optAutoAction']) && !$config->demo &&
			(in_array('optimize_variations', $this->configData['optAutoAction']) || in_array('optimize_variationsCI3', $this->configData['optAutoAction']))) {
			$this->addHookAfter('ImageSizer::resize', $this, 'checkOptimizeNeeded');
			$this->addHookAfter('Pageimage::size', $this, 'optimizeOnResize');
			if($this->wire('modules')->isInstalled('FieldtypeCroppableImage3') && in_array('optimize_variationsCI3', $this->configData['optAutoAction'])) {
				$this->addHookAfter('ProcessCroppableImage3::executeSave', $this, 'optimizeOnResizeCI3');
			}
		}

 

There are 2 issues:

1. The 260px variation gets optimized twice as I'm getting 2 logs for it with 1 second difference.
2. On the cropping screen of CI3 when I press the button "Proceed Crop" there is no indication that the image is currently optimizing.

Link to comment
Share on other sites

I tested this line as the first one in ready() and no errors were shown. But I forgot on  this: $config = $this->wire('config'); - put it just after $this->configData...

I'm getting just one log when an image is uploaded and the thumbnail is generated... Are you also using size() methods? 

I don't use CI3, bu t will check.

Link to comment
Share on other sites

49 minutes ago, matjazp said:

Are you also using size() methods? 

No.

On the screenshot are the logs I'm getting when upload an image. It seems that it's missing the CI3's automated variation from the "Crop Settings" but instead it optimize PW's automated variation twice.

autosmush.thumb.jpg.77cfc4c4249867f2be822222cac0f919.jpg

Link to comment
Share on other sites

What do you mean by:

On 1/20/2019 at 10:16 AM, PWaddict said:

The CI3's automated variation from the "Crop Settings" is still missing from the logs.

Currently CI3 variations are not optimized in auto mode, they only get optimized when you manually crop.

Link to comment
Share on other sites

Go to a CI3 image field. At the bottom of the Input tab there are the Crop Settings where we can add for example: thumbnail,1200,600. So when you upload an image that CI3 crop is automatically generated along with it's tiny variation 48px. And as you can see from the logs only that tiny CI3 48px is optimized in auto mode.

Please don't forget the 2nd problem: On the cropping screen of CI3 when I press the button "Proceed Crop" there is no indication that the image is currently optimizing.

Link to comment
Share on other sites

5 hours ago, PWaddict said:

Go to a CI3 image field. At the bottom of the Input tab there are the Crop Settings where we can add for example: thumbnail,1200,600. So when you upload an image that CI3 crop is automatically generated along with it's tiny variation 48px. And as you can see from the logs only that tiny CI3 48px is optimized in auto mode.

If I understand correctly: you would like that images created by CI3 (eg. yourimage.-thumbnail.jpg) also get optimized on upload? Currently, they are not since Horst is not using pageimage size method() so the hook is not triggered. I could optimize in imagesizer::resize(), but then you couldn't differentiate between CI3 and "normal" image, also I would have to rewrite some methods in Autosmush. On top of that, CI3 is executing resize() even if you cancel crop from the modal (if you press escape), there are some js errors... @horst

  • Sad 1
Link to comment
Share on other sites

On 1/17/2019 at 11:01 AM, matjazp said:

Hooks are applied only in admin template...

Is there any technical reason it can't be changed to support compression when an API call is made from a template file?

It seems to me it would make sense to be able to upload an image and leave this version untouched, but then automatically compress any resized images created via the API. Obviously this should only happen the first time they are created.

Without this, it seems to me that the only real option is resizing on upload which would affect the main image (which I don't think we should ever do). Otherwise site editors must manually crop the uploaded image to force a compression.

Am I missing something in how you guys use this in your site editors workflow?

Link to comment
Share on other sites

@PWaddict please try this version.

@adrian Don't know why hooks were only allowed inside admin template... I found it that way and just left it ? Uploaded version supports this, but you have to enable it, to maintain backward compatibility. Don't have much time to test, but it should be possible to do something like this:

echo $page->images->first()->width(400)->url;

and file should get optimized. More testing is needed...

AutoSmush.module

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Thanks @matjazp - that attached version does exactly what I am looking for - I would have thought this was the approach that most devs would want, so great to have it working.

The only thing that took me a while to figure out was that I needed both of these checked:

image.png.c39595fc1b546a48eb2b9bea8f33ec3d.png

I was initially testing with just the "Optimize on API resize/add" checked, but it also needed "Optimize on resize/crop" as well. Not sure if you want to make the second one greyed out until the first is checked, or remove the requirement for the first on line 1276. What do you think?

Thanks again for these updates - looking forward to seeing the committed! 

Link to comment
Share on other sites

My intention with this option was to prevent running the hooks in template files to maintain backward compatibility, currently hooks are applyed only in admin interface - don't want to change that as someone might use size() in template files but don't want to optimize images? So, by checking "Optimize on resize", you want images to be optimized in admin interface, and if you also check "Optimize in API" that means that optimize would happen in template files too. I'm not sure if this is how this new version is working actually, no time to test. But I see your point of view... Don't know what would be the best solution...

Link to comment
Share on other sites

8 hours ago, matjazp said:

@PWaddict please try this version.

Now it optimizes the CI3 crops in auto mode. Thank you.

The "Optimize on API resize/add" option is probably buggy? Sometimes I'm getting this error in the logs:

Quote

reSmush.it (auto): Error optimizing C:/laragon/www/mysite/site/assets/files/4100/test_a_1548188586.-thumbnail.400x0.jpg, 500 Internal Server Error (possible request timeout)

 

Link to comment
Share on other sites

1 hour ago, matjazp said:

Don't know what would be the best solution...

I think making the API checkbox indented and dependent on the Optimize on resize/crop makes most sense to me, but I don't really mind if it stays as is. It works, which is great!

I haven't tested, but I assume crop also works via the API? Perhaps the checkbox should mention that as well. 

One other thought - I am not sure I actually want it to optimize on "add" via the API - that suggests to me that  it would optimize the uploaded image which I don't want. I only want resized / cropped images optimized. I think there should be an option to prevent optimizing on API add if desired.

Thanks again.

Link to comment
Share on other sites

10 hours ago, matjazp said:

@PWaddict please try this version.

@adrian Don't know why hooks were only allowed inside admin template... I found it that way and just left it ? Uploaded version supports this, but you have to enable it, to maintain backward compatibility. Don't have much time to test, but it should be possible to do something like this:


echo $page->images->first()->width(400)->url;

and file should get optimized. More testing is needed...

AutoSmush.module

I never knew it didn't do this already?! I haven't been optimising me images when I thought they were optimised haha! I noticed the module had been updated today, however it doesn't seem to be this version. I'm happy to help test it.

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

×
×
  • Create New...