tha*_*ent 6 twitter twitter-oauth omniauth ruby-on-rails-3
我正在使用omniauth-twitter gem通过twitter验证用户身份.我也使用他们的Twitter个人资料图片作为我网站的头像.但是,我从Twitter获得的图像是低分辨率.我知道Twitter有更好的分辨率图片.我怎么得到它?
这是我目前正在做的事情.它是用户模型中的一种方法.它的工作原理,只是没有给我一个高品质的图片:
user.rb
def update_picture(omniauth)
self.picture = omniauth['info']['image']
end
Run Code Online (Sandbox Code Playgroud)
我想也许我可以以某种方式将尺寸选项传递给它,但似乎无法找到一个好的解决方案.
joh*_*ers 16
我也在使用omniauth-twitter gem.在我的User模型的apply_omniauth方法中,我保存了这样的Twitter图像路径,剥离了_normal后缀:
if omniauth['provider'] == 'twitter'
self.image = omniauth['info']['image'].sub("_normal", "")
end
Run Code Online (Sandbox Code Playgroud)
然后我有一个名为portrait的辅助方法,它接受一个size参数.正如Terence Eden建议的那样,您只需替换文件名的_size后缀即可访问Twitter提供的不同图像大小:
def portrait(size)
# Twitter
# mini (24x24)
# normal (48x48)
# bigger (73x73)
# original (variable width x variable height)
if self.image.include? "twimg"
# determine filetype
case
when self.image.downcase.include?(".jpeg")
filetype = ".jpeg"
when self.image.downcase.include?(".jpg")
filetype = ".jpg"
when self.image.downcase.include?(".gif")
filetype = ".gif"
when self.image.downcase.include?(".png")
filetype = ".png"
else
raise "Unable to read filetype of Twitter image for User ##{self.id}"
end
# return requested size
if size == "original"
return self.image
else
return self.image.gsub(filetype, "_#{size}#{filetype}")
end
end
end
Run Code Online (Sandbox Code Playgroud)
获得图像的URL后,它非常简单.您需要从URL的末尾删除"_normal".
这是我的头像图片
https://si0.twimg.com/profile_images/2318692719/7182974111_ec8e1fb46f_s_normal.jpg
Run Code Online (Sandbox Code Playgroud)
这是更大的版本
https://si0.twimg.com/profile_images/2318692719/7182974111_ec8e1fb46f_s.jpg
Run Code Online (Sandbox Code Playgroud)
一个简单的正则表达式就足够了.
请记住,图像的大小是不可预测的 - 因此您可能希望在将其显示在您的网站上之前调整其大小.
| 归档时间: |
|
| 查看次数: |
6368 次 |
| 最近记录: |