我们如何在imgareaselect中展示选定的区域

时间:2013-12-17 05:53:28

标签: javascript jquery html

代码是:

    <!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
<title>jQuery UI Tooltip - Default functionality</title>
<link rel="stylesheet" type="text/css" href="css/imgareaselect-default.css" />
      <script type="text/javascript" src="scripts/jquery.min.js"></script>
      <script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js"></script>
<script src="http://deepliquid.com/projects/Jcrop/js/jquery.min.js"></script>
        <script src="http://deepliquid.com/projects/Jcrop/js/jquery.Jcrop.js"></script>
        <link rel="stylesheet" href="http://deepliquid.com/projects/Jcrop/css/jquery.Jcrop.css" type="text/css" />
        <link rel="stylesheet" href="http://deepliquid.com/projects/Jcrop/demos/demo_files/demos.css" type="text/css" />

<link rel="stylesheet" href="/resources/demos/style.css">
<script >
$(function () { 
function readImage(file) {

    var reader = new FileReader();
    var image = new Image();
    var maxw = 600;
    var maxh = 600;

    reader.readAsDataURL(file);
    reader.onload = function (_file) {
        image.src = _file.target.result; // url.createObjectURL(file);
        image.onload = function () {
            var w = this.width,
                h = this.height,
                t = file.type, // ext only: // file.type.split('/')[1],
                n = file.name,
                s = ~~ (file.size / 1024) + 'KB';
            if (  h > maxh || w > maxw) {
                alert("Height and width is bigger then over max criteria pls select image max height and width                                            =2024X2024");
                alert(w);
                alert(h);
            } else {
                alert(w);
                alert(h);
                $('#uploadPreview').html('<img  id="myImage" src="' + this.src + '"> ' + w + 'x' + h + ' ' + s + ' ' + t + ' ' + n + '<br>');
                $('#myImage').Jcrop({
                    onChange: showPreview,
                    onSelect: showPreview,
                    aspectRatio: 1
                });
            }

        };
        image.onerror = function () {
            alert('Invalid file type: ' + file.type);
        };
    };

}

$("#choose").change(function (e) {
    if (this.disabled) return alert('File upload not supported!');
    var F = this.files;
    if (F && F[0]) for (var i = 0; i < F.length; i++) readImage(F[i]);
});

var thumbWidth = 145, thumbHeight = 190;
<!--$(function(){ $('#jcrop_target').Jcrop(); });-->
function showPreview(coords)
{
    var rx = thumbWidth / coords.width;
    var ry = thumbHeight / coords.height;

    $('#uploadPreview1 + div > img').css({
        width: Math.round(rx * $("#uploadPreview1").width()) + 'px',
        height: Math.round(ry * $("#uploadPreview1").height()) + 'px',
        marginLeft: '-' + Math.round(rx * coords.width) + 'px',
        marginTop: '-' + Math.round(ry * coords.height) + 'px'
    });
}
$('<div id=""> <img src=\"" + image + "\" \/><div>  ')
.css({
            float: 'right',
            position: 'relative',
            overflow: 'hidden',
            width: thumbWidth + 'px',
            height: thumbHeight + 'px'
        })
        .insertAfter($('#uploadPreview1'));



});




</script>



<style>



</style>
</head>
<body >
<input type="file" id="choose" multiple="multiple"   />
<br>
<div id="uploadPreview" ></div><br>

   <div id="" ><img src=""  /></div><br>

</body>
</html>

首先,我上传图像,然后检查图像的最小标准。

如果图像通过了标准,那么我将在页面上显示它。然后我选择一个区域来创建缩略图。

但是,当我选择图像的某个区域时,我无法在新的div中显示所选区域。

一切正常。但它无法显示选定的区域。我的代码中的问题是(如何将图像的src传递给img标签或div标签)

希望你能理解这个问题。

1 个答案:

答案 0 :(得分:0)

divHtml=$('<div/>',{'id':ANYID}).css({
                    float: 'right',
        position: 'relative',
        overflow: 'hidden',
        width: thumbWidth + 'px',
        height: thumbHeight + 'px'
            });
imgHtml=$('<img/>',{'src':IMGSRCPATH, 'alt':ALTVALUE, 'title':TITLEVALUE});

var appendVal = divHtml.append(imgHtml).insertAfter($('#uploadPreview'));

请试一试。