Add user picture profile programmatically

Add user picture profile programmatically

by lital l -
Number of replies: 4

Hi,


I'm trying to add picture to user profile.

I have url to the picture thats need to be upload to the user.

I need to do it programmatically ( from the upload users csv ).

How can I do it?


Thanks,

Lital

Average of ratings: -
In reply to lital l

Re: Add user picture profile programmatically

by Jez H -

We do that in our authentication plugin (as we user our own SSO plugin).

Its perhaps not the best place for it but it works and means the first time a user logs in their profile picture is there.

What we do is CURL in the image from a URL and store it in a temporary directory which is required to then use Moodles function to crop the image.

Once we have the image in place we simply call:

process_new_icon($context, 'user', 'icon', 0, $filepath);

That function will crop the image and put it in the users profile for you.

In reply to lital l

Re: Add user picture profile programmatically

by praveen chamarthi -

yes you can add user pictures through programmatically.

  • you need to add one filed in mdl_user table.ex:url_picture(give default value is null)
  • next you have any csv file are text file there need to be one matching filed csv file(moodle username or email )Ex:testusername(this is moodle username)|https://www.google.co.in//images/srpr/logo11w.png(This is image url)
  • next you need to write one query for inserting all image urls based on user name.
  • now we have all image urls inserted respected usernames in moodle database.
  • next open lib/outputrenderers.php line number 2317 function name  render_user_picture
        here you need to replace code 

     old Code :$src = $userpicture->get_url($this->page, $this);


     New CODE : 

 $user_picture_url = "select * from {user} where id = ".$userpic;

$module_course = $DB->get_records_sql($user_picture_url);

foreach($module_course AS $rec){

$url_picture=$rec->url_picture;

}

if($url_picture != NULL && $user->picture == 0)

{

if (getimagesize($url_picture) !== false) {

//echo 'display image';

$src = $url_picture;

}else{

$src = $userpicture->get_url($this->page, $this);

}

}


  • here what happened is it's showing user picture with respected images what ever we inserted.
  • here condition is if user is uploaded profile picture through moodle profile page it will show moodle image priority.
Thanks ,

Praveen 

Average of ratings: Useful (1)
In reply to lital l

Re: Add user picture profile programmatically

by Igor Sazonov -

Hi!

You need to use php function copy to move your image from the external source to your (tempotary) folder, and then you need to use Moodle function process_new_icon

You can read about upload user profiles images at the article here: http://lms-service.org/blog/news/lenauth-updated-version-1-1-0/

Average of ratings: Useful (1)