Retrieve a medium or large profile from Twitter using omniauth-twitter

I use omniauth-twitter gem to authenticate users via twitter. I also use their Twitter profile profile as my avatar for my site. However, the image I get from Twitter has a low resolution. I know that Twitter has the best resolution photos. How to get it?

This is what I am doing right now. This is the method in the user model. It works, just does not give me good quality:

user.rb

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

I was thinking, maybe I can somehow pass the size option, but I cannot find a suitable solution.

+6
source share
3 answers

I use omniauth-twitter gem. In the apply_omniauth method of my User model, I save the Twitter image path this way, removing the _normal suffix:

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

Then I have a helper method called a portrait that takes a size argument. As Terence Eden suggests, you can simply replace the _size suffix with the file name 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 
+16
source

Once you have the image url, it's pretty simple. You need to remove "_normal" from the end of the URL.

Here is a picture of my avatar

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

Here is the big version

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

A simple regular expression is enough.

Remember that the size of the image is unpredictable - so you can resize it before displaying it on your site.

+8
source

The best way to do this is through omniauth-twitter gem configuration options.

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

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

+2
source

Source: https://habr.com/ru/post/920186/


All Articles