Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate two different versions of the same image #19

Closed
arggh opened this issue Jan 2, 2017 · 8 comments
Closed

Generate two different versions of the same image #19

arggh opened this issue Jan 2, 2017 · 8 comments

Comments

@arggh
Copy link

arggh commented Jan 2, 2017

Is there an easy way to create two different versions, for example one with the specific target resolution and one with the same resolution doubled, named respectively image@1x.jpg and image@2x.jpg?

I gave it a quick shot, and I ended up with:

image@1x.jpg image@1x@2x.jpg and image@2x.jpg.

I think this is quite a common use case.

@arggh
Copy link
Author

arggh commented Jan 2, 2017

Got it done with this matcher: **/*(!(*@1x)).@(jpg|jpeg|png) and doing a second pass with Sharp, but it makes my build process duration jump from couple of seconds to over a minute.

@arggh
Copy link
Author

arggh commented Jan 2, 2017

Got the speed back by doing a little tweak, final matcher: **/*(!(*@1x.)+(jpg|jpeg|png)).

Maybe a similar example matcher should be in the docs? (or then I couldn't figure out the "proper" way to accomplish this)

edit: I guess one could also use the metalsmith-branch plugin to achieve this.

@axe312ger
Copy link
Owner

axe312ger commented Jan 3, 2017

See the tests here, where I create multiple versions of one image:
https://github.com/axe312ger/metalsmith-sharp/blob/master/test/index.test.js#L168

Alternatively, you can tell the plugin NOT to move files in the first step, so you do another image manipulation different naming. But calling the plugin twice might extend the compile time.

By default, the image is moved, thats why you had to use such a complex matcher.

See here: https://github.com/axe312ger/metalsmith-sharp#movefile

@arggh
Copy link
Author

arggh commented Jan 3, 2017

I had disabled moving, the problem was that I didn't know I can do this:

.use(sharp([{
        src: '**/*.@(jpg|png|jpeg)',
        namingPattern: '{dir}/{name}@1x.jpg',
        methods: [{
          name: 'resize',
          args: [768]
        }, {
          name: 'toFormat',
          args: [ 'jpeg' ]
        }],
        moveFile: false
      }, {
        src: '**/*.@(jpg|png|jpeg)',
        namingPattern: '{dir}/{name}@2x.jpg',
        methods: [{
          name: 'resize',
          args: [1536]
        }, {
          name: 'toFormat',
          args: [ 'jpeg' ]
        }],
        moveFile: false
      }]))

So, I would add a section to the docs explaining, that to achieve multiple versions of the same files you can give sharp an array of objects as parameter, instead of only an object.

@axe312ger
Copy link
Owner

yes, sure, this is a pretty new feature of the plugin, i missed it in the docs

@axe312ger
Copy link
Owner

i added example code to the usage section in the readme :)

@arggh
Copy link
Author

arggh commented Jan 3, 2017

Nice, that should clear things up!

@arggh
Copy link
Author

arggh commented Jan 3, 2017

Also, thanks for a great plugin :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants