我尝试使用 Codeigniter 上传 canvas 图像,这是我的 Ajax 代码:
$.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 () { },
});
这里是保存帖子数据的代码:
$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();
}
请帮我修复或寻找其他方法上传画布图像。
file_put_contents($filepath, base64_decode($img));
来上传图片呢?我在类似的任务中使用过这个方法,它很有效。 - Jignesh Bhavani