Upload Customer Profile Image in Magento 2: A Comprehensive Guide
In Magento 1, uploading customer profile images was quite straightforward. However, Magento 2 introduced significant changes that require a different approach. In this post, we’ll guide you through the process of creating a customer image attribute and enabling both customers and admin to upload, update, and delete profile images.
Step 1: Creating a Customer Image Attribute
To create an image attribute for customers in Magento 2:
-
Login to your Magento 2 backend.
-
Navigate to Stores > Attributes > Customer.
-
Click on the “Add New Attribute” button.
-
Fill out the required fields:
- Attribute Code: customer_image (or any unique name)
- Backend Type: Image
- Default Value: Leave it blank

-
Click “Save Attribute.”
Step 2: Updating the Customer Form
Next, we need to update the customer form to include our new image attribute:
- Open the file
app/code/Magento/Customer/view/base/forms/customer/form.phtml. - Add the following code inside the
<form>tags:
<div class="field">
<label for="customer_image" data-bind="attr: {required: isRequired('customer_image'), id: 'customer_image'}"><?php echo $block->escapeHtml(__('Profile Picture')); ?></label>
<div>
<input type="file" id="customer_image" name="customer_image" data-form-part="customer_form" class="admin__field-control input-text">
<small class="admin__legend-help">Recommended file types: .jpg, .jpeg, .gif, .png; Max. size: 2048 KB.</small>
</div>
</div>
This code adds an image upload field to the customer form.
Step 3: Saving and Displaying Customer Images
To save and display the uploaded images:
- Open the
app/code/Magento/Customer/Model/Data/AbstractData.phpfile. - Add the following method inside the class:
public function beforeSave()
{
if (isset($this->_data['customer_image']) && !empty($this->_data['customer_image']['value'])) {
$imageName = time() . '_' . $this->getId() . '.' . pathinfo($this->_data['customer_image']['name'], PATHINFO_EXTENSION);
$imagePath = 'customer/' . $imageName;
try {
$uploader = new Uploader('customer_image');
$uploader->save($this->_mediaDirectory->getAbsolutePath($imagePath));
$this->setCustomerImage($imageName);
} catch (\Exception $e) {
$this->addErrorMessage(__('Failed to save the image'));
}
}
}
This code handles the image upload and saves it in the customer directory.
- Add the following line inside your customer view file (usually under
app/design/frontend/{vendor}/{theme}/Magento_Customer/templates/form/register.phtml):
<div class="field">
<label for="customer_image" data-bind="attr: {required: isRequired('customer_image'), id: 'customer_image'}"><?php echo $block->escapeHtml(__('Profile Picture')); ?></label>
<?php if ($block->getCustomer()->getData('customer_image')): ?>
<div class="profile-image">
<img src="<?php echo $this->getMediaUrl($block->getCustomer()->getData('customer_image')); ?>" alt="">
</div>
<?php endif; ?>
</div>
This code displays the uploaded image in the customer view.
Conclusion
Uploading a customer profile image in Magento 2 requires a combination of creating an attribute, updating the form, and handling the file upload. By following this guide, you should now be able to provide customers and admin with the ability to upload, update, and delete profile images seamlessly within your Magento store.
If you found this article helpful, consider exploring more advanced topics on Laravel Company’s blog. Happy coding!