0

好的,所以我尝试在 jquery 上传预览之前进行最大高度最大宽度验证,以便在图像大于最大高度或最大宽度时提醒用户并要求他们选择较小的照片。我怎么能做那部分代码?

html代码:

<img id="uploadPreview" style="display:none;"/>

<!-- image uploading form -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input id="uploadImage" type="file" accept="image/jpeg" name="image" />
<input type="submit" value="Upload">

<!-- hidden inputs -->
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
</form>

jQuery代码:

function setInfo(i, e) {
    $('#x').val(e.x1);
    $('#y').val(e.y1);
    $('#w').val(e.width);
    $('#h').val(e.height);
}

$(document).ready(function() {
    var p = $("#uploadPreview");

    // prepare instant preview
    $("#uploadImage").change(function(){
        // fadeOut or hide preview
        p.fadeOut();

        // prepare HTML5 FileReader
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);

        oFReader.onload = function (oFREvent) {
            p.attr('src', oFREvent.target.result).fadeIn();
        };
    });

    // implement imgAreaSelect plug in (http://odyniec.net/projects/imgareaselect/)
    $('img#uploadPreview').imgAreaSelect({
        // set crop ratio (optional)
        aspectRatio: '1:1',
        onSelectEnd: setInfo
    });
});
4

1 回答 1

2

只需将您的 script.js 替换为

function setInfo(i, e) {
    $('#x').val(e.x1);
    $('#y').val(e.y1);
    $('#w').val(e.width);
    $('#h').val(e.height);
}
$(document).ready(function () {
    var p = $("#uploadPreview");
    // prepare instant preview
    $("#uploadImage").change(function () {
        var that=this;
        // fadeOut or hide preview
        p.fadeOut();
        // prepare HTML5 FileReader
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);
        oFReader.onload = function (oFREvent) {
            var image = new Image();
            image.src = oFREvent.target.result;
            image.onload = function () {
                if ((this.width > 400) && (this.height)) {
                    that.value="";
                    alert("choose another file");
                }
                else {
                    p.attr('src', oFREvent.target.result).fadeIn();
                }
                // access image size here & do further implementation
            };
        };
    });
    // implement imgAreaSelect plug in (http://odyniec.net/projects/imgareaselect/)
    $('img#uploadPreview').imgAreaSelect({
        // set crop ratio (optional)
        aspectRatio: '1:1',
        onSelectEnd: setInfo,
        maxHeight: 100,
        maxWidth: 100
    });
});

我会帮你的..

于 2013-10-04T09:01:06.897 回答