diff --git a/admin/settings/users.php b/admin/settings/users.php
index d765c97..2b73125 100644
--- a/admin/settings/users.php
+++ b/admin/settings/users.php
@@ -129,6 +129,8 @@ if ($hassiteconfig
         $temp->add(new admin_setting_configmulticheckbox('extrauserselectorfields',
                 get_string('extrauserselectorfields', 'admin'), get_string('configextrauserselectorfields', 'admin'), array('email' => '1'),
                 array('email' => get_string('email'), 'idnumber' => get_string('idnumber'), 'username' => get_string('username'), )));
+
+        $temp->add(new admin_setting_configcheckbox('enablegravatar', get_string('enablegravatar', 'admin'), get_string('configenablegravatar', 'admin'), 0));
     }
 
     $ADMIN->add('roles', $temp);
diff --git a/lang/en/admin.php b/lang/en/admin.php
index c3c674b..db7937a 100644
--- a/lang/en/admin.php
+++ b/lang/en/admin.php
@@ -202,6 +202,7 @@ $string['configenablecalendarexport'] = 'Enable exporting or subscribing to cale
 $string['configenablecomments'] = 'Enable comments';
 $string['configenablecourserequests'] = 'This will allow any user to request a course be created.';
 $string['configenableglobalsearch'] = 'This setting enables global text searching in resources and activities, it is not compatible with PHP 4.';
+$string['configenablegravatar'] = 'If a user has not uploaded a profile picture, Moodle will display a Gravatar picture for that user based on the email address. If no Gravatar picture is found, the default profile picture is displayed. See <a target="_blank" href="http://www.gravatar.com/">http://www.gravatar.com/</a> for more information.';
 $string['configenablegroupmembersonly'] = 'If enabled, access to activities can be restricted to group members only. This may result in an increased server load. In addition, gradebook categories must be set up in a certain way to ensure that activities are hidden from non-group members.';
 $string['configenablehtmlpurifier'] = 'Use HTML Purifier instead of KSES for cleaning of untrusted text. HTML Purifier is actively developed and is believed to be more secure, but it is more resource intensive. Expect minor visual differences in the resulting html code. Please note that embed and object tags can not be enabled, MathML tags and old lang tags are not supported.';
 $string['configenablemobilewebservice'] = 'Enable mobile service for the official Moodle app or other app requesting it. For more information, read the {$a}';
@@ -487,6 +488,7 @@ $string['enablecourseajax_desc'] = 'Allow AJAX when editing main course pages. N
 $string['enablecourserequests'] = 'Enable course requests';
 $string['enabledevicedetection'] = 'Enable device detection';
 $string['enableglobalsearch'] = 'Enable global search';
+$string['enablegravatar'] = 'Enable Gravatar for profile pictures';
 $string['enablegroupmembersonly'] = 'Enable group members only';
 $string['enablehtmlpurifier'] = 'Enable HTML Purifier';
 $string['enablemobilewebservice'] = 'Enable mobile web service';
diff --git a/lang/en/moodle.php b/lang/en/moodle.php
index ecd7141..43de656 100644
--- a/lang/en/moodle.php
+++ b/lang/en/moodle.php
@@ -750,6 +750,7 @@ See the Moodle License information page for full details:
 http://docs.moodle.org/dev/License';
 $string['grade'] = 'Grade';
 $string['grades'] = 'Grades';
+$string['gravatarbeingused'] = 'Your <a target="_blank" href="http://www.gravatar.com/">Gravatar</a> picture is currently being used (if available). You can override this by uploading a different picture below.';
 $string['group'] = 'Group';
 $string['groupadd'] = 'Add new group';
 $string['groupaddusers'] = 'Add selected to group';
diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php
index f822ef9..1011655 100644
--- a/lib/outputcomponents.php
+++ b/lib/outputcomponents.php
@@ -318,15 +318,10 @@ class user_picture implements renderable {
             }
             return moodle_url::make_pluginfile_url($context->id, 'user', 'icon', NULL, $path, $filename);
 
-        } else if ($this->user->picture == 2) {
-            // This is just VERY basic support for gravatar to give the actual
-            // implementor a headstart in what to do.
-            if ($size < 1 || $size > 500) {
-                $size = 35;
-            }
-            $md5 = md5(strtolower(trim($this->user->email)));
-            $default = urlencode($this->pix_url('u/'.$filename)->out(false));
-            return "http://www.gravatar.com/avatar/{$md5}?s={$size}&d={$default}";
+        } else if (!empty($CFG->enablegravatar)) {
+            // Display Gravatar picture (w/ fallback to default user picture)
+            $src = $this->pix_url('u/'.$filename)->out(false);
+            return 'http://www.gravatar.com/avatar.php?gravatar_id='.md5(strtolower($this->user->email)).'&default='.urlencode($src).'&size='.$size;
         }
 
         return $renderer->pix_url('u/'.$filename);
diff --git a/user/edit_form.php b/user/edit_form.php
index d227848..646ce5d 100644
--- a/user/edit_form.php
+++ b/user/edit_form.php
@@ -76,6 +76,9 @@ class user_edit_form extends moodleform {
                 $image_el =& $mform->getElement('currentpicture');
                 if ($user and $user->picture) {
                     $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID, 'size'=>64)));
+                } else if (!empty($CFG->enablegravatar)) {
+                    // provide an explanation when Gravatar images are being used by default
+                    $image_el->setValue(get_string('gravatarbeingused'));
                 } else {
                     $image_el->setValue(get_string('none'));
                 }
diff --git a/user/editadvanced_form.php b/user/editadvanced_form.php
index 10f6b45..0abcb72 100644
--- a/user/editadvanced_form.php
+++ b/user/editadvanced_form.php
@@ -104,6 +104,9 @@ class user_editadvanced_form extends moodleform {
             $image_el =& $mform->getElement('currentpicture');
             if ($user and $user->picture) {
                 $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID)));
+            } else if (!empty($CFG->enablegravatar)) {
+                // provide an explanation when Gravatar images are being used by default
+                $image_el->setValue(get_string('gravatarbeingused'));
             } else {
                 $image_el->setValue(get_string('none'));
             }
