0

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?

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

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?

1 Answer 1

2

The image data is not going to be in POST variable. You should be using

$userPhoto = UploadedFile::getInstanceByName('userPhoto');
5
  • 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 Commented Mar 6, 2015 at 19:56
  • Take a look at UsersController::_processUserPhoto(). All the cropping logic there just ensures that the image is square. Commented Mar 6, 2015 at 20:42
  • 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); Commented Mar 6, 2015 at 22:13
  • ahh nevermind i see it in there.. do user profile images have to be square? Commented Mar 6, 2015 at 22:15
  • 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. Commented Mar 8, 2015 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.