codeigniter图像大小调整和ajax / json的问题。请帮忙!

时间:2011-07-02 12:34:37

标签: ajax json image codeigniter resize

我有一个让我发疯的问题。我正在构建一个CMS系统,它涉及使用jquery ajax插件(uploadify)上传图像,并使用codeigntier的图像调整大小类根据用户指定的尺寸调整服务器端的大小。

问题在于偶尔(大约七次中的一次)图像大小调整在其中一个图像上失败(一次最多发送5个图像),导致ajax post功能超时。从CI的错误日志中可以看出其中一个文件已损坏。不太确定如何或为什么会发生这种情况,但无论如何 - 我正在尝试建立一个错误系统,如果发生这种情况,将通过json发回错误,这将通过javascript警告,只是要求他们再试一次或其他什么。但这是问题 -

在我的模特中,我有:

foreach($size_image as $si){
                if(!$this->resize_image($si['dimensions'],$si['fileName'],$si['oldPath'],$si['newPath'])){
                    return false;           // I can confirm this does indeed return false whenever the image resize problem occurs...                                                                          
                }
            }

在我的控制器中我有:

    function save_page(){
    if($this->admin_model->save_page()){
        $return_array['error'] = '';
    }else{
        $return_array['error'] = 'Sorry, there was a problem processing one of the images.';
    }
    $return_array['success'] = true;
    echo json_encode($return_array);
    exit();
}

每当调整大小工作(并且save_page函数返回true)时,echo json编码工作正常,并在另一端接收并由javascript处理。但不是当它返回false !!

为了证明我并不愚蠢,我可以用 if(!$ this->)替换 if($ this-> admin_model-> save_page()){。 admin_model-> save_page()){(意味着我现在在调整 DOES 工作时收到错误消息) - 是的,我在屏幕上收到错误消息!

因此,每当出现调整大小问题时,由于某种原因,上面的函数只是拒绝回显json,即使它肯定会到达脚本的这一部分。

为了绝对清晰,我可以这样做:

function save_page(){
    if($this->admin_model->save_page()){
        $return_array['error'] = '';
    }else{

    //  $return_array['error'] = 'Sorry, there was a problem processing one of the images.';        

        $this->load->helper('file');
        write_file('testOutput.txt','ERROR!!!!');       // <-- and yep, I get 'ERROR' written to this file. 
        exit();         
    }
    $return_array['success'] = true;
    echo json_encode($return_array);                    // <-- ...so why the hell doesn't this work then?
    exit();
}

为什么哦为什么哦为什么?

非常欢迎任何帮助或建议,非常感谢。

1 个答案:

答案 0 :(得分:0)

也许是因为你回来了

  

$ return_array ['success'] = true;

每一次,即使失败了?