我使用的是 Magento 1.11.2.0 版本,我想为客户添加在“我的帐户”页面上传图片的选项。
我在管理员中添加了图像文件类型的新客户属性,效果很好。但它只有图像的最大图像宽度、最大图像高度选项。我想添加另外两个输入,以便在他们上传头像时指定宽度和高度以调整图像大小。
有办法吗?我也很好奇什么模块/类用于客户的上传图片属性。
最佳答案
这个过程有几个步骤。首先,您需要创建一个属性并将其添加到默认组和属性集中。下面是一些可以添加到设置脚本中的代码:
$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer->startSetup();
$vCustomerEntityType = $installer->getEntityTypeId('customer');
$vCustAttributeSetId = $installer->getDefaultAttributeSetId($vCustomerEntityType);
$vCustAttributeGroupId = $installer->getDefaultAttributeGroupId($vCustomerEntityType, $vCustAttributeSetId);
$installer->addAttribute('customer', 'avatar', array(
'label' => 'Avatar Image',
'input' => 'file',
'type' => 'varchar',
'forms' => array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'),
'required' => 0,
'user_defined' => 1,
));
$installer->addAttributeToGroup($vCustomerEntityType, $vCustAttributeSetId, $vCustAttributeGroupId, 'avatar', 0);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'avatar');
$oAttribute->setData('used_in_forms', array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'));
$oAttribute->save();
$installer->endSetup();
关键是设置 input
至 file
.这导致系统在后端显示一个文件 uploader ,并在处理表单时查找上传的文件。 type
是varchar
, 因为 varchar 属性用于存储文件名。
创建属性后,您需要将输入元素添加到 persistent/customer/form/register.phtml
模板。执行此操作的一些示例代码如下:
<label for="avatar"><?php echo $this->__('Avatar') ?></label>
<div class="input-box">
<input type="file" name="avatar" title="<?php echo $this->__('Avatar') ?>" id="avatar" class="input-text" />
</div>
这里还需要注意的主要事情是字段的 ID 和名称应该与您的属性代码相同。另外,不要忘记添加 enctype="multipart/form-data"
到 <form>
标签。
这将允许用户在注册时上传头像图像。随后,当显示图像时,您需要将其调整为适合您站点的尺寸。 Magento 图像助手代码旨在处理产品图像,但 this blog post将向您展示如何创建可以调整图像大小并缓存调整大小的文件的辅助函数。我之前曾使用这些说明来调整类别图像的大小,它们运行良好。
关于image - 在 Magento 中上传客户图片并调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9800766/