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


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 = '[email protected]' //ここにメールアドレス

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