【问题标题】:Retrieving Medium or Large Profile Image from Twitter with omniauth-twitter使用 omniauth-twitter 从 Twitter 中检索中型或大型配置文件图像
【发布时间】:2012-07-10 16:38:18
【问题描述】:

我正在使用 omniauth-twitter gem 通过 twitter 对用户进行身份验证。我还使用他们的 Twitter 个人资料图片作为我网站的头像。但是,我从 Twitter 获得的图像分辨率很低。我知道 Twitter 有更好的分辨率图片可用。如何获得?

这是我目前正在做的事情。它是用户模型中的一种方法。它有效,只是没有给我一张高质量的照片:

用户.rb

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

我想也许我可以通过某种方式将大小选项传递给它,但似乎找不到好的解决方案。

【问题讨论】:

    标签: ruby-on-rails-3 twitter omniauth twitter-oauth


    【解决方案1】:

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

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

    然后我有一个名为 Portrait 的辅助方法,它接受一个大小参数。正如 Terence Eden 所建议的,您只需替换文件名的 _size 后缀即可访问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
    

    【讨论】:

    • 这个答案给出了更详细和有用的答案,以及使用 sub 方法删除“_normal”的解决方案。谢谢!
    【解决方案2】:

    获得图片的 URL 后,就很简单了。您需要从 URL 的末尾删除“_normal”。

    这是我的头像

    https://si0.twimg.com/profile_images/2318692719/7182974111_ec8e1fb46f_s_normal.jpg
    

    这是大图

    https://si0.twimg.com/profile_images/2318692719/7182974111_ec8e1fb46f_s.jpg
    

    一个简单的正则表达式就足够了。

    请记住,图像的大小是不可预测的 - 因此您可能希望在将其显示在您的网站上之前调整它的大小。

    【讨论】:

    • 你在“一个简单的正则表达式”中迷失了我 :) 我会试试看。关于如何最好地将正则表达式添加到我拥有的方法中的任何建议?谢谢。
    • 实际上,我决定在使用 .sub 方法将图像加载到视图时修改 url,而不是保存修改后的 url,如下所示: user.picture.sub("normal", "reasonably_small")
    【解决方案3】:

    更好的方法是通过omniauth-twitter gem 的配置选项。

    provider :twitter, "API_KEY", "API_SECRET", :image_size => 'original'

    https://github.com/arunagw/omniauth-twitter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-11
      • 2020-02-13
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2011-06-04
      相关资源
      最近更新 更多