Ruby

require 'digest'
require 'uri'

# Assume you manually set the email_address here or get it from user input
email_address = 'someone@example.com'.downcase

# Create the SHA256 hash
hash = Digest::SHA256.hexdigest(email_address)

# Set default URL and size parameters
default = "https://www.example.com/default.jpg"
size = 40

# Compile the full URL with URI encoding for the parameters
params = URI.encode_www_form('d' => default, 's' => size)
image_src = "https://www.gravatar.com/avatar/#{hash}?#{params}"

# This 'image_src' can now be used in an <img> tag or wherever needed
puts image_src  # Example to output the result



Provide a link to gravatar.com/profile and let your users know how to edit their Gravatar profile. Use of our free APIs is governed by these Guidelines for Responsible Use. Documentation is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


Last updated on:

Blog at WordPress.com.