Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在图像上自动显示裁剪选择_Jquery - Fatal编程技术网

Jquery 如何在图像上自动显示裁剪选择

Jquery 如何在图像上自动显示裁剪选择,jquery,Jquery,我想知道,一旦选择了照片文件,如何使imgareaselect自动选择?这样用户就知道他们可以在照片中裁剪出要使用的内容,到目前为止,我有: 我试图实现的是,当我选择一个图像文件时,我试图让imgareaselect自动选择照片的一部分 imgareaselect文档: jQuery代码: // set info for cropping image using hidden fields function setInfo(i, e) { // Get on screen image var s

我想知道,一旦选择了照片文件,如何使imgareaselect自动选择?这样用户就知道他们可以在照片中裁剪出要使用的内容,到目前为止,我有:

我试图实现的是,当我选择一个图像文件时,我试图让imgareaselect自动选择照片的一部分

imgareaselect文档:

jQuery代码:

// set info for cropping image using hidden fields
function setInfo(i, e) {
// Get on screen image
var screenImage = $("#uploadPreview");

// Create new offscreen image to test
var theImage = new Image();
theImage.src = screenImage.attr("src");

// Get accurate measurements from that.
var imageWidth = theImage.width;

//Get width of resized image
var scaledimagewidth = document.getElementById("uploadPreview").width;

if ( imageWidth > scaledimagewidth){ var ar = imageWidth/scaledimagewidth;}
else { var ar = 1;}
    $('#x').val(e.x1*ar);
    $('#y').val(e.y1*ar);
    $('#w').val(e.width*ar);
    $('#h').val(e.height*ar);
}
$(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) {
            var image = new Image();
            image.src = oFREvent.target.result;
            image.onload = function () {
                if ((this.width >= 4500) || (this.height >= 4500)) {
                    alert("Picture Has to Be Lower Than 4500 by 4500, 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({
        aspectRatio: '1.33:1',
        onSelectEnd: setInfo
    });
});
HTML标记:

    <div class="wrap">
        <!-- image preview area-->
        <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>
    </div>

这个答案似乎通过()起作用

//添加图像区域选择实例
功能添加区域选择(img){
img.addClass('imgAreaSelect').imgAreaSelect({
是的,
aspectRatio:'16:9',
时尚种子:1,
秀:真的
});
load(函数(){//显示初始图像选择16:9
变量高度=(此宽度/16)*9;
如果(高度)
// adds an image area select instance
function addImgAreaSelect( img ){
        img.addClass( 'imgAreaSelect' ).imgAreaSelect({
                handles : true,
                aspectRatio : '16:9',
                fadeSpeed : 1,
                show : true
        });
        img.load(function(){ // display initial image selection 16:9
                    var height = ( this.width / 16 ) * 9;
                    if( height <= this.height ){     
                            var diff = ( this.height - height ) / 2;
                            var coords = { x1 : 0, y1 : diff, x2 : this.width, y2 : height + diff };
                    }   
                    else{ // if new height out of bounds, scale width instead
                            var width = ( this.height / 9 ) * 16; 
                            var diff = ( this.width - width ) / 2;
                            var coords = { x1 : diff, y1 : 0, x2 : width + diff, y2: this.height };
                    }   
                $( this ).imgAreaSelect( coords );
        });
}