【发布时间】:2012-03-22 19:35:35
【问题描述】:
我正在尝试在我的网站上显示 Facebook 个人资料图片,但不想泄露源中人员的 Facebook id。
例如,当您在浏览器中加载此 URL:http://graph.facebook.com/4/picture 时,它将重定向到:http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg。我想获取第二个 url(CDN url)并将其用作我的 img src,因为它不会在 url 中显示 facebook id。
我目前正在 Ruby on Rails 中执行此操作,我很好奇是否有比我在下面所做的更好的方法:
def picture_square(facebook_id, secure=false)
raw_url = "http://graph.facebook.com/" facebook_id + "/picture?type=square"
if secure
binary_img = ''
open(raw_url) do |f|
binary_img = f.read
end
encoded_img = Base64.encode64(binary_img)
return 'data:image/jpg;base64,' + encoded_img.to_s
else
return raw_url
end
end
您可以使用以下 HTML 调用它(使用上面的示例):
<img src="<%= picture_square(4, true) %>"
这绝对有效,并且使用内联图像属性来实际渲染图像,但如果您有一堆图像要加载,它会有点慢。
在 Ruby 中有没有一种方法可以让我获取重定向的 URL 并返回它,而不是尝试获取实际的原始二进制数据并将其编码为 base64?
【问题讨论】:
标签: ruby-on-rails http facebook-graph-api