Problem with codeigniter image resizing and ajax / json. Please help!
I have a problem that is driving me crazy. I am building a CMS system which involves uploading images with a jquery ajax plugin (uploadify) and having them resized server-side according to user-specified dimensions using codeigntier's image resize class.
The problem is that occasionally (about one in 7 times) the image resizing fails on one of the images (up to 5 images sent at a time) which was causing the ajax post function to timeout. From CI's error logs it says one of the files is corrupt. Not quite sure how or why that happens, but anyway- I am trying to set up an error system whereby if this happens, an error is sent back through json which will be alerted with javascript, just asking them to please try again or whatever. But here is the problem-
In my model I have:
foreach($size_image as $si){
if(!$this->resize_image($si['dimensions'],$si['fileName'],$si['oldPath'],$si['newPath'])){
return false; // I can confirm this does开发者_如何学Python indeed return false whenever the image resize problem occurs...
}
}
In my controller I have:
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();
}
Whenever the resizing works (and save_page function returns true) the echo json encode works fine and is being received at the other end and handled by the javascript. But NOT when it returns false!!
Just to prove I am not being silly, I can replace if($this->admin_model->save_page()){ with if(!$this->admin_model->save_page()){ (meaning that I now get the error message when the resizing DOES work) - and yes, I get the error message on the screen!
So whenever this resizing problem occurs, for some reason the above function just refuses to echo the json, even though it is definitely getting to this part of the script.
And for absolute clarity, I can do this:
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();
}
Why oh why oh why?
Any help or suggestions very much welcome, many thanks in advance.
Maybe because you return
$return_array['success'] = true;
every time, even if it fails?
精彩评论