This is the community forum. For a developer response use the Client Area.
Follow us on Facebook, Twitter and YouTube!

Gravatar > User supplied image
#1

On my site I allow users to create a profile and upload any image they wish to serve as their profile image. I'm wondering if there is a way to insert this user supplied image in place of the gravatar when users submit comments? Thanks.
Reply
#2

Okay, I was able to search around in the code and I've got this one figured out.

In the includes/functions folder, I modified the file comments.php as follows:

In place of

$box .= "<img src='http://www.gravatar.com/avatar/" . md5(strtolower(trim($email))) . ".png?s=71" . $gravatar_parameter . "' alt='Gravatar' title='Gravatar'/>";

I inserted

if (isset($_COOKIE['profile_image'])) {
$box .= "<img src='upload/profile_images/" . $_COOKIE['profile_image'] . "' alt='Profile Image' title='Profile Image' width='50' height='50' />";
} else {
$box .= "<img src='http://www.gravatar.com/avatar/" . md5(strtolower(trim($email))) . ".png?s=71" . $gravatar_parameter . "' alt='Gravatar' title='Gravatar'/>";
}

On login to my site, I store the profile image (if there is one) in a cookie. For users that haven't uploaded a profile image, the default gravatar will show up, or the gravatar stored at gravatar.com if they happen to have a gravatar tied to the email address on file with my site.
Reply
#3

!!ERROR!! - I discovered that the code I amended yesterday will insert the currently logged-in user profile image for virtually every user comment, so I had to do a little more work to get this right.

All of the following amends the comments.php file located in the includes/functions folder.

PHP Code:
<?php 
if (!defined("IN_COMMENTICS")) { die("Access Denied."); } //This line exists in the original comments.php file, insert code amendments below

// Connect to your Database
$server= "your server"; /* Address of database server */
$user= "your login"; /* Database user name */
$password= "your pw"; /* Database Password */
$database= "your db name"; /* name of database */
$mylink = MYSQL_CONNECT($server, $user, $password, true) or die ( "<H3>Server unreachable</H3>");
MYSQL_SELECT_DB($database,$mylink) or die ( "<H3>Database non existent</H3>");
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'",$mylink);
$_SESSION['mylink'] = $mylink; //IMPORTANT - store your db link in a session variable so it's available globally

//Function to look for and return the profile image file name, if one exists
function GetProfileImg($theuser,$lnk) {
//If profile image exists, use as gravatar for comments
$sql = "SELECT MAX(id) FROM tbl_files WHERE uploader='$theuser' AND client_user='profile_images'";
$result = mysql_query($sql,$lnk);
$row=mysql_fetch_row($result);
$themaximageid = $row[0];
$sql = "SELECT * FROM tbl_files WHERE id='$themaximageid'";
$result = mysql_query($sql,$lnk);
$imgcount = mysql_num_rows($result);
if(
$imgcount == 1) {
$row=mysql_fetch_row($result);
$theprofileimage = $row[1];
}
else {
$theprofileimage = "none";
}
return
$theprofileimage;
}

//Functions to resize the dimensions of profile images to a max of 60px per side
function ComfilewidthScale($src) {
$target_width=60;
$target_height=60;
$size = getimagesize($src); // original image size
$scale = min($target_width/$size[0], $target_height/$size[1]);
$target_width = round($size[0]*$scale);
$target_height = round($size[1]*$scale);
return
$target_width;
}
function
ComfileheightScale($src) {
$target_width=60;
$target_height=60;
$size = getimagesize($src); // original image size
$scale = min($target_width/$size[0], $target_height/$size[1]);
$target_width = round($size[0]*$scale);
$target_height = round($size[1]*$scale);
return
$target_height;
}

Then, in place of this:

PHP Code:
<?php 
$box
.= "<img src='http://www.gravatar.com/avatar/" . md5(strtolower(trim($email))) . ".png?s=71" . $gravatar_parameter . "' alt='Gravatar' title='Gravatar'/>";

Insert this:

PHP Code:
<?php 
$theprofileimg
= GetProfileImg($name,$_SESSION['mylink']);
if (
$theprofileimg != "none") {
$pathtoimage = 'upload/profile_images/'.$theprofileimg;
$resizewidth = ComfilewidthScale($pathtoimage);
$resizeheight = ComfileheightScale($pathtoimage);
$box .= "<img src='" . $pathtoimage . "' alt='Profile Image' title='Profile Image' width='" . $resizewidth . "' height='" . $resizeheight . "' />";
} else {
$box .= "<img src='http://www.gravatar.com/avatar/" . md5(strtolower(trim($email))) . ".png?s=71" . $gravatar_parameter . "' alt='Gravatar' title='Gravatar'/>";
}

With this revision, I no longer have to store the users profile image in a cookie upon login. I prefer to minimize the use of cookies, so for me this is a good thing.

Thanks.
Skilife
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by msp
09-Jan-2016, 01:32 AM
Last Post by Things
10-Jun-2014, 10:48 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)