【问题标题】:cakephp - debugger error using imagecreatefrompng()cakephp - 使用 imagecreatefrompng() 的调试器错误
【发布时间】:2013-03-06 14:17:44
【问题描述】:

当我在控制器的操作中使用 imagecreatefrompng() 时,我有一些代码会中断。我不认为它应该有所作为,但是 imagecreatefrompng() 是在通过 AJAX 调用的操作中调用的。

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

我很确定这条路是正确的。在同一操作中,我将文件保存到该文件夹​​中,但是返回以下错误:

警告:get_class() 期望参数 1 为对象,资源在 C:\xampp\htdocs\cakephp\lib\Cake\Utility\Debugger.php 中给出在 578

警告:get_object_vars() 期望参数 1 是对象,资源在 C:\xampp\htdocs\cakephp\lib\Cake\Utility\Debugger.php 中给出在线 584

错误指向的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);
            });

【问题讨论】:

    标签: php cakephp imagecreatefrompng


    【解决方案1】:

    errormessage基本上就是告诉你问题所在;

    $img 是“资源”,而不是 Object 或“简单”数据类型(字符串/整数等)。无法调试资源。你可以使用var_dump(),但这可能只会给你类似resource #123的东西。使用这个可以调试资源的“类型”;

    debug(get_resource_type($img));
    

    但是,查看您的代码,您已经期望这是一个“图像”,或者如果 imagecreatefrompng() 失败,则为 FALSE :)

    关于“资源”的更多信息; http://php.net/manual/en/language.types.resource.php

    【讨论】:

    • 您好,感谢您的回答。不过我还是有点困惑。如果我关闭调试,则不会收到错误消息,但无论哪种方式,问题仍然是 imagecreatefrompng() 似乎不起作用。据我所知,路径是正确的,并且文件夹的权限也可以,因为我能够在该文件夹中写入和重命名文件。什么可能导致 imagecreatefrompng() 不起作用?对不起,如果我看起来很愚蠢,特别是 php 和 cakephp 的新手。
    • Debug 导致错误,因为它尝试确定$img 的“类”,但由于它不是$object 而失败。您认为还有哪些其他代码?如果您尝试在视图内部输出图像,则无法将其与其他输出 (HTML)
    • 我在原始问题中发布了一些代码。谢谢你帮我解决这个问题,这几天一直在困扰我。我只是不明白为什么 imagecreatefrompng() 失败!
    • 很抱歉一直打扰你,但我很绝望。任何想法我的问题可能是什么?有什么明显的吗?
    • 代码太多,都在一个函数中,而且有太多地方可能出错;下载可能会失败,保存/移动下载可能会失败。而且,您无法检查下载的文件是否实际上是 PNG 图像,您似乎“假设”它是 PNG。我建议将此功能拆分为单独的功能,以便您可以分别测试各个部分(例如,尝试下载一个网站图标并检查它是否有效,调整您自己在某个地方的图像大小并知道它是一个PNG)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2019-03-09
    • 2012-05-12
    • 2012-02-19
    • 1970-01-01
    相关资源
    最近更新 更多