使用codeigniter上传Blueimp jQuery-File-Upload并重命名和调整图像大小

时间:2018-05-29 10:15:55

标签: php codeigniter blueimp multifile-uploader

我一直在寻找和尝试很多天,我正在使用

Blueimp jQuery-File-Upload

对于使用codeigniter进行多文件上传,我对使用它的某些方面有疑问:

  1. 如何使用15_1.jpg / 15_2.jpg / 15_3.jpg
  2. 等序列重命名上传的文件
  3. 如何使用相同的表单提交两次首次提交 上传,第二次提交,以便向数据库插入一些输入。
  4. 如何在上传前调整图片大小

1 个答案:

答案 0 :(得分:1)

1)重命名 而不是

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $_FILES["file"]["name"]);

这样做,

$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);

将根据当前时间生成一个随机数,并附加原始上传文件的扩展名。

2)使用相同的表格上传和插入,你需要两个按钮

$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        add: function (e, data) {
            data.context = $('<button/>').text('Upload')
                .appendTo(document.body)
                .click(function () {
                    data.context = $('<p/>').text('Uploading...').replaceAll($(this));
                    data.submit();
                });
        },
        done: function (e, data) {
            data.context.text('Upload finished.');
        }
    });
});

类似于插入创建另一个函数并调用它..

3)上传前调整图片大小?..他们给你 要进行客户端图像大小调整,您需要包含以下脚本:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<!-- The jQuery UI widget factory, can be omitted if jQuery UI is already included -->
<script src="js/vendor/jquery.ui.widget.js"></script>
<!-- The Load Image plugin is included for the preview images and image resizing functionality -->
<script src="https://blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js"></script>
<!-- The Canvas to Blob plugin is included for image resizing functionality -->
<script src="https://blueimp.github.io/JavaScript-Canvas-to-Blob/js/canvas-to-blob.min.js"></script>
<!-- The Iframe Transport is required for browsers without support for XHR file uploads -->
<script src="js/jquery.iframe-transport.js"></script>
<!-- The basic File Upload plugin -->
<script src="js/jquery.fileupload.js"></script>
<!-- The File Upload processing plugin -->
<script src="js/jquery.fileupload-process.js"></script>
<!-- The File Upload image preview & resize plugin -->
<script src="js/jquery.fileupload-image.js"></script>

然后,您需要做的就是将disableImageResize选项设置为false。 默认情况下,图像会调整为FullHD(1920x1080),但您可以定义自己的选项:

$('#fileupload').fileupload({
    url: '//jquery-file-upload.appspot.com/',
    dataType: 'json',
    // Enable image resizing, except for Android and Opera,
    // which actually support image resizing, but fail to
    // send Blob objects via XHR requests:
    disableImageResize: /Android(?!.*Chrome)|Opera/
        .test(window.navigator && navigator.userAgent),
    imageMaxWidth: 800,
    imageMaxHeight: 800,
    imageCrop: true // Force cropped images
})