0

I tried (with some success)

require 'open-uri'
require 'chunky_png'

image_url = "http://res.cloudinary.com/houlihan-lokey/image/upload/c_limit,h_75,w_120/ixl7z4c1czlvrqnbt0mm.png"
# image_url = "http://res.cloudinary.com/houlihan-lokey/image/upload/c_limit,h_75,w_120/zqw2pgczdzbtyj3aib2o.png" # this works

image_file = open(image_url)
image = ChunkyPNG::Image.from_file(image_file)
puts image.width

Some images work, others don't. The error:

TypeError: no implicit conversion of StringIO into String
from /Users/theuser/.rvm/gems/ruby-2.0.0-p247/gems/chunky_png-1.3.3/lib/chunky_png/datastream.rb:66:in `initialize'
from /Users/theuser/.rvm/gems/ruby-2.0.0-p247/gems/chunky_png-1.3.3/lib/chunky_png/datastream.rb:66:in `open'
from /Users/theuser/.rvm/gems/ruby-2.0.0-p247/gems/chunky_png-1.3.3/lib/chunky_png/datastream.rb:66:in `from_file'
from /Users/theuser/.rvm/gems/ruby-2.0.0-p247/gems/chunky_png-1.3.3/lib/chunky_png/canvas/png_decoding.rb:53:in `from_file'
from (irb):5
from /Users/theuser/.rvm/rubies/ruby-2.0.0-p247/bin/irb:16:in `<main>'

I will be running this on Heroku and am wondering -- is there a reliable way to achieve this without creating temporary files?

aaandre
  • 2,502
  • 5
  • 33
  • 46
  • Can you tell what you mean with "don't work"? Is there an error, something in the logs? If it does not work, are you able to load that file locally, without redirections? – spickermann Nov 19 '14 at 03:25
  • @spickermann, I added the error message above. – aaandre Nov 19 '14 at 17:56

1 Answers1

1

The issue was with files which were too small for open to create a temp file for.

The solution is to not rely on temp files but to read the image into memory and use ChunkyPNG's Image.from_blob:

require 'open-uri'
require 'chunky_png'

image_url = "http://res.cloudinary.com/houlihan-lokey/image/upload/c_limit,h_75,w_120/ixl7z4c1czlvrqnbt0mm.png"

image_file = open(image_url).read
image = ChunkyPNG::Image.from_blob(image_file)
puts image.width

This may not work with large images, but is OK for my application.

aaandre
  • 2,502
  • 5
  • 33
  • 46