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

resize but keep proportion #23

Closed
oupala opened this issue Nov 11, 2019 · 13 comments
Closed

resize but keep proportion #23

oupala opened this issue Nov 11, 2019 · 13 comments
Labels

Comments

@oupala
Copy link

oupala commented Nov 11, 2019

I want to resize a serie of images that are in a directory. I'm using the following command:

npx sharp-cli resize 800 800 --withoutEnlargement true --optimise true --progressive true --withMetadata false --format input --input *.jpg --output "out/"

Images smaller than 800x800 are just copied in the output directory.

But bigger image are resized to 800x800 (square). What I wanted is that when an image is resized, its ratio is keeped: 800x600 for a landscape, and 600x800 for a portrait.

Have I missed something?

When I have a look at the documentation, I'm not sure if I have to use a height or width parameter or not:

sharp resize [height]

--height Number of pixels wide the resultant image should be [nombre]
--width Number of pixels high the resultant image should be [nombre]

@oupala
Copy link
Author

oupala commented Nov 12, 2019

I was looking for something like this imagemagick command:

convert -resize 640x640> *.jpg "${pattern}-%d.jpg"

@vseventer
Copy link
Owner

Try using the --fit inside option - see https://sharp.pixelplumbing.com/en/stable/api-resize/#resize

@oupala
Copy link
Author

oupala commented Nov 12, 2019

Thanks a lot, that was what I was looking for.

Is there a way to overwrite the input file, or to set a pattern for naming the out files (something like ${original_name}-resized.jpg)?

All what I saw on the README file is that sharp-cli is made for one file at a time (even if bash car use jokers to change multiple files at a time).

You can have an example of such pattern in the imagemagick command above.

And it seems that overwriting is explicitly forbidden by the cli. Here is an error of the cli when trying to overwrite an input file:

Cannot use same file for input and output

@vseventer
Copy link
Owner

vseventer commented Nov 13, 2019

You can use url-templates for this - for example sharp resize 800 600 --fit inside -i ./src/*.jpg -o ./dest/{name}{ext}. Batch processing is supported.

@oupala
Copy link
Author

oupala commented Nov 13, 2019

Is there a way to use sharp and url-templates directly in a CLI?

Or is it mandatory to create a js script to use both of them in cunjunction?

@vseventer
Copy link
Owner

Yes, you can use this right from the CLI - see my last reply for an example.

@oupala
Copy link
Author

oupala commented Nov 13, 2019

You mean, I juste have to install url-templates globally, then to enter the following command:

sharp resize 800 600 --fit inside -i ./src/*.jpg -o ./dest/{name}{ext}

and {name} and {ext} will be automatically defined?

It is still not possible to overwrite the source file with this solution, right?

@vseventer
Copy link
Owner

vseventer commented Nov 14, 2019

Everything already comes with the CLI - just try and run the command in my example above. Overwriting the source file is not supported.

@oupala
Copy link
Author

oupala commented Nov 14, 2019

You're right, I tested it and it works perfectly, impressive! Is this feature documented somewhere?

The only thing I am missing is overwriting. What a pity... I'll have to find a workaround.

@vseventer
Copy link
Owner

vseventer commented Nov 14, 2019

The CLI used to be able to do that, but I removed support for it since it led to problems with large files. Large files work great with streams, but we cannot use streams when saving to the same file.

I might be able to add a --overwrite flag in the CLI, which when set would overwrite the input file. Gotcha would be that you should use this flag only for small files.

@oupala
Copy link
Author

oupala commented Nov 14, 2019

I understand the limitation that you encountered.

But I would be very happy to see this feature back in sharp.

@vseventer
Copy link
Owner

I created this request in #24. Closing this.

@mathieu-aubin
Copy link

you could pipe output to 'sponge' and save as same file. Otherwise, input file gets destroyed before reading

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

No branches or pull requests

3 participants