matjazp

Auto Smush

Recommended Posts

How are you resizing images? In admin (backend) or in template file via API?

Share this post


Link to post
Share on other sites
3 minutes ago, matjazp said:

How are you resizing images? In admin (backend) or in template file via API?

API

Share this post


Link to post
Share on other sites
26 minutes ago, matjazp said:

Hooks are applied only in admin template...

Then why the automatically generated thumbnail (260px) after you uploaded an image doesn't get optimized?

Share this post


Link to post
Share on other sites

On mobile so I cant test. Are you sure its not optimised? What log file says?

Share this post


Link to post
Share on other sites
8 hours ago, matjazp said:

On mobile so I cant test. Are you sure its not optimised? What log file says?

Yep. The log says only about the original image. Nothing about any other variations.

Share this post


Link to post
Share on other sites

Same problem happens on "Optimize on resize/crop for CI3" too. So, in Automatic Mode only "Optimize on upload" is actually working.

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
11 hours ago, matjazp said:

@PWaddict please tr y an attached version, let me know if optimize on resize is working and if double optimization of thumbnails is fixed. 

AutoSmush.zip

It's fixed. Thanks.

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

In my testing I don't get tiny variation 48x. I have set portrait,200,600 in filed settings. To be honest, I don't use CI3 ...

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
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! 

Share this post


Link to post
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...

Share this post


Link to post
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)

 

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Rudy
      Hi,
      I am writing a custom module that requires storing array of settings in the module config/settings.
      Is there a built-in fieldtype that allows me to store settings in an array (sort of like Repeater)?
      I tried using InputfieldAceExtended and InputfieldTextarea to store JSON array. Both times, my data was stored in the module settings (in the database) but upon reload, that information was not retrieved.
      Thanks
      Rudy
       
    • By Anssi
      A simple module to enable easy navigation between the public and the admin side of the site. After installation a green bar will appear to the upper side of the screen, containing a few navigation elements and displaying the PW version number.
      Heavily inspired by @apeisa's great AdminBar (Thanks!). I needed a bit simpler tool for my projects and as a result, this was made. Available on GitHub .

    • By Sebi
      I've created a small module which lets you define a timestamp after which a page should be accessible. In addition you can define a timestamp when the release should end and the page should not be accessable any more.
      Github: https://github.com/Sebiworld/PageAccessReleasetime
      Usage
      PageAccessReleasetime can be installed like every other module in ProcessWire. Check the following guide for detailed information: How-To Install or Uninstall Modules
      After that, you will find checkboxes for activating the releasetime-fields at the settings-tab of each page. You don't need to add the fields to your templates manually.
      Check e.g. the checkbox "Activate Releasetime from?" and fill in a date in the future. The page will not be accessable for your users until the given date is reached.
      If you have $config->pagefileSecure = true, the module will protect files of unreleased pages as well.
      How it works
      This module hooks into Page::viewable to prevent users to access unreleased pages:
      public function hookPageViewable($event) { $page = $event->object; $viewable = $event->return; if($viewable){ // If the page would be viewable, additionally check Releasetime and User-Permission $viewable = $this->canUserSee($page); } $event->return = $viewable; } To prevent access to the files of unreleased pages, we hook into Page::isPublic and ProcessPageView::sendFile.
      public function hookPageIsPublic($e) { $page = $e->object; if($e->return && $this->isReleaseTimeSet($page)) { $e->return = false; } } The site/assets/files/ directory of pages, which isPublic() returns false, will get a '-' as prefix. This indicates ProcessWire (with activated $config->pagefileSecure) to check the file's permissions via PHP before delivering it to the client.
      The check wether a not-public file should be accessable happens in ProcessPageView::sendFile. We throw an 404 Exception if the current user must not see the file.
      public function hookProcessPageViewSendFile($e) { $page = $e->arguments[0]; if(!$this->canUserSee($page)) { throw new Wire404Exception('File not found'); } } Additionally we hook into ProcessPageEdit::buildForm to add the PageAccessReleasetime fields to each page and move them to the settings tab.
      Limitations
      In the current version, releasetime-protected pages will appear in wire('pages')->find() queries. If you want to display a list of pages, where pages could be releasetime-protected, you should double-check with $page->viewable() wether the page can be accessed. $page->viewable() returns false, if the page is not released yet.
      If you have an idea how unreleased pages can be filtered out of ProcessWire selector queries, feel free to write an issue, comment or make a pull request!
    • By David Karich
      Thanks to the great Pro module "RepeaterMatrix" I have the possibility to create complex repeater items. With it I have created a quite powerful page builder. Many different content modules, with many more possible design options. The RepeaterMatrix module supports the cloning of items, but only within the same page. Now I often have the case that very design-intensive pages and items are created. If you want to use this module on a different page (e.g. in the same design), you have to rebuild each item manually every time.
      With this proof of concept I have created a module which adds the feature to copy a repeater item to the clipboard so that you can paste this item to another page with the same repeater field. The module has been developed very rudimentarily so far. It is currently not possible to copy nested items. There is also no check of Min/Max. You can also only copy items that have the same field on different pages. And surely you can solve all this more elegantly with AJAX. But personally I lack the deeper understanding of the repeaters. Also missing on the Javascript side are event triggers for the repeaters, which would make it easier. Like e.g. RepeaterItemInitReady or similar.
      it would be great if @ryan would implement this functionality in the core of RepeaterMatrix. I think he has better ways to implement this. Or what do you think, Ryan?
      Everybody is welcome to work on this module and improve it, if it should not be integrated into the matrix core. Therefore I put it for testing and as download on GitHub: https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDublicate
      You can best see the functionality in the screencast: 
       
    • By anderson
      Hi,
      Please take a look at this:
      https://templatemag.com/demo/Good/
      The upper nav bar, including dropdowns like "pages" and "portfolios", what do you call this whole thing? At first I guess it's called "dropdown nav bar", but seems not.
      AND of course, what's the simplest way/module to achieve this in PW?
      Thanks in advance.