I'm a new user of this module for D8 and I've set up a few image styles to use a resmush.it pipeline with the 2016-Oct-05 dev version, which seem to work, although I'm not seeing a huge difference in resulting filesizes between resmush.it and the imagick library I was using.

I was primarily interested in optimizing the filesize of the "original" image upon uploading (this is typically where we can save most space), but I do not see any significant optimization happening there and I can't find a way to set a pipeline for this image size, since there is no original style... I was expecting to find something on /admin/config/media/image-toolkit or on the admin content-type field form where a max resolution can be set, but I haven't found it. Did I miss anything?

Comments

juliencarnot created an issue. See original summary.

jcisio’s picture

Title: use pipeline to optimize original image » Possibility to optimize original image if it is resized
Category: Support request » Feature request

There is no such option now, but I can see it is a useful feature to add.

juliencarnot’s picture

Thanks for your quick answer! In my view, it could be nice to have the option to optimize even if no resize is carried out, would that be possible too?

jcisio’s picture

I haven't evaluated the feasibility of either options, in either D7 or D8. This is just an open request request.

Steven Jones’s picture

Drupal tends to have a policy of not changing files as they are uploaded, but rather doing the processing when someone requests the file.
That way, the original is preserved in case you need it for any reason.

If you just want the original image, but optimized, then you just need to use an image style that doesn't have any actions, but then select and image optimize pipeline on it. It'll go through image styles in the usual way, remain the same pixel size, but then go through and be optimized.

As for the optimization levels of reSmush.it, yeah, they aren't great. There are alternatives like Kraken and Tiny PNG that tend to do a better job, the former has integration with the D8 version of this module.

juliencarnot’s picture

Thanks for your feedback Steven!

I perfectly understand the idea behind keeping the original file in its original state, but for a user contributed website, I can't expect users to upload a sensible sized image and this can result in hosting huge jpgs that nobody will need to display. I'm also investigating client-side resize with js lib such as FineUploader (see http://docs.fineuploader.com/branch/master/features/scaling.html ) to avoid uploading a big file just to squash it, which would make a lot of sense in a mobile setting.

Drupal 8 is already offering to replace the original image by setting max width and heights on the image field config, so that's why I was expecting Image Optimize to offer to offer it at this level too.

For optimization levels, I tried Kraken's web interface and had impressive results but I'll also look at jpegtran. At the moment, it looks like relying on reSmush.it is not worth the benefits it's offering.

Steven Jones’s picture

Title: Possibility to optimize original image if it is resized » Optimize images on file upload

Drupal 8 is already offering to replace the original image by setting max width and heights on the image field config, so that's why I was expecting Image Optimize to offer to offer it at this level too.

Fair enough, maybe we should be able to optimize on file upload too.

W.M.’s picture

I agree this is a pretty handy feature .. I saves time for content authors surely, since the optimization happens on upload & server-side. Hope this will be available for both Drupal 7 and 8. Thank you very much for this valuable module.

juliencarnot’s picture

Dom.’s picture

@juliencarnot: thanks for the link to this issue. I also second that: I have multiple clients who just upload huge imags (sometimes 10+MB images directly from their camera) and who are not willing to change their contribution flow. This is a nightmare to keep optimized / clever sized image sizes with them.

W.M.’s picture

Any progress on this? I still believe that such an option should be available.

At the meantime, I am considering running a files watcher which auto optimizes the uploaded images immediately as they get saved to the server.

FR6’s picture

I forced the generation of the image styles directly on upload this way:

// When an entity is created or saved
function MYMODULE_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
	if($entity->getEntityTypeId() == 'file') {
		$uri = $entity->getFileUri();
		$image = \Drupal::service('image.factory')->get($uri);

		if ($image->isValid()) {
			$styles = ImageStyle::loadMultiple();

			// Generate all styles
			foreach ($styles as $st) {
				$destination = $st->buildUri($uri);
				$st->createDerivative($uri, $destination);
			}
		}
	}
}

Hope it helps someone.

mmv93’s picture

You can use such code to optimize original image:

function mymodule_entity_presave(EntityInterface $entity) {
  if($entity->getEntityTypeId() == 'file') {
    $uri = $entity->getFileUri();
    $image = \Drupal::service('image.factory')->get($uri);

    if ($image->isValid()) {
      $pipeline = ImageAPIOptimizePipeline::load('tinypng');
      if ($pipeline instanceof ImageAPIOptimizePipeline) {
        $pipeline->applyToImage($uri);
      }
    }
  }
}
chike’s picture

Thanks @mmv93 #13 worked.

In my use case I still need one more help. #13 passes the image through an optimization pipeline so the image got optimized but I equally want to resize the images. This would have been achieved if #12 worked but it didn't work for me likewise all attempts I made to pass the image through an image style. I would appreciate if there is a way to resize the image as well.

Thank you.

mably’s picture

+1 It would definitely be an interesting feature to have.

@chike this module might be what you are looking for: https://www.drupal.org/project/image_style_on_upload

chike’s picture

Thank you @mably that's exactly what I am looking for.