Jcrop图像区域需要使用jcrop默认选择

时间:2014-11-05 10:01:13

标签: javascript jquery jcrop

我正在尝试使用jcrop进行图像上传和裁剪。但是一旦上传图像,它就会显示我需要进行裁剪的预览预览。默认情况下,上传图片需要启用裁剪区域图像本身。如何使用jcrop实现这一点?

function showCoords(c) {
    $('#crop_x').val(c.x);
    $('#crop_y').val(c.y);
    $('#crop_x2').val(c.x2);
    $('#crop_y2').val(c.y2);
    $('#crop_w').val(c.w);
    $('#crop_h').val(c.h);
};

var jcrop_api;  
$().ready(function() {
    FormImageCrop.init();
    initJcrop();
    function initJcrop()
    {
        jcrop_api = $.Jcrop('#preview');
    };

    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) { 
                $('#preview').attr('src', e.target.result);

                jcrop_api.setImage(e.target.result); 
                $('#preview').Jcrop({
                    aspectRatio: 1,
                    boxWidth: 600,
                    boxHeight: 600,
                    onChange: showCoords,
                    onSelect: showCoords
                });

                $('.jcrop-holder img').attr('src', e.target.result);

                //updatePreview();
                $('#crop_form').submit(function(){
                    if (parseInt($('#crop_w').val())) return true;
                    alert('Please select a crop region then press submit.');
                    return false;
                });
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#id_profile_picture").change(function(){    
        readURL(this);
    });
});

0 个答案:

没有答案