LoginSignup
1
0

More than 3 years have passed since last update.

Twitter APIで取得したアイコン画像の大きさを変える【Ruby on Rails】

Posted at

はじめに

TwitterAPIの取得にomniauthを使っています。
またomniauthの基本的な使い方については省略しています。
omniauthの基本的な使い方については別記事を参照してください。

開発環境

Ruby 2.6.1
Ruby on Rails 6.0.1

Twitterアイコン画像を取得する

app/controllers/twitter_controller.rb
class TwitterController < ApplicationController
  # 基本的なomniauthの使い方は省略
  def twitter
    auth_hash = request.env["omniauth.auth"]
    @image_url = auth_hash[:info][:image]
  end
end

このままでは48x48にリサイズされたアイコン画像のURLを取得してしまいます。
小さい!当然CSSなどで拡大してもボヤボヤになってしまいます。

大きなTwitterアイコン画像の取得方法

omniauthの設定に使ったファイルを書き換えます。

config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter,
  Rails.application.credentials.twitter[:key],
  Rails.application.credentials.twitter[:secret_key],
  # 以下を追加する
  {
    :image_size => "original"
  }
end

こうすることによって、本来のサイズでのアイコン画像のURLを取得できます。

参考

omniauth-twitter(GitHub):https://github.com/arunagw/omniauth-twitter

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0