Jump to content

Image quality


sambadave
 Share

Recommended Posts

Hi folks

I recently put a site live and in the last stages of the project decided to do some image optimisation.

$options = array(
   'quality' => 70,
   'upscaling' => false
);
$pageImageLargeDesktop = $pageImage->size(960,720,$options)->url;

The thing I love about ProcessWire is that you can change the size here and the new images will be generated across all pages. It's such a time saver. However after adding in $options parameters I expected the quality of the images to reduce on a page refresh, howeever the quality settings only seem to be applied during upload. This meant I had to re-upload all my images again into ProcessWire for the changes in quality to appear.

Is there a way of getting round this? It wasn't a massive problem for this particular site but I'm thinking more along the lines of a larger site with hundreds or thousands of images. You just couldn't go back in and re-upload every image unless you had a lot of time on your hands.

Am I missing a trick? Thanks in advance for any help.

Link to comment
Share on other sites

Hi sambadave,

as far as I know you don't have to upload the file again. You could go into the files folder and delete only the generated versions of the images.

For example if there is an image "image.jpg" and it has generated versions like "image-600x400.jpg" and so on, you can search all images with "-600x400" delete them and after reloading the site, ProcessWire generates new versions of this image.

You can try it out, but be carefull not to delete the original. Maybe make a backup before and be aware, that generating a lot of images could lead to a timeout when reloading the page, so you have to reload the page several times.

Regards, Andreas

  • Like 1
Link to comment
Share on other sites

Hi,

I don't have great knowledge of the processwire core, but I think that the following lines in wire/core/Pageimage.php are responsible:

$basename = basename($this->basename(), "." . $this->ext());
...
$basename .= '.' . $width . 'x' . $height . $crop . $suffixStr . "." . $this->ext();
$filenameFinal = $this->pagefiles->path() . $basename;
$exists = file_exists($filenameFinal);

if(!$exists || $options['forceNew']) {
  // Generate and save
...

That means, that indeed the image options are not a dependency for the image file generation.

One can argue about that, but I think that they should be.

Link to comment
Share on other sites

Hi guys

Thanks Andreas – Good to know I can do that. I'm just a little concerned for larger sites as going through all the image folders to delete certain images could take ages and might involve an element of risk if I was to delete the original image by mistake. Unless there is a way to do this with code?

tsdtsdtsd – I wonder if there could be a way to toggle this setting through the admin to allow options to be taken into account.

It's not a biggie now I know but it would be a shame to have a huge site and have to go through all the images manually. ProcessWire is still awesome though!

Link to comment
Share on other sites

It's always as a project gets big you'll have to take care. No matter what CMS.

In case of images:

a.  you can now in 2.6 delete variations for a image right in the backend. But that doesn't work for batch processing a whole site's images.

b. you can write a script to delete all variations through API, but that as soon as there's 1000's of pages involved you have to take care of memory, script time etc. Bit this if you own ListerPro (which I recommend all PW users should buy anyway) you can use and build an custom action that batches all your pages (or the ones you filtered) to remove variations. It's built with scalability in mind so it scales for millions of pages (just would take a very long time). 

c. you can also use the API when using $page->image->removeVariations() to delete and force new generation of variations. Then use a template var to toggle that globally in your template. Usually a custom $config var would be best as it allows also modules to read it. This works fine when building a site and you want to experiment to get the best options. Maybe not always suited but something like:

$config->forceNewVariation = true;

The always use a code like this where you generate thumbs or sizes.

$img = $page->image;
if($config->forceNewVariations) $img->removeVariations();
$img = $img->size(100, 100, $options);

Of course this doesn't work well if you're live and have 1000 of pages. As you'd have to view all pages again to trigger new creation WHILE the setting is true.

Just a few ideas.

  • Like 4
Link to comment
Share on other sites

Maybe there's some new settings in core that would handle that if the setting quality changes it will force a new generation (which would be desired anyway) 

Maybe Horst knows as he worked on that one. I think the new naming convention includes quality, which would make that work.

  • Like 1
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...