2013-10-20 31 views
0

我在我的應用程序這個組件:CakePHP的組件錯誤

class ImageComponent extends Component { 


var $image; 
var $image_type; 

function __construct($filename = null){ 
    if(!empty($filename)){ 
     $this->load($filename); 
    } 
} 

function load($filename) { 
    $image_info = getimagesize($filename); //<<== LINE OF THE ERROR 
    $this->image_type = $image_info[2]; 
    if($this->image_type == IMAGETYPE_JPEG) { 
     $this->image = imagecreatefromjpeg($filename); 
    } elseif($this->image_type == IMAGETYPE_GIF) { 
     $this->image = imagecreatefromgif($filename); 
    } elseif($this->image_type == IMAGETYPE_PNG) { 
     $this->image = imagecreatefrompng($filename); 
    } else { 
     throw new Exception("The file you're trying to open is not supported"); 
    } 

} 
} 

但是,當我剛剛加載組件,我收到此錯誤:

getimagesize() expects parameter 1 to be string, object given [APP/Controller/Component/ImageComponent.php, line 33] 

感謝

+3

['Component :: __ construct(ComponentCollection $ collection,$ settings = array())'](http://book.cakephp .org/2.0/en/controllers/components.html#Component :: __ construct) – ndm

回答

0

的構造一個組件期望第一個參數是一個ComponentCollection對象。在一個側面說明 - 這是一個代碼味道改變父母的類功能的參數類型

+0

我可以只刪除構造函數嗎? –

+0

不這樣做。當某些東西不適合你的實現時,你應該重新考慮你的設計模式 –