Post Reply 
Gravatar > User supplied image
12-May-2012, 01:38 PM
Post: #1
Gravatar > User supplied image
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.
Find all posts by this user
Quote this message in a reply
12-May-2012, 03:37 PM
Post: #2
RE: Gravatar > User supplied image
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.
Find all posts by this user
Quote this message in a reply
13-May-2012, 02:11 PM
Post: #3
RE: Gravatar > User supplied image
!!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:
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$passwordtrue) 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:
$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:
$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
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Tanya Cara menampilkan coment user dan admin di halaman customer upenx 4 729 05-Jun-2012 07:43 AM
Last Post: upenx
  get the User name and email from db Abdul 6 760 14-Apr-2012 03:48 PM
Last Post: Steven

Forum Jump:


User(s) browsing this thread: 1 Guest(s)