jquery - codeigniter image resize not changing without page refresh -
i'm trying crop image via ajax. after ajax call, checked target file directory, , image file cropped expected. when try show file using $('#upload_cover').css('background-image', 'url('+res.img_src+')');
, file shown before cropped. if refresh page , put same file path, shows cropped image.
why that? how can show image after cropped without refreshing page?
this code:
the ajax call:
var formdata = $(this).closest('form').serializearray(); console.log('formdata', formdata); $.ajax({ url: '/uploader/resize_img', type: 'post', data: formdata, datatype: 'json', success: function(res) { console.log('success resize img', res); $('#upload_cover').css('background-image', "url("+res.img_src+")"); }, error: function(err) { console.log('error resize img', err.responsetext); } });
console.log('formdata', formdata):
0 : {name: "csrftokengotravelly", value: "somerandomhash"} 1 : {name: "x_axis", value: "29"} 2 : {name: "y_axis", value: "50.5"} 3 : {name: "width", value: "154"} 4 : {name: "height", value: "149"} 5 : {name: "rotate", value: "0"} 6 : {name: "scalex", value: "1"} 7 : {name: "scaley", value: "1"} 8 : {name: "img_src", value: "/assets/img/user/ca6f717ab7d023224229ff0d12f2344d.jpg"}
php function resize image:
public function resize_img() { if ( ! $this->input->is_ajax_request()) { show_404(); } $result = array( 'status' => 0, 'message' => '', ); $this->load->library('image_lib'); try { $config['image_library'] = 'gd2'; $x_axis = $this->input->post('x_axis'); $y_axis = $this->input->post('y_axis'); $width = $this->input->post('width'); $height = $this->input->post('height'); $rotate = $this->input->post('rotate'); $scalex = $this->input->post('scalex'); $scaley = $this->input->post('scaley'); $img_src = $this->input->post('img_src'); if (empty($img_src) || !file_exists('.'.$img_src)) { throw new exception('source image file not found.'); } $config['source_image'] = '.'.$img_src; if ($x_axis) { $config['x_axis'] = (int)$x_axis; } if ($y_axis) { $config['y_axis'] = (int)$y_axis; } if ($width) { $config['width'] = (int)$width; } if ($height) { $config['height'] = (int)$height; } $this->image_lib->clear(); $this->image_lib->initialize($config); if ( ! empty($config['rotate'])) { if ( ! $this->image_lib->rotate()) { throw new exception("failed processing image file. ".$this->image_lib->display_errors()); } $result['message'] .= "image rotated successfully. "; } if ($x_axis && $y_axis && $width && $height) { if ( ! $this->image_lib->crop()) { throw new exception("failed processing image file. ".$this->image_lib->display_errors()); } $result['message'] .= "image cropped successfully. "; } $result['status'] = 1; $result['img_src'] = $img_src; $pos = strrpos($config['source_image'], '/'); $result['filename'] = substr($config['source_image'], $pos+1); // abc123.jpg } catch (exception $e) { $result['message'] = $e->getmessage(); } echo json_encode($result); } // end public function resize_img($params)
Comments
Post a Comment