<p>The image data is not going to be in POST variable. You should be using</p>

<pre><code>$userPhoto = UploadedFile::getInstanceByName('userPhoto');
</code></pre>

CC BY-SA 3.0
2015-03-06T19:56:59.113Z
ahh getting closer.  how do i make that an \craft\image?  getting this error Argument 2 passed to Craft\UsersService::saveUserPhoto() must be an instance of Craft\Image, instance of CUploadedFile given
Keith Mancuso
CC BY-SA 3.0
2015-03-06T20:42:50.193Z
Take a look at UsersController::_processUserPhoto(). All the cropping logic there just ensures that the image is square.
Andris Sevcenko
CC BY-SA 3.0
2015-03-06T22:13:08.857Z
hmm how would i call that from a plugin controller?  Fatal error: Call to private method Craft\UsersController::_processUserPhoto() from context 'Craft\SalesforceController'   I tried just $userPhoto = UploadedFile::getInstanceByName('photo');;
    $userPhoto = UsersController::_processUserPhoto($userPhoto);
Keith Mancuso
CC BY-SA 3.0
2015-03-06T22:15:38.510Z
ahh nevermind i see it in there.. do user profile images have to be square?
Keith Mancuso
CC BY-SA 3.0
2015-03-08T09:59:52.823Z
Well, that's how they are in the CP. If you don't mind them being stretched or squeezed in the CP, you'll be fine.
Andris Sevcenko
CC BY-SA 3.0
2015-03-06T07:39:58.097Z
2015-03-06T07:39:58.097Z
Andris Sevcenko
<p>Im trying to upload a profile image from a front end form via a controller function</p>

<p>this is what i have but the $image keeps returning null?</p>

<pre><code>&lt;form name="updateProfile" method="post" enctype="multipart/form-data"&gt;
     &lt;input type="hidden" name="action" value="salesforce/saveContact"&gt;
     &lt;input type="file" name="photo" id="avatarUpload"&gt;
     &lt;input type="submit"/&gt;
&lt;/form&gt;
</code></pre>

<p>and my controller action is</p>

<pre><code>public function actionSaveContact()
{

    $this-&gt;requirePostRequest();

    $userPhoto = craft()-&gt;request-&gt;getPost('photo');

    $user = craft()-&gt;userSession-&gt;getUser();

    craft()-&gt;users-&gt;saveUserPhoto('test.jpg',$userPhoto, $user);

}
</code></pre>

<p>Not sure what im doing wrong?</p>

<form><image>
Upload user profile image from custom plugin controller
# Upload user profile image from custom plugin controller

(Asked by: username_0 on 2015-03-06)

Im trying to upload a profile image from a front end form via a controller function
this is what i have but the $image keeps returning null?

     
     
     


and my controller action is
public function actionSaveContact()
{

    $this->requirePostRequest();

    $userPhoto = craft()->request->getPost('photo');

    $user = craft()->userSession->getUser();

    craft()->users->saveUserPhoto('test.jpg',$userPhoto, $user);

}

Not sure what im doing wrong?

**Tags:** form, image


---

## 1 answers

### Answer by username_1 on 2015-03-06. Score: 2 | Accepted answer

The image data is not going to be in POST variable. You should be using
$userPhoto = UploadedFile::getInstanceByName('userPhoto');

> **Comments:**
> **username_0**: ahh getting closer.  how do i make that an \craft\image?  getting this error Argument 2 passed to Craft\UsersService::saveUserPhoto() must be an instance of Craft\Image, instance of CUploadedFile given  
> **username_1**: Take a look at UsersController::_processUserPhoto(). All the cropping logic there just ensures that the image is square.  
> **username_0**: hmm how would i call that from a plugin controller?  Fatal error: Call to private method Craft\UsersController::_processUserPhoto() from context 'Craft\SalesforceController'   I tried just $userPhoto = UploadedFile::getInstanceByName('photo');;     $userPhoto = UsersController::_processUserPhoto($userPhoto);  
> **username_0**: ahh nevermind i see it in there.. do user profile images have to be square?  
> **username_1**: Well, that's how they are in the CP. If you don't mind them being stretched or squeezed in the CP, you'll be fine.  

---

