php - 使用 codeigniter 上传 Canvas 图像

标签 php jquery codeigniter

我尝试使用 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();
}

请帮我修复或找到其他上传 Canvas 图像的方法。

最佳答案

看看这对你有用吗?

<canvas id="cnvs"></canvas>
<button id="upld"></button>
$(document).on('click', '#upld', function () {
    var form_data = new FormData();
    var file;
    file = $('#cnvs')[0].toDataURL('image/png');
    file = file.replace(/^data:image\/(png|jpg|jpeg);base64,/, '');
    form_data.append('photo', file);
    $.ajax({
        url: 'mycontroller/myfunc',
        type: 'POST',
        dataType: 'JSON',
        data: form_data,
        processData: false,
        contentType: false,
        async: false,
        cache: false,
        beforeSend: function () {
             ...
        },
        success: function (data) {
             ...      
        },
        error: function (xhr, ajaxOptions, thrownError) {
             ...
        }
    });
});

PHP

$imgData = base64_decode($this->input->post('photo', TRUE));

/* calculate the approximate file size */
$fileSize = (int) (strlen(trim($imgData, '=')));

if ($fileSize > /* any value */) {
    /* show error message for big files */
}

$path = 'assets/uploads/file.png'; /* my path and file name with extension */
if (file_exists($path)) {
    unlink($path);
}
/* create image */
file_put_contents($path, $imgData);

关于php - 使用 codeigniter 上传 Canvas 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45690488/

相关文章:

php PDO MySQL 根据另一个表的结果数组选择一个表的结果

php - Bootstrap - 卡无法正常工作

php - 我想按类别在两个不同的列中显示记录

php - 在 codeIgniter 中插入多个 SQL 语句

PHP 和 SQL(尝试使用提交按钮更新我的数据库)

php - 为什么我得到的 .csv 文件的 mime 类型为 "application/octet-stream"?

javascript - 向上或向下滚动时显示/隐藏页脚

javascript - 从外部 JS 文件访问 JS 数组数据

javascript - 以编程方式选择 jQuery 按钮集中的 radio 使其崩溃

php - mysql查询以检查codeigniter中数组中是否存在字段