AVrAmTAR API

Hello,

We have implemented basic AVrAmTAR API, so you can start using AVrAmTAR in your (web) applications from now on very easy. The base of our API is AVrAmTAR URL, which is built from several parts.

  1. The URL
  2. Usage in PHP
  3. Usage in Perl
  4. Usage in ASP
  5. Usage in ASP.NET/vb
  6. Usage in Ruby on Rails

The URL

The URL is built from three parts: URL base, email parameter and default image parameter.

http://avatars.avramtar.com/avatar.php
?email=EMAIL_MD5
&default=DEFAULT_IMAGE_URL

  • URL base is URL to avatar.php on our website. It is script that will serve avatars to visitors of your web application.
  • Email parameter contains md5 hash of user’s email addres. This is required and most important parameter, and with this parameter our script knows which user’s avatars it should serve. So when user registers on your website and leaves his email address, you can instantly display his avatar(s).
  • Default image parameter contains URLencoded (full) URL to default avatar, which will be shown ONLY if no avatars are found for provided (hashed) email. This parameter is optional, and if not supplied default no-avatar image will be shown:

Usage in PHP

To use our API in PHP, you don’t need any external plugins as PHP already contains functions for MD5 hashing and URLencoding.

Example:

  1. <?php
  2. //AVrAmTAR function, you should edit default value of $default_avatar
  3. function get_avramtar($user_email, $default_avatar = )
  4. {
  5. return ‘http://avatars.avramtar.com/avatar.php?email=’ . md5($user_email) . ‘&amp;default=’ . urlencode($default_avatar);
  6. }
  7.  
  8. //usage example
  9. echo "<img src=’" . get_avramtar(‘user@domain.com’) . "’ />";
  10. ?>

Note: We suggest that you re-type above code as WordPress may mess up quotes and apostrophes in code, or just download AVrAmTAR API for PHP

Usage in Perl

Using AVrAmTAR API in Perl is easy, as Perl also supports MD5 hashing and URL encoding. Nenad Vasić provided sample code for Perl:

  1. #!/usr/bin/perl
  2. use URI::Escape qw(uri_escape);
  3. use Digest::MD5 qw(md5_hex);
  4.  
  5. #AVrAmTAR function
  6. sub get_avramtar {
  7.         $user_email = $_[0];
  8.         $default_avatar = $_[1];
  9.         $result = "http://avatars.avramtar.com/avatar.php?email=" . md5_hex($user_email) . "&amp;default=" . uri_escape($default_avatar);
  10.         return $result;
  11. }
  12.  
  13. #usage example, first parameter is user’s email address
  14. #and second parameter is default avatar url (empty string)
  15. #for default no-avatar image
  16. print "<img src=\"" . get_avramtar("user@domain.com","") . "\" />";

Note: We suggest that you re-type above code as WordPress may mess up quotes and apostrophes in code, or just download AVrAmTAR API for Perl

Usage in ASP

ASP does not have MD5 support by default, so we’re using code for MD5 digest from freevbcode.com. I wish to thank ES community and it’s supermoderator Shadowed for help on this. Example:

  1. <HTML>
  2. <!–#include file="md5.asp"–>
  3. <%
  4. ‘AVrAmTAR function
  5. Public function get_avramtar(user_email, default_avatar)
  6.     result = "http://avatars.avramtar.com/avatar.php?email=" & md5(user_email)
  7.     if default_avatar <> "" then result = result & "&amp;default=" & Server.URLEncode(default_avatar)
  8.     return result
  9. end function
  10.  
  11. ‘usage example, first parameter is user’s email address
  12. ‘and second parameter is default avatar url (empty string)
  13. ‘for default no-avatar image
  14. response.write "<img src=’" & get_avramtar("user@domain.com","") & "’ />"
  15. %>
  16. </HTML>

Note: We suggest that you re-type above code as WordPress may mess up quotes and apostrophes in code, or just download AVrAmTAR API for ASP

Usage in ASP.NET/vb

Shadowed also provided code for ASP.NET/VisualBasic:

  1. <%@ Page Language="VB" AutoEventWireup="false" %>
  2. <%@ Import Namespace="System.Security.Cryptography" %>
  3. <script runat="server">
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         imgAvramtar.ImageUrl = get_avramtar("user@domain.com", "")
  6.     End Sub
  7.     Private Function get_avramtar(ByVal user_email As String, ByVal default_avatar As String) As String
  8.         Dim MD5Hasher As MD5 = MD5.Create()
  9.         Dim data() As Byte = MD5Hasher.ComputeHash(Encoding.Default.GetBytes(user_email))
  10.         Dim MyBuilder As New System.Text.StringBuilder()
  11.         Dim i As Integer
  12.         Dim result As String
  13.         For i = 0 To data.Length1
  14.             MyBuilder.Append(data(i).ToString("x2"))
  15.         Next
  16.         result = "http://avatars.avramtar.com/avatar.php?email=" & MyBuilder.ToString()
  17.         If default_avatar <> "" Then result &= "&amp;default=" & System.Web.HttpUtility.UrlEncode(default_avatar)
  18.         Return result
  19.     End Function
  20. </script>
  21. <html>
  22.     <body>
  23.         <asp:Image ID="imgAvramtar" runat="server" />
  24.     </body>
  25. </html>

Note: We suggest that you re-type above code as WordPress may mess up quotes and apostrophes in code, or just download AVrAmTAR API for ASP.NET/vb

Usage in Ruby on Rails

Dejan Simić provided plugin for Rails, and it can be installed from svn:

ruby script/plugin install svn://rors.org/avramtar

Example of usage:

  1. <%= avramtar_img ‘user@domain.com’ %>

To change default avatar (one that is shown when no original avatar can be found), override default_avatar function in ApplicationHelper like this:

  1. module ApplicationHelper
  2.   def default_avatar
  3.     ‘http://www.domain.com/default-avatar.png’
  4.   end
  5. end

Usage in…

More languages soon… maybe ;-)

Leave a Reply