title: Rendering an image
body: <p>So this should be pretty easy, yet I can't get it work.</p>
  
  <p>I have a controller method that finds an image based on a query, then the output gets cached. The image could be remote (flickr, google images, etc) or it could be local. Regardless of the source, I just need take the image file contents, and pass it through to the user. In essence, a proxy. Passing through remote images seems to work fine, but passing through local images gives me a:</p>
  
  <pre><code>invalid byte sequence in UTF-8
  </code></pre>
  
  <p>So here's what I got. I'm hoping someone can solve the problem or guide me in a better direction with my code.</p>
  
  <pre><code>def image_proxy
    query = params[:query]
    image_url = get_image_url(query) # returns an absolute local file path or a URL
  
    response.headers['Cache-Control'] = "public, max-age=#{12.hours.to_i}"
    response.headers['Content-Type'] = 'image/jpeg'
    response.headers['Content-Disposition'] = 'inline'
    render :text =&gt; open(image_url).read
  end
  </code></pre>
  
  <p>Remote files work fine, local files don't.</p>
  
  <p>Bonus to anyone that can help solve this other issue:</p>
  
  <ol>
  <li>I need to set the proper content type. Remote image urls don't tell me the image type, I just get a url and sometimes the url doesn't contain an extension. So I picked jpeg because it seems to work regardless of the image type sent to me.</li>
  </ol>
  
  <p>Thanks!</p>