php - Upload canvas image using codeigniter -
i try upload canvas image using codeigniter, have ajax code
$.ajax({ url: '/gifs/savepreview', type: 'post', beforesend: function (xhr) { xhr.setrequestheader('content-type','application/x-www-form-urlencoded'); }, data: 'imgdata=' + canvas.todataurl('image/gif'), success: function () { }, error: function () { }, });
and here code saving post data
$imgdata = $this->security->xss_clean($this->input->post('imgdata')); $temp_file_path = tempnam(sys_get_temp_dir(), 'tempimage'); $img = str_replace('data:image/png;base64,', '', $imgdata); $img = str_replace('[removed]', '', $img); $img = str_replace(' ', '+', $img); file_put_contents($temp_file_path, base64_decode($img)); $image_info = getimagesize($temp_file_path); $_files['userfile'] = array( 'name' => uniqid().'.'. str_replace("image/", '', $image_info['mime']), 'type' => $image_info['mime'], 'tmp_name' => $temp_file_path, 'error' => upload_err_ok, 'size' => filesize($temp_file_path), ); $config['upload_path'] = 'assets/uploads/'; $config['remove_spaces'] = true; $config['allowed_types'] = "*"; $config['max_size'] = "2048000"; $config['overwrite'] = true; $config['file_name'] = "canvas-image"; $this->load->library('upload', $config); $this->upload->initialize($config); if ($this->upload->do_upload('userfile')){ return "success"; }else{; echo $this->upload->display_errors(); }
please me fix or find other way upload canvas image.
Comments
Post a Comment