Get moodle user profile image in PHP

Get moodle user profile image in PHP

by Gavin Neale -
Number of replies: 1

Oops, I put this in the wrong forum so I hope it is okay to post her again. 


Hi Everyone,

I have a problem with trying to retrieve user profile pics for inclusion in a mpdf60 PDF file. I have used solutions from other forum posts here to get the URL code and this works. I can can enter the direct URL in a browser or into a PHP echo statement and the picture appears perfectly eg:

http://XXXXXXXX/moodle/pluginfile.php/1267/user/icon/f1

and 

http://XXXXXXX/user/pix.php/USERID/f1.jpg


But when I enter the same code into my PHP mpdf60 page, moodle retrieves the grey avatar picture instead of the correct image. (You know the grey image when a user does not have a picture uploaded). Any idea why the same URL returns the image to HTML but an avatar for mpdf60? I am not assuming that anyone will be able to help me with mpdf but maybe you know why the moodle returns two different values based on the exact same URL? Is there some sort of context or security that I need to take into account?


As an example of what I mean for those without mpdf.

If I have this URL for a known userid and I paste it into a web browser I get the correct image but if I copy the file to a tmp.png I get the grey avatar. Any ideas about what I am doing wrong?

EG, the code below gives me a grey avatar but in a browser the link gives the correct image

$url = 'http://XXXXXXXXX/moodle/user/pix.php/241/f1.jpg';

$img = 'tmp.png';

file_put_contents($img, file_get_contents($url));

The above code also works when I can replace the URL with a file. It copies the file okay and if I put a broken link I get a corrupted tmp.png. 


Thanks,

Gavin


Moodle 3.1.1+ (Build: 20160714)


Average of ratings: -
In reply to Gavin Neale

Re: Get moodle user profile image in PHP

by Gavin Neale -

I can fix the issue by setting Security->Site Policies->Force users to log in to view user pictures to "NO". I would rather not do that though. Any ideas?