LoginSignup
1
1

More than 5 years have passed since last update.

Node.jsでGravatarの画像を取得するメモ

Posted at

Gravatarの画像をNode.jsから取得します。 事前に利用するメールアドレスでgravatarに登録してある前提です。

  • step1: emailハッシュを作成
  • step2: URLを作成

step1: email ハッシュ

md5でemailハッシュを作成します。外部依存モジュール使わないで出来ますね。

参考: https://ja.gravatar.com/site/implement/hash/

'use strict'

const crypto = require('crypto')

//md5ハッシュを作る関数を作成
const md5hex = str => crypto.createHash('md5').update(str, 'binary').digest('hex')

const mail = 'hogehoge@hoge.com' //ここにメールアドレス

console.log(`email hash: ` + md5hex(mail));
//email hash: ca0740a1455580936ff1bdc634fb8e2f

step2: URLを作成

https://www.gravatar.com/avatar/ + ハッシュ

という形式でアクセスするとアイコンが取得できます。

例: https://www.gravatar.com/avatar/dae1d4cc4599a65a5da6e1a1f9d96f05

おまけ ?s=xxxでサイズ変更

  • s=10

https://www.gravatar.com/avatar/dae1d4cc4599a65a5da6e1a1f9d96f05?s=10

  • s=50


https://www.gravatar.com/avatar/dae1d4cc4599a65a5da6e1a1f9d96f05?s=50

  • s=100


https://www.gravatar.com/avatar/dae1d4cc4599a65a5da6e1a1f9d96f05?s=100

  • s=200


https://www.gravatar.com/avatar/dae1d4cc4599a65a5da6e1a1f9d96f05?s=200

1
1
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
1