drupal 7 set user picture programmatically

ok so you want people to easily change their profile picture , to other images well thats cool below you will find working sample code for drupal 7 to  set user picture programmatically:
what I did was create a view of feilds with  image (with a Contextual filter of user id optional )  just create a view how ever you want but makesure in views "Format" has  "Show: Fields".
now add an image feild and use what ever setting you want the in rewright results creat a link like this:

<a href='http://somesite.com/somepath?fid=[field_image_1-fid]'>[field_image]</a>
replace tokens as nessasary
now on the on the destination page ether in template node overide ie "node--123.tpl.php" or in scarry phpfilter or better custom module page.

in template or wherever  load the view programiticly if you want .. print views_embed_view('testselectimage','block',$uid); (optional).

put this:

// check if fid 
	$filedid = trim(strip_tags($_GET['fid']));
	// call function from user_pic module 
$uerrpic = user_pic_addnew($filedid);

Then create a new  module I called mine  user_pic:
then add this :

function user_pic_addnew($fileid){
// get the user 	
global $user; 
	$uid = $user->uid;
$account = user_load($uid); // views re wright replacement token [field_image_1-fid] // load the file $thefile = file_load($fileid); // now file is loaded all we need to do is apply it // basic validation / security check if userpicture is owned by the user if($thefile->uid == $uid){ $edit['picture'] = $thefile; user_save($account, $edit); $status = 'status'; return array("Profile Picture Updated",$status); }else{ // no dice $status = 'error'; return array("You dont have access to this file", $status); } }

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
1 + 1 =

Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.