OM The Eternity
OM The Eternity

Reputation: 16204

Path to profile picture in Moodle?

I was programming something in moodle web-application and was looking into retrieving the path of the user profile images.

I assumed I could find the path somewhere in the database but I only got to mdl_user.picture and mdl_user.imagealt, so practically I know who has uploaded a picture but can't get to which picture he/she uploaded.

Is there a way to get it from the database?

Thanks for your help,

OM

Upvotes: 6

Views: 11334

Answers (5)

pgee70
pgee70

Reputation: 3984

The OP requested getting this information from the database, other posters gave hints on how to get it from moodle using PHP - but if you don't have access to moodle code, this is the way to do it..

you need to first find the instance ID for the user's id (3) in example.

Then you need to search the files table for a hash of the instance ID /user/icon/f/f[1-3].[jpg|png]

this query will give all files, in size order, where the largest file is first.

the first column file_path will be the file path relative to the moodle files on your server. see config.php for $CFG->dataroot for its location.

select @instanceID := id instance_id
from mdl_context where contextlevel = 30 and instanceid = 3; # instanceid = 3 is user id 3
select @instanceID; # check it is present.
;
SELECT concat(left(f.contenthash,2),"/",substring(f.contenthash,3,2),"/",f.contenthash) file_path, f.pathnamehash, timecreated, filename, f.*
FROM mdl_files f
LEFT JOIN mdl_files_reference r ON f.referencefileid = r.id
WHERE f.pathnamehash in(
    sha1(concat("/",@instanceID,"/user/icon/0/f1.jpg")),
    sha1(concat("/",@instanceID,"/user/icon/0/f2.jpg")),
    sha1(concat("/",@instanceID,"/user/icon/0/f3.jpg")), 
    sha1(concat("/",@instanceID,"/user/icon/0/f1.png")),
    sha1(concat("/",@instanceID,"/user/icon/0/f2.png")),
    sha1(concat("/",@instanceID,"/user/icon/0/f3.png")))
order by timecreated desc, filename desc

Upvotes: 1

Muhammad Rauuf
Muhammad Rauuf

Reputation: 108

this one works for me in 3.4 version

<?php echo new moodle_url('/user/pix.php/'.$USER->id.'/f1.jpg')?>

Upvotes: 3

k_kumar
k_kumar

Reputation: 191

To get the User profile pic simply you can use the following path:

NOTE: used userid = 3 and appended with number 2

Path : http://moodleservername/moodle/pluginfile.php/23/user/icon/clean/f1

Upvotes: -1

aladein
aladein

Reputation: 125

in moodle 2.0 you can use this

global $USER,$PAGE; 
$user_picture=new user_picture($USER);
$src=$user_picture->get_url($PAGE);

Upvotes: 5

matthewdaniel
matthewdaniel

Reputation: 1476

If you want the image tag you can use print_user_picture() and pass the user object that you got from the database. You can also specify the size of the image. So to print the full size user picture for the current user you could do

global $USER, $COURSE;

print_user_picture($USER, $COURSE->id, null, true);

Otherwise if you need just the url you have do do something like this

require_once($CFG->libdir.'/filelib.php');

$size = array('large' => 'f1', 'small' => 'f2');

$src = false;
if ($user->picture) {
   $src = get_file_url($user->id.'/'.$size['large'].'.jpg', null, 'user');
}

Upvotes: 6

Related Questions