调整使用Jcrop的图像

5 javascript jquery resize crop jcrop

我使用Jquery Jcrop裁剪我的图像.现在我正在实现一个用于调整图像大小的滑块.我希望裁剪和调整大小发生在同一页面上.

我是这样做的:

$(document).ready(function() {


    var img = $('#cropbox')[0]; // Get my img elem
    var orgwidth, orgheight;
    $("<img/>") // Make in memory copy of image to avoid css issues
        .attr("src", $(img).attr("src"))
        .load(function() {
            orgwidth = this.width;   // Note: $(this).width() will not
            orgheight = this.height; // work for in memory images.
        });

    $('#cropbox').Jcrop({
        onSelect: updateCoords
    });

    $("#imageslider").slider({
        value: 100,
        max: 100,
        min: 1,
        slide: function(event, ui) {
            //$('ul#grid li').css('font-size',ui.value+"px");
            resizeImage(orgwidth, orgheight);
        }
    });

});
Run Code Online (Sandbox Code Playgroud)

还有我简单的resizeImage函数:

function resizeImage(orgwidth, orgheight) {
    var value = $('#imageslider').slider('option', 'value');
    var width = orgwidth * (value / 100);
    var height = orgheight * (value / 100);
    $('#cropbox').width(width);
    $('#cropbox').height(height);
    $('#tester').val("org: "+orgwidth+" now: "+width);
}
Run Code Online (Sandbox Code Playgroud)

问题是,一旦我打开Jcrop我就无法调整图像大小.我怎样才能同时使用这两个功能?

小智 5

我在调整大小时重新调整大小并重新启动时最终破坏了jCrop.不管怎么说,还是要谢谢你.码:

        function resizeImage(orgwidth, orgheight) {
        jcrop_api.destroy();

        var value = $('#imageslider').slider('option', 'value');
        var width = orgwidth * (value / 100);
        var height = orgheight * (value / 100);
        $('#cropbox').width(width);
        $('#cropbox').height(height);
        $('#rw').val(width);
        $('#rh').val(height);

        initJcrop();

    }
Run Code Online (Sandbox Code Playgroud)