我正在尝试在我的网站上显示 Facebook 个人资料图片,但不想泄露源中人员的 Facebook ID。
例如,这个网址:http://graph.facebook.com/4/picture http://graph.facebook.com/4/picture将重定向到:http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg当您将其加载到浏览器中时。我想获取第二个网址(CDN 网址)并将其用作我的 img src,因为它在网址中不显示 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 并返回该 URL,而不是尝试获取实际的原始二进制数据并将其编码为 base64?