图像重新调整大小不适用于cakephp

时间:2013-03-05 14:08:35

标签: php cakephp image-processing gd

我正在尝试编写一个上传一个favicon的函数,如果所述的favicon大于16x16px,那么创建一个16x16px的副本。我正在使用cakephp 2,当用户在我的网站上输入表单中的网址时,通过ajax调用该函数。

到目前为止,该函数的工作原理是它获得了favicon,如果它大于16x16px,那么它会根据需要重命名该文件,但是应该创建新的较小版本的代码失败而且我不能找出原因。我看过很多其他人的例子和书籍,但我不知道我的问题是什么,这对我来说都是正确的。

这是当我发出警告从函数回显时返回的警告:

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

警告:get_object_vars()期望参数1为对象,资源在 C:\ xampp \ htdocs \ xxxxxx \ lib \ Cake \ Utility \ Debugger.php 行< b> 584

警告:在 585 C:\ xampp \ htdocs \ xxxxxx \ lib \ Cake \ Utility \ Debugger.php 中为foreach()提供的参数无效 b>

警告:ReflectionObject :: __ construct()要求参数1为对象,资源在 C:\ xampp \ htdocs \ xxxxxx \ lib \ Cake \ Utility \ Debugger.php 中给出在线 591

致命错误:内部错误:无法在 C:\ xampp \ htdocs \ xxxxxx \ lib \ Cake \ Utility \ Debugger.php 中的行检索反射对象593

这是功能:

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';
            switch($metaData['mime']){
                case 'image/jpeg':
                $img=imagecreatefromjpeg($imgName);
                break;
                case 'image/png':
                $img=imagecreatefrompng($imgName);
                break;
                case 'image/gif':
                $img=imagecreatefromgif($imgName);
                break;
                case 'image/wbmp':
                $img=imagecreatefromwbmp($imgName);
                break;
            }
            $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;
    }
}

任何人都可以告诉我我做错了什么。

编辑:此问题正在发生:

$imgThumb=imagecreatetruecolor(16,16);
imagecopyresampled($imgThumb,$img,0,0,0,0,16,16,$metaData[0],$metaData[1]);
imagepng($imgThumb, $thumbName);
imagedestroy($imgThumb);

如果我注释掉imagecopyresampled行,我不会收到任何错误。因此,imagecopyresampled的某些内容导致调试器在将对象处理为字符串转换的函数中失败。

这是错误消息中提到的Debugger中的函数

protected static function _object($var, $depth, $indent) {
    $out = '';
    $props = array();

    $className = get_class($var);
    $out .= 'object(' . $className . ') {';

    if ($depth > 0) {
        $end = "\n" . str_repeat("\t", $indent - 1);
        $break = "\n" . str_repeat("\t", $indent);
        $objectVars = get_object_vars($var);
        foreach ($objectVars as $key => $value) {
            $value = self::_export($value, $depth - 1, $indent);
            $props[] = "$key => " . $value;
        }

        if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
            $ref = new ReflectionObject($var);

第578行是

$className = get_class($var);

显然,功能还有更多,但所有错误都发生在这些行中。

0 个答案:

没有答案