2017-02-21 241 views
2

我想通过设置其高度和宽度属性customizedCycrop使用Jcrop裁剪大图像。我已经在Jcrop中尝试了很多选项,但似乎没有任何工作。这里是我的代码:如何通过调整大小使用Jcrop裁剪大图像?

HTML:

<input type="file" id="FileUpload1" accept=".jpg,.png,.gif" /> 
<br /> 
<br /> 
<table border="0" cellpadding="0" cellspacing="5"> 
    <tr> 
     <td> 
      <img id="Image1" src="" alt="" style="display: none; height:600px; width:600px;" /> 
     </td> 
     <td> 
      <canvas id="canvas" height="5" width="5"></canvas> 
     </td> 
    </tr> 
</table> 
<br /> 
<input type="button" id="btnCrop" value="Crop" style="display: none" /> 
<input type="hidden" name="imgX1" id="imgX1" /> 
<input type="hidden" name="imgY1" id="imgY1" /> 
<input type="hidden" name="imgWidth" id="imgWidth" /> 
<input type="hidden" name="imgHeight" id="imgHeight" /> 
<input type="hidden" name="imgCropped" id="imgCropped" /> 

的Jquery:

$(document).delegate('#cover-image','change', function(e){ 
     var reader = new FileReader(); 
     reader.onload = function (e) { 
      $('#Image1').show(); 
      $('#Image1').attr("src", e.target.result); 
      $('#Image1').Jcrop({ 
       setSelect: [ 0,0,600,180 ], 
       aspectRatio: 10/3, 
       boxWidth: 600, 
       boxHeight: 600, 
       trueSize: [600, 600], 
       onChange: SetCoordinates, 
       onSelect: SetCoordinates 
      }); 
     } 

     reader.readAsDataURL($(this)[0].files[0]);  
    }); 

    $('#btnCrop').click(function() { 
     var x1 = $('#imgX1').val(); 
     var y1 = $('#imgY1').val(); 
     var width = $('#imgWidth').val(); 
     var height = $('#imgHeight').val(); 
     var canvas = $("#canvas")[0]; 
     var context = canvas.getContext('2d'); 
     var img = new Image(); 
     img.onload = function() { 
      canvas.height = 180; 
      canvas.width = 600; 
      context.drawImage(img, x1, y1, width, height, 0, 0, canvas.width, canvas.height); 
      $('#imgCropped').val(canvas.toDataURL()); 
      $('#btnCrop').hide(); 
     }; 
     img.src = $('#Image1').attr("src"); 
    }); 
}); 

function SetCoordinates(c) { 
    $('#imgX1').val(c.x); 
    $('#imgY1').val(c.y); 
    $('#imgWidth').val(c.w); 
    $('#imgHeight').val(c.h); 
    $('#btnCrop').show(); 
    $('#save-cropped-image, #delete-image').hide(); 
}; 

我试图作物在一个div比图像更短的一个大的图像配合,因此图像得到压缩在里面。但是当我剪下这张图片时,它不会返回给我,正确的坐标和正确的图像不会在画布中产生。

我被困在4小时。任何帮助将不胜感激。 谢谢!

+0

请检查http://deepliquid.com/content/Jcrop_Manual.html –

+0

我已经看到这个文档。但没有找到完美的解决方案。 –

回答

1

使用此代码。获取图像的原始大小,然后裁剪。

$(document).delegate('#cover-image','change', function(e){ 
      var reader = new FileReader(); 
      reader.onload = function (e) { 
       $('#Image1').attr("src", e.target.result); 
       var $img = $("#Image1"); 
       $img.hide().removeClass("image12"); 
       var w = $img.width(); 
       var h = $img.height(); 
       $('#Image1').Jcrop({ 
        trueSize: [w, h], 
        onChange: SetCoordinates, 
        onSelect: SetCoordinates 
       }); 
      } 

      reader.readAsDataURL($(this)[0].files[0]);  
     }); 

<style type="text/css"> 
.image12{ 
    height:600px; 
    width:600px; 
} 
</style> 

<img id="Image1" class="image12" src="" alt="" style="display: none; " /> 
+0

谢谢!哈林: - )......它工作得很好。 –