调整主图像大小后,jcrop演示不再有效(使用图片更新)

时间:2010-11-01 16:12:32

标签: jquery jcrop

我正在尝试replicate this jcrop demo。一切正常,但我的原始图片非常大,所以as per the manual, i am resizing them在我的页面上是这样的:

 jQuery('#cropbox').Jcrop({
            onChange: showPreview,
            onSelect: showPreview,
            bgColor: 'white',
            aspectRatio: 1,
            boxWidth: 300,
            boxHeight: 500
        });

问题是现在第二个预览窗口(id =“preview”)不再显示裁剪框上裁剪部分的内容。这是一个例子:

alt text

显然,预览窗口与我在第一张图片中裁剪的区域不匹配。

任何想法如何在预先调整主图像大小时正确显示预览图像?

2 个答案:

答案 0 :(得分:4)

尝试this JSFiddle

HTML:

<img id="cropbox" src="http://homepage.mac.com/davydewit/popacademie/rw_common/themes/blendit/images/header/image1.jpg" />

<br />

<br />
<div id="previewcrop">
<img id="preview" src="http://homepage.mac.com/davydewit/popacademie/rw_common/themes/blendit/images/header/image1.jpg" />
</div>

的CSS:

#previewcrop{
    width: 100px;
    height: 100px;
    overflow: hidden;
    margin-left: 5px;
}

JS:

var imgwidth = 849; // width of the image
var imgheight = 423; // height of the image

jQuery('#cropbox').Jcrop({
            onChange: showPreview,
            onSelect: showPreview,
            bgColor: 'white',
            aspectRatio: 1,
            boxWidth: imgwidth/3,
            boxHeight: imgheight/3
        });

function showPreview(coords)
{
    var rx = 100 / coords.w;
    var ry = 100 / coords.h;

    $('#preview').css({
        width: Math.round(rx * imgwidth) + 'px',
        height: Math.round(ry * imgheight) + 'px',
        marginLeft: '-' + Math.round(rx * coords.x) + 'px',
        marginTop: '-' + Math.round(ry * coords.y) + 'px'
    });
};

答案 1 :(得分:0)

function showPreview(coords)
{
    if (parseInt(coords.w) > 0)
    {
        var rx = 100 / coords.w;
        var ry = 100 / coords.h;

        jQuery('#preview').css({
            width: Math.round(rx * 800) + 'px',
            height: Math.round(ry * 600) + 'px',
            marginLeft: '-' + Math.round(rx * coords.x) + 'px',
            marginTop: '-' + Math.round(ry * coords.y) + 'px'
        });
    }
}

对于(rx * 800),800应该是真实图像的宽度。对于(ry * 600),600应该是真实图像的宽度。我已经针对800x600图像测试了这个并且它可以工作(使用Tutorial3.html并对其进行修改)。另外,不要使用缩放图像的宽度和高度,它将无法工作。