Search code examples
phpcakephpimagecreatefrompng

cakephp - debugger error using imagecreatefrompng()


I have some code which breaks when I use imagecreatefrompng() in an action in my controller. I don't think it should make a difference but the imagecreatefrompng() is called inside an action which has been called via AJAX.

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

I'm pretty sure the path is right. In the same action I save a file to that very folder, however I get returned the following errors:

Warning: get_class() expects parameter 1 to be object, resource given in C:\xampp\htdocs\cakephp\lib\Cake\Utility\Debugger.php on line 578

Warning: get_object_vars() expects parameter 1 to be object, resource given in C:\xampp\htdocs\cakephp\lib\Cake\Utility\Debugger.php on line 584

The particular function in the debugger.php file that the errors point to handles object to string conversion, and the lines which cause the error are:

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

Anyone have any ideas what's going wrong? Thanks

EDIT :

Here's the code for the function

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;
    }
}

and in the view I just call this function via ajax and alert the results which is where I see the error messages.

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

Solution

  • The errormessage basically tells you the problem;

    $img is a 'resource', not an Object or a 'simple' datatype (string/integer etc). Debugging resources is not possible. You can use var_dump() but this will probably only give you something like resource #123. Debugging the 'type' of resource may be possible by using this;

    debug(get_resource_type($img));
    

    But, looking at your code, you're already expecting that to be an 'image', or FALSE if imagecreatefrompng() failed :)

    More information on 'resources'; http://php.net/manual/en/language.types.resource.php