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

Optimize from raw image data, not a file #25

Closed
ippa opened this issue Jan 22, 2014 · 7 comments
Closed

Optimize from raw image data, not a file #25

ippa opened this issue Jan 22, 2014 · 7 comments

Comments

@ippa
Copy link

ippa commented Jan 22, 2014

First, thanks for a great lib!

Sometimes in scripts, I already have the imagedata, maybe I've cut up thumbnails for a bigger image or whatever.

It would be nice to be able to optimize that imagedata Without having to first save it to a file so image_optim can read it.

Maybe a new method to not break the current API:
image_optim.optimize_image_data( image_data )

@toy
Copy link
Owner

toy commented Jan 22, 2014

You are welcome!

Processing of images as data is already there. You've guessed method name optimize_image_data correctly, also there is optimize_images_data. I just forgot to mention it in readme … already fixed.

@toy toy closed this as completed Jan 22, 2014
@ippa
Copy link
Author

ippa commented Jan 22, 2014

oh, heh, funny :). Now I've replaced:

io = IO.popen("/usr/bin/jpegtran -optimize /proc/self/fd/0", "r+").binmode
io.syswrite thumbnail.to_blob
io.flush
io.close_write
optimized_thumbnail = io.read
io.close_read

With:
optimized_thumbnail = @image_optim.optimize_image_data(thumbnail.to_blob)

When I'm saving with active record 4.0.2 the script that just worked suddenly crashes with "Error: invalid byte sequence in UTF-8" .. is there any gotcha in what format optimize_image_data() returns the optimized data?

@ippa
Copy link
Author

ippa commented Jan 22, 2014

Hm, ok. The IO.popen-hack returned ASCII-8BIT and optimized_image_data returns UTF-8. I'll have to dig further on my side why it crashes my database save, not a image_optim problem. Thanks again!

@toy
Copy link
Owner

toy commented Jan 22, 2014

In reality optimize_image_data should return binary data. I'll put it into master after testing.

@toy
Copy link
Owner

toy commented Jan 22, 2014

v0.10.1 out

@ippa
Copy link
Author

ippa commented Jan 22, 2014

Things are moving fast, I like it :). Just got bitten by another thing ...optimize_image_data seems to return nil if no optimization is done?

Maybe that's the desired behavior? I was just expecting it to return imagedata as optimized as possible (sometimes that means the original image).

@toy
Copy link
Owner

toy commented Jan 22, 2014

nil is returned to indicate failure. Otherwise you would have to compare result with original to determine success. optimize_image and optimize_image! also return nil on failure. If you don't care if optimization was successful or not just use image_optim.optimize_image_data(data) || data.

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