Skip to content

Instantly share code, notes, and snippets.

@smith
Last active May 9, 2017 03:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smith/b4d30f860856f6bcaddb to your computer and use it in GitHub Desktop.
Save smith/b4d30f860856f6bcaddb to your computer and use it in GitHub Desktop.
Gravatar Component for Angular 2
import {Component} from "angular2/core";
const md5 = require("blueimp-md5");
@Component({
selector: "gravatar",
inputs: ["defaultStyle", "email", "size"],
template: `
<img class="my-gravatar"
width="{{size || DEFAULT_SIZE}}" height="{{size || DEFAULT_SIZE}}"
src='{{gravatarUrl(defaultStyle, email, size)}}'>`
})
export class GravatarComponent {
private DEFAULT_SIZE: number = 80;
private gravatarUrl(defaultStyle: string = "retro",
email: string, size: number = this.DEFAULT_SIZE) {
defaultStyle = encodeURIComponent(defaultStyle);
return `https://secure.gravatar.com/avatar/
${md5(email.toLowerCase().trim())}?
d=${defaultStyle}&
s=${size}`.replace(/\s/g, "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment