2013-03-06 84 views
0

我有一些代碼,當我在我的控制器中的一個動作中使用imagecreatefrompng()時破壞。我不認爲它應該有所作爲,但imagecreatefrompng()在通過AJAX調用的動作中調用。cakephp - 調試器錯誤使用imagecreatefrompng()

$imgName='img/favicons/'.$saveFileName.'_large.png'; 
$img=imagecreatefrompng($imgName); 

我很確定路徑是正確的。在同樣的動作我將文件保存到十分文件夾,但我得到返回以下錯誤:

警告:get_class()預計參數1是目標,資源在C中給出:\ XAMPP \ htdocs中\ cakephp的\ lib中\蛋糕\實用\ Debugger.php上線

警告:get_object_vars()預計參數1爲對象,資源在C中給出:\ XAMPP \ htdocs中\ cakephp中\ lib \ Cake \ Utility \ Debugger.php on line

在debugger.php文件中的特定功能的錯誤指向對象的句柄到字符串轉換,這會導致錯誤的線路有:

$className = get_class($var); 
$objectVars = get_object_vars($var); 

任何人有任何想法什麼錯?由於

編輯:

下面是功能

public function saveSiteFavicon() { 
    $this->layout = 'ajax'; 
    $url = $this->request->data['websiteurl']; 
    $saveFileName = parse_url($url); 
    $saveFileName = $saveFileName['host']; 
    $saveFileName = str_replace('.', '', $saveFileName); 
    $saveFileName = str_replace('www', '', $saveFileName); 
    $this->Favicon->recursive = 0; 
    $faviconexists = $this->Favicon->findByImage($saveFileName.'.png'); 
    if(!empty($faviconexists)) { 
     echo $saveFileName.'.png:'.$faviconexists['Favicon']['id']; 
    } else { 
     $fp = fopen ('img/favicons/'.$saveFileName.'.png', 'w+'); 
     $ch = curl_init('http://g.etfv.co/'.$url); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 6); 
     /* Save the returned data to a file */ 
     curl_setopt($ch, CURLOPT_FILE, $fp); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
     curl_exec($ch); 
     curl_close($ch); 
     fclose($fp); 
     //if the favicon is oversized resize it 
     $faviconFileName = 'img/favicons/'.$saveFileName.'.png'; 
     $metaData=getimagesize($faviconFileName); 
     if($metaData[1] > 16) { 
      rename($faviconFileName, 'img/favicons/'.$saveFileName.'_large.png'); 
      $imgName='img/favicons/'.$saveFileName.'_large.png'; 
      $thumbName='img/favicons/'.$saveFileName.'.png'; 
      $img=imagecreatefrompng($imgName); 

      $imgThumb=imagecreatetruecolor(16,16); 
      imagecopyresampled($imgThumb,$img,0,0,0,0,16,16,$metaData[0],$metaData[1]); 
      imagepng($imgThumb, $thumbName); 
      imagedestroy($imgThumb); 
     } 
     $this->Favicon->create(); 
     $this->Favicon->save(array('image'=>$saveFileName.'.png')); 
     $faviconid = $this->Favicon->getLastInsertId(); 
     echo $saveFileName.'.png:'.$faviconid; 
    } 
} 

的代碼,並在視圖中我只需要調用通過ajax此功能,並提醒其結果是我看到的錯誤消息。

$.post("../favicons/saveSiteFavicon", {websiteurl: value}) 
       .done(function(data) { 
        alert(data); 
      }); 

回答

1

errormessage基本上告訴你這個問題;

$img是'資源',而不是Object或'簡單'數據類型(字符串/整數等)。調試資源是不可能的。你可以使用var_dump(),但這可能只會給你類似resource #123。通過使用這個調試資源的'類型'可能是可能的;如果

debug(get_resource_type($img)); 

但是,看着你的代碼,你已經期待這是一個「形象」,或FALSEimagecreatefrompng()的「資源」失敗:)

更多信息; http://php.net/manual/en/language.types.resource.php

+0

嗨,謝謝你的回答。儘管如此,我仍然有點困惑。如果關閉調試,那麼我不會收到錯誤消息,但是無論哪種方式,問題仍然存在,imagecreatefrompng()似乎不起作用。路徑是正確的,只要我能解決問題,文件夾的權限也可以,因爲我可以在該文件夾中編寫和重命名文件。什麼可能導致imagecreatefrompng()不起作用?對不起,如果我看起來很愚蠢,特別是使用php和cakephp的新手。 – 2013-03-06 16:05:01

+0

調試導致錯誤,因爲它正在*試圖*來確定'$ img'的'class',由於它不是'$ object',所以失敗。你認爲其他的代碼是什麼?如果你想在一個視圖中輸出圖像*,你*不能*將它與其他輸出(HTML)結合起來 – thaJeztah 2013-03-06 16:20:14

+0

我已經在原始問題中發佈了一些代碼。感謝你幫助我,這一直在我的腦海裏好幾天。我只是不明白爲什麼imagecreatefrompng()失敗! – 2013-03-06 16:44:44