Coder Social home page Coder Social logo

akqa-nz-silverstripe-optimisedimage's Introduction

SilverStripe Optimised Image

This module provides two image manipulation services that can be configured independently:

  • Run command-line image optimisation tools on images resampled by SilverStripe: reduces the file size of the generated images without reducing fidelity in a noticeable way
  • Resize images on upload to fit within configured dimensions: prevents images with massive dimensions entering the assets and becoming a memory problem when resampling.

For a SilverStripe 2.4 version check the 0.1 branch.

Configurations for some common image optimisation tools (eg. jpegoptim and optipng) are provided as part of this module, though any command line program can be used with the image optimisation service. No optimisation program binaries are bundled with this module, so you'll need to install any programs you want to use on the target system.

Note that only resampled images are optimised by the optimisation service module (eg. using CroppedImage, SetWidth, SetHeight, PaddedResize, etc). If you want to optimise images without resizing them, there is currently a workaround to trigger optimisation.

Installation (with composer)

$ composer require heyday/silverstripe-optimisedimage

Usage

Resampling Images on upload

By default, resampling on upload is not enabled. To activate it, the ResampleImage extension needs to be added to Image. In your mysite/_config/config.yml add:

Image:
  extensions:
    - ResampleImage

The default maximum width & height for uploaded images is 1024 x 1024 pixels. Images larger than this will be scaled to fit their largest dimension to this size.

You can set your own maximum height and width for uploaded images by overriding the default config in your mysite/_config/config.yml:

ResampleImage:
  max_x: 2000
  max_y: 2500

Note that resampling on upload is a destructive process: the original uploaded image is discarded.

Optimising SilverStripe Resampled Images - Selecting a backend

The image backend that SilverStripe uses needs to be changed to either OptimisedGDBackend or OptimisedImagickBackend depending on whether you want to us GD or Imagick. To do this, in your mysite/_config/config.yml add:

Image:
    backend: OptimisedGDBackend

// or

Image:
    backend: OptimisedImagickBackend

Configuration options:

  • enabledCommands
  • availableCommands
  • binDirectory
  • optimisingQuality

At the very least enabledCommands needs to be overridden in your own config in order to enable the optimising of images resampled by SilverStripe.

This can be done as follows,

  1. Create a file mysite/_config/optimisedimage.yml
  2. Add the following contents
---
After: 'silverstripe-optimisedimage/config#core'
---
ImageOptimiserService:
  enabledCommands:
    - jpegoptim
    - optipng

If you want to add your own commands, you can override availableCommands e.g.

---
After: 'silverstripe-optimisedimage/config#core'
---
ImageOptimiserService:
  enabledCommands:
    - jpegoptim
    - optipng
  availableCommands:
    jpg:
      jpegoptim: '%s/jpegoptim -p --strip-all --all-progressive %s'
    png:
      optipng: '%s/optipng %s -o 1 -strip all -i 1'
    gif:
      optipng: '%s/optipng %s -o 1 -strip all'

If your binaries are not located at /usr/local/bin/ you can override this by setting binDirectory

---
After: 'silverstripe-optimisedimage/config#core'
---
ImageOptimiserService:
  enabledCommands:
    - jpegoptim
    - optipng
  binDirectory: '/home/user/bin/'

If your binaries aren't all located in the same directory you will need to manually enter the command by overriding availableCommands

---
After: 'silverstripe-optimisedimage/config#core'
---
ImageOptimiserService:
  enabledCommands:
    - jpegoptim
    - optipng
  availableCommands:
    jpg:
      jpegoptim: '/my/special/path/jpegoptim -p -m%3$d --strip-all %2$s'

Commands have certain variables exposed to them when they are built, this is done by sprintf, the variables available are in the following order:

  1. binDirectory
  2. File path to be optimised
  3. optimisingQuality

If when defining a custom command you need to use these arguments in a different order, you will need to use a position specifier, see PHP sprintf

License

Optimised image is licensed under an MIT license

akqa-nz-silverstripe-optimisedimage's People

Contributors

camspiers avatar stecman avatar sn4h avatar mschilder avatar

Stargazers

 avatar Bram de Leeuw avatar Tim avatar Design City avatar Mikaela Young avatar Anton Smith avatar Christopher Darling avatar Anselm Christophersen avatar  avatar John Slipper avatar  avatar Jason Irish avatar Petar Simic avatar Chris Bacon avatar Mark Guinn avatar Matt Bailey avatar Gordon Anderson avatar Nic avatar Nicolas Cusan avatar LiveSource avatar James Cocker avatar  avatar Pieter Vanderwerff avatar Pali Ondras avatar Will Morgan avatar Loz Calver avatar  avatar Luke Fromhold avatar Frank Mullenger avatar Milan Jelicanin avatar

Watchers

 avatar Luciano A. Di Giuseppe avatar Will Rossiter avatar James Cocker avatar Glenn Bautista avatar Bohonyi Balazs - Zsolt avatar James Cloos avatar  avatar David Montgomery avatar Jason Loeve avatar Cormac lynch avatar Peter Demulsant avatar Tim Kung avatar Stevie Mayhew avatar  avatar Ben Dubuisson avatar Tim Higgs avatar Nicholas Byfleet avatar Sreejish avatar Lucas avatar  avatar  avatar Digitweaks Studio avatar Clément Tartar avatar tiffany.nguyen avatar Jack Conner avatar Leandro Rodrigues avatar  avatar Matt Peel avatar

akqa-nz-silverstripe-optimisedimage's Issues

Resizing Already Uploaded Images

I often end up adding this module to a site after the owners have gone and batch uploaded many high res images via SilverStripe and maxed out their disk space. This deals with any future uploads, but for the images that have already been uploaded, I end up downloading them, resizing them locally and then reuploading them, overwriting the high res versions.

Is there any way to batch run a resize on images that had been uploaded before the module was installed?

inclusion of binary tools

Thanks for this module, I really like this idea. Just one question: is there a reason why you don't include the binary tools as thirdparty files?
Cheers

Class Raven does not exist

Hi

I'm just trying to to test this locally. I'm getting the error:

[User Error] Uncaught ReflectionException: Class Raven does not exist

Shouldn't raven be supported rather than required?

Will

PHP Notice: getimagesize(): Read error!

The getimagesize throws exeption when used with CloudAssets module.

When uploading completes a error message as this shows in the upload field:
Object->__call(): the method 'parent' does not exist on 'Image'

The image get saved on the server and if you have configured the upload field to overwrite existing a second upload will go through and work fine.

Wrapping a try-catch around getImagesize in ImageOptimiserService::optimiseImage [43] fixes the issue.
E.g like this:
try { list($width, $height, $type, $attr) = getimagesize($filename); } catch (Exception $e) { $this->logger->error( "Unable to get image size.", array( 'exception' => $e ) ); }

I can create a pull request if this looks ok?

Global jpegopti

I have jpegopti and pngopti installed so i can just type jpegopti in the terminal, no need for a path.
I can't seem to allow for that, if i define binDirectory: '' (empty string) it's looks for /jpegopti.
Would it be possible to have only the command called? No need for different settings across servers then

composer.json - require SS:3.x, symfony/process:3.x

Please update the composer.json to require v3 of SilverStripe and check if it can be installed with latest Symfony/process:3.x

composer require heyday/silverstripe-optimisedimage
Using version ^2.2 for heyday/silverstripe-optimisedimage
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: remove symfony/process v3.3.6
    - Conclusion: don't install symfony/process v3.3.6
    - heyday/silverstripe-optimisedimage 2.2.0 requires symfony/process ~2.3 -> satisfiable by symfony/process[2.3.x-dev, 2.4.x-dev, 2.5.x-dev, 2.6.x-dev, 2.7.x-dev, 2.8.x-dev].
    - heyday/silverstripe-optimisedimage 2.2.1 requires symfony/process ~2.3 -> satisfiable by symfony/process[2.3.x-dev, 2.4.x-dev, 2.5.x-dev, 2.6.x-dev, 2.7.x-dev, 2.8.x-dev].
    - heyday/silverstripe-optimisedimage 2.2.2 requires symfony/process ~2.3 -> satisfiable by symfony/process[2.3.x-dev, 2.4.x-dev, 2.5.x-dev, 2.6.x-dev, 2.7.x-dev, 2.8.x-dev].
    - Can only install one of: symfony/process[2.3.x-dev, v3.3.6].
    - Can only install one of: symfony/process[2.4.x-dev, v3.3.6].
    - Can only install one of: symfony/process[2.5.x-dev, v3.3.6].
    - Can only install one of: symfony/process[2.6.x-dev, v3.3.6].
    - Can only install one of: symfony/process[2.7.x-dev, v3.3.6].
    - Can only install one of: symfony/process[2.8.x-dev, v3.3.6].
    - Installation request for symfony/process (locked at v3.3.6, required as ^3.3) -> satisfiable by symfony/process[v3.3.6].
    - Installation request for heyday/silverstripe-optimisedimage ^2.2 -> satisfiable by heyday/silverstripe-optimisedimage[2.2.0, 2.2.1, 2.2.2].

Command works from commandline, but script does nothing

I'm having some issues, and i don't really know how to debug this stuff.
When i do a flush=1 images are created.
Added an echo sprintf($command ... which gives me a string like
jpegoptim -p -m80 --strip-all '/Applications/MAMP/htdocs/flammen-cms/assets/HeroImages/_resampled/CroppedFocusedImage62080050-61-header4.jpg
The file is not optimized though. If i take the string and run it in commandline, it optimizes the image.

Is this a problem with me using CroppedFocusedImage? or how can i debug?

Resample Image doesn't respect/retain exif orientation data

Very useful extension, cheers! My only problem is finding way I can get a resampled image but also rotate as per the exif orientation data (for something like a photo from a digital camera). This seems to be done by default in SilverStripe these days but is lost when using resampling here. Can this be done?

I've tried also using the silverstripe-fixjpeg-orientation module alongside this but no joy. I'm guessing they have conflicting onAfterUpload functions possibly?

SS4 support

Is anyone working on SS4 support and could give some timelines? Or provide a cost to get it upgraded, I have a motivated client.

UploadField: SyntaxError: Unexpected token <

Today I gave your repo a go. Have installed libjpeg-9a, jpegoptim-1.4.1 and optipng-0.7.5 on my Mavericks Mac on a SilverStripe 3.1.5 installation.

Contents of /mysite/_config/optimisedimage.yml:


---
After: 'silverstripe-optimisedimage/config#core'

---
ImageOptimiserService:
  enabledCommands:
    - jpegoptim
    - optipng

Contents of /mysite/_config.php:

(...)
// Enable jpegoptim and optipng image optimisation
Image::set_backend("OptimisedGDBackend");

Now, when I upload an Image using a CMS UploadField the file is uploaded and stored, BUT the file is not optimised and in the UploadField area the following error is shown: SyntaxError: Unexpected token <

How to fix this?!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.