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

Write to standard out #56

Open
tielur opened this issue Mar 30, 2018 · 12 comments
Open

Write to standard out #56

tielur opened this issue Mar 30, 2018 · 12 comments

Comments

@tielur
Copy link

tielur commented Mar 30, 2018

Does this library have the ability to create an image and then write to standard out without having to interact with the file system?

@talklittle
Copy link
Collaborator

Not currently, but a pull request would be very welcome. I think most ImageMagick commands support writing to standard out.

@theangryangel
Copy link

I started looking at this as I kinda have a need for it as well. It looks fairly straight forward, but I'm struggling with what the API should look like (I think I've actually spent more time thinking about the API than it would to actually do the work based on what I'm looking at for the imagemagick docs 😓 )

For my usage I've got an inline base64 encoded image that I'd like to resize and get an inline output back.

The best idea I've come up with is to effectively pass the base64 encoded image to open, and populate the path. For the output, I'd do the same, if save is passed with an inline: true option. This would work, but it feels wrong because its not really a path anymore. Before I open up an editor and submit a PR I'd appreciate if anyone else has any better ideas that doesn't substantially change the api but also doesnt feel yucky.

@tielur
Copy link
Author

tielur commented Jun 1, 2018

The work I was doing that would involve this has been put on the back burner so I never looked much further into it. I'd love to see this implemented though and I'd be happy to help.

@theangryangel
Copy link

I spent an hour with this the other evening and its functionally working. Unfortunately;

  • I broke a load of tests
  • I'm not very pleased with the hacks and workarounds

Part of the pain is that a bunch of functions all assume the input and output are file system based and take actions based on extensions, the path, etc. which for my use case is a pain. For just output these mostly dont really matter and it's a special case in the save function, basically (output path of format:-, capture and a small bit of manipulation - literally thats it).

I'd like to come up with something a more elegant/cleaner, that allows both input and output without touching the file system, hopefully without breaking the API (although I'm not entirely convinced thats going to be possible and API design is my weakest skill).

I'm going away on my first proper holiday in several years next week (🎉 ), and I'm not sure how much time I'll be able to put into this until the end of June. If someone wants to pick up before then I won't get upset 😄

@kpanic
Copy link
Contributor

kpanic commented Nov 20, 2018

Hello @theangryangel -- I am interested about this issue, do you have a branch with the "hack"?
I might give it a try.

@kpanic
Copy link
Contributor

kpanic commented Nov 20, 2018

@theangryangel I opened #62

@talklittle
Copy link
Collaborator

talklittle commented Nov 25, 2018

Merged #62 which adds buffer support to create/2.

Before release I'd like to:

@route
Copy link
Member

route commented Nov 26, 2018

@talklittle thanks for you hard work! @kpanic thank you!

@talklittle
Copy link
Collaborator

I'm looking into mogrify with the -write - option to write to stdout, for the save/2 function. It kind of works except for a bug in the mogrify command-line application (tested with version 6.9.7-4) where it writes the buffer twice.

$ mogrify -write - -resize 100x100 bender.jpg | wc -c
8558

Compared with regular operation:

$ mogrify -write tmp.jpg -resize 100x100 bender.jpg
$ cat tmp.jpg | wc -c
4279

Work so far is at branch https://github.com/talklittle/mogrify/tree/save-buffer but I consider this upstream bug a blocker.

@kpanic
Copy link
Contributor

kpanic commented Dec 1, 2018

Same behaviour on 6.9.10-14

$ mogrify -write - -resize 100x100 bender.jpg | wc -c
8552
$ mogrify -write tmp.jpg -resize 100x100 bender.jpg
$ wc -c tmp.jpg 
4276 tmp.jpg
$ mogrify --version
Version: ImageMagick 6.9.10-14 Q16 x86_64 20181023 https://imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib

@lucacorti
Copy link

@talklittle I've opened an issue on imagemagick.

They are fixing it, but apparently you can pass +write instead of -write as a workaround for the double output.

@talklittle
Copy link
Collaborator

@lucacorti Thanks for opening the ImageMagick issue.

I tested the +write workaround (https://github.com/talklittle/mogrify/tree/save-buffer-pluswrite), and it works for the buffers, but with a side effect that breaks other functionality and Mogrify tests.

The documentation says (https://imagemagick.org/script/command-line-options.php#write):

To restore the image to its original state after writing it, use the +write filename option.

It apparently tells ImageMagick to rewrite the input image file to disk. This is a problem because it apparently can change the bytes and width/height of the original image. Tested on ImageMagick 6.9.7.

So unfortunately we can't use +write as an immediate workaround.

When the upstream fix becomes stable, we can merge the code to use buffers. We'll probably want to raise an error if trying to use the feature on older ImageMagick versions.

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

No branches or pull requests

6 participants