2013-10-31 27 views
1

我想在resize方法上使用foreach循環創建幾個不同大小的大拇指。

$sizes = array(
    'thumb' => Configure::read('Shop.image_thumb_dimensions'), 
    'medium' => Configure::read('Shop.image_medium_dimensions'), 
    'large' => Configure::read('Shop.image_large_dimensions') 
); 


foreach($sizes as $folder => $size) { 

    $destFolder = WWW_ROOT. $this->upload_dir . DS . $folder; 

    if (!file_exists($destFolder)) { 
     @mkdir($destFolder); 
    } 

    $dimensionsArray = explode(',', $size); 

    $newWidth = $dimensionsArray[0]; 
    $newHeight = $dimensionsArray[1]; 

    $destFile = $destFolder . DS . $fileName; 

    $resize = $this->__resize($filePath, $destFile, $newWidth, $newHeight); 

} 

,然後它利用一些方法從組件的大小調整功能是這樣的:

private function __resize($src, $destFile, $newWidth, $newHeight) { 

    $this->Watimage->setImage($src); 
    $this->Watimage->resize(array('type' => 'resizecrop', 'size' => array($newWidth, $newHeight))); 
    if (!$this->Watimage->generate($destFile)) { 
     // handle errors... 
     return $this->Watimage->errors; 
    } 
    else { 
     return true; 
    } 

} 

所以這第一個圖像尺寸(拇指)的偉大工程,但此後我得到的錯誤:

b>Notice</b> (8)</a>: Indirect modification of overloaded property WatimageComponent::$file has no effect [<b>APP/Plugin/Gallery/Controller/Component/WatimageComponent.php</b>, line <b>114</b> 

我不明白我在做什麼錯?花了數小時試圖弄清楚這一點。有關此事的任何照明將不勝感激。

這是從組件類中的方法:

public function setImage($file) { 
    // Remove possible errors... 
    $this->errors = array(); 
    try 
    { 
     if (is_array($file) && isset($file['file'])) 
     { 
      if (isset($file['quality'])) 
       $this->setQuality($file['quality']); 
      $file = $file['file']; 
     } 
     elseif (empty($file) || (is_array($file) && !isset($file['file']))) 
     { 
      throw new Exception('Empty file'); 
     } 

     if (file_exists($file)) 
      $this->file['image'] = $file; 
     else 
      throw new Exception('File "' . $file . '" does not exist'); 

     // Obtain extension 
     $this->extension['image'] = $this->getFileExtension($this->file['image']); 
     // Obtain file sizes 
     $this->getSizes(); 
     // Create image boundary 
     $this->image = $this->createImage($this->file['image']); 
     $this->handleTransparentImage(); 
    } 
    catch (Exception $e) 
    { 
     $this->error($e); 
     return false; 
    } 
    return true; 
} 
+1

好,你發佈一些代碼,但實際的代碼,其中錯誤被觸發失蹤。另外請始終提及您的確切CakePHP版本!這就是說,搜索「_Indirect修改重載的屬性_」,你會發現通常會導致此錯誤的大量信息(例如http://stackoverflow.com/q/10454779/1392379)。 – ndm

+0

重點在於它首次運行,但在循環的上下文中,$文件似乎未被設置,並且重載屬性的間接修改只發生在循環的第二次迭代中。我相信我正在運行cakephp 2.4.2的最新版本,但是這個錯誤並不是蛋糕特有的。我已經閱讀了超載財產,但沒有接近擺動的問題。 – Manu

+0

它可能不是Cake特有的,但不管它是什麼,沒有看到觸發錯誤的代碼,沒有人能夠給你一個具體的解決方案。所以,請顯示組件代碼。 – ndm

回答

2

有你摹O,最初的問題是最有可能的WaitmageComponent::$file財產

unset($this->file); 

https://github.com/elboletaire/Watimage/blob/b72e7ac17ad30bfc47ae4d0f31c4ad6795c8f8d2/watimage.php#L706

的解封這樣做之後,神奇的屬性存取Component::__get()將嘗試訪問現在所沒有的WaitmageComponent::$file屬性時踢,因此這會導致您收到警告。

而是取消設置變量,應該重新初始化:

$this->file = array(); 

當然,還應該適當地初始化:

private $file = array(); 
+0

你太棒了。非常感謝您花時間幫助我解決這個問題。我注意到$ src變量未被設置,但正在嘗試一切,除了尋找明顯的!謝謝。現在完美運作。 – Manu

0

你應該初始化您的類的屬性,我認爲正在發生的事情是,你正在嘗試做的是這樣的:

$this->file = $var; 

但你需要告訴你的類什麼是$文件屬性爲:

class WaitmageComponent extends Component { 
    public $file = array(); 
} 
相關問題