使用omniauth-twitter从Twitter检索中型或大型配置文件图像 [英] Retrieving Medium or Large Profile Image from Twitter with omniauth-twitter

查看:86
本文介绍了使用omniauth-twitter从Twitter检索中型或大型配置文件图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用omniauth-twitter gem通过Twitter验证用户身份.我还使用他们的Twitter个人资料图像作为他们在我网站上的头像.但是,我从Twitter获得的图像分辨率很低.我知道Twitter拥有更好的分辨率图片.我怎么得到的?

I'm using omniauth-twitter gem to authenticate users through twitter. I am also using their Twitter profile image as their avatar for my site. However, the image I get from Twitter is low resolution. I know Twitter has better resolution pics available. How do I get it?

这是我当前正在做的事情.这是用户模型中的一种方法.它行得通,只是无法获得高质量的照片:

Here is what I am currently doing. It is a method in the user model. It works, just doesn't get me a good quality pic:

user.rb

  def update_picture(omniauth)
    self.picture   = omniauth['info']['image'] 
  end

我想也许我可以以某种方式将size选项传递给它,但似乎找不到一个好的解决方案.

I thought maybe I could pass a size option onto it somehow, but can not seem to find a good solution.

推荐答案

我也在使用omniauth-twitter gem.在我的用户模型的apply_omniauth方法中,我像这样保存Twitter图像路径,去掉 _normal 后缀:

I'm using the omniauth-twitter gem as well. In the apply_omniauth method of my User model, I save the Twitter image path like this, stripping the _normal suffix:

if omniauth['provider'] == 'twitter'
    self.image = omniauth['info']['image'].sub("_normal", "")
end

然后,我有一个名为portrait的辅助方法,该方法接受size参数.正如Terence Eden所建议的那样,您只需替换文件名的 _size 后缀即可访问

Then I have a helper method called portrait that accepts a size argument. As Terence Eden suggests, you can just replace the _size suffix of the filename to access the different image sizes that Twitter provides:

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

这篇关于使用omniauth-twitter从Twitter检索中型或大型配置文件图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆