从facebook中获取用户个人资料大图片,并在设计2.0中使用omniauth

我想从Facebook获取用户个人资料大图或普通图片。 现在我使用以下代码从用户个人资料图片中获取方形版本:

:image => access_token.info.image # http://graph.facebook.com/id/picture?type=square 

我怎样才能获得大版或普通版?

或者和应用程序,我如何在这个url中替换最后一个字,大而不是方形……

非常感谢你!

以下是Facebook允许的4种不同尺寸的个人资料图片。

 http://graph.facebook.com/id/picture?type=small http://graph.facebook.com/id/picture?type=square http://graph.facebook.com/id/picture?type=large http://graph.facebook.com/id/picture?type=normal 

如果您想在登录时获取不同大小的图片,omniauth-facebook策略可以选择更改请求的图片。

image_size:在auth哈希中设置返回的图像URL的大小。

例如,omniauth.rb将以这种方式请求大图片:

 provider :facebook, 'secrets', 'sekrets', :image_size => 'large' 

https://github.com/mkdynamic/omniauth-facebook#configuring

当你将它保存到数据库中时你可以这样做access_token.info.image.split("=")[0] << "=large"

并且只需将大小更改为您想要的任何大小。

或者您可以使用辅助方法在视图中显示不同的大小。

 def profile_photo(type="large") puts @user.image.split("=")[0] << "=#{type}" end profile_photo("small") #=> http://url?type=small profile_photo("square") #=> http://url?type=square profile_photo #=> http://url?type=large profile_photo("normal") #=> http://url?type=normal 

我没有更改配置文件,只是在url中加了+ "?type=large"

<%= image_tag current_user.image + "?type=large" %>

所以你可以随时要求不同尺寸的它。

<%= image_tag current_user.image + "?type=normal" %>

<%= image_tag current_user.image + "?type=small" %>