2012-03-21 130 views
2

我使用的是Magento 1.11.2.0版本,我想添加選項讓客戶在我的帳戶頁面上上傳圖片。客戶圖片在Magento上傳並調整大小

我已經在管理員中添加了一個新的圖像文件類型的客戶屬性,並且這工作得很好。但它只有圖像的最大圖像寬度,最大圖像高度選項。我想添加兩個其他輸入,以便我們可以指定在上傳頭像時調整圖像大小的寬度和高度。

有沒有辦法做到這一點?我也很好奇客戶使用什麼模塊/類來上傳圖片屬性。

+1

你是企業客戶,所以你可以要求Magento團隊的支持。 – 2012-03-21 13:05:10

回答

7

這個過程有幾個步驟。首先,您需要創建一個屬性並將其添加到默認組和屬性集。下面是一些代碼,可以添加到安裝腳本這樣做:

$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(); 

最關鍵的事情有設置inputfile。這會導致系統在後端顯示文件上傳器,並在處理表單時查找上傳的文件。 typevarchar,因爲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將向您展示如何創建可調整圖像大小的助手函數,並緩存調整大小的文件。我之前使用過這些說明來調整類別圖片的尺寸,並且它們運作良好。