2017-04-25 127 views
0

我可以在沒有畫布的情況下裁剪圖像嗎?如何在不調整圖像大小的情況下裁剪圖像?像這樣真實的畫面是133px X 133px如何用jquery裁剪圖像?

Large ImageSmall Image

Large ImageSmall Image

而且thiis我的代碼https://jsfiddle.net/w26La1u6/2/

$(document).on('change', '#files', function(event) { 
     var files = event.target.files; // FileList object 
     var output = document.getElementById("list"); 

     for (var i = 0, f; f = files[i]; i++) { 
       if (f.type.match('image.*')) { 
         if (this.files[0].size < 12097152) { 
           var reader = new FileReader(); 
           reader.onload = (function(theFile) { 
             return function(e) { 
               var span = document.createElement('span'); 
               span.innerHTML = ['<img class="thumbnail" src="', e.target.result, 
                 '" title="', escape(theFile.name), '"/>' 
               ].join(''); 
               output.insertBefore(span, null); 
             }; 
           })(f); 

           $('#clear, #list').show(); 
           reader.readAsDataURL(f); 
         } else { 
           alert("Image Size is too big. Minimum size is 2MB."); 
           $(this).val(""); 
         } 
       } 
     } 
}) 

但是,如果沒有其他如何在沒有畫布的情況下裁剪,告訴我如何用畫布裁剪

編輯

<input id="uploadImage" type="file" accept="image/*" capture="camera" /> 
<img id="imgDisplay" src="http://placehold.it/300x200" alt="Not a kitten" /> 


var Resample = (function (canvas) { 

    // (C) WebReflection Mit Style License 

    function Resample(img, width, height, onresample) { 

    var load = typeof img == "string", 
    i = load || img; 

    if (load) { 
    i = new Image; 
    // with propers callbacks 
    i.onload = onload; 
    i.onerror = onerror; 
    } 

    i._onresample = onresample; 
    i._width = width; 
    i._height = height; 

    load ? (i.src = img) : onload.call(img); 
    } 

    function onerror() { 
    throw ("not found: " + this.src); 
    } 

    function onload() { 
    var 
    img = this, 
    width = img._width, 
    height = img._height, 
    onresample = img._onresample 
    ; 

    // Altered section - crop prior to resizing 
    var imgRatio = img.width/img.height; 
    var desiredRatio = width/height; 
    var cropWidth, cropHeight; 
    if (desiredRatio < imgRatio) { 
     cropHeight = img.height; 
     cropWidth = img.height * desiredRatio; 
    } else { 
     cropWidth = img.width; 
     cropHeight = img.width/desiredRatio; 
    } 

    delete img._onresample; 
    delete img._width; 
    delete img._height; 

    canvas.width = width; 
    canvas.height = height; 

    context.drawImage(
    // original image 
    img, 
    // starting x point 
    0, 
    // starting y point 
    0, 
    // crop width 
    cropWidth, 
    // crop height 
    cropHeight, 
    // destination x point 
    0, 
    // destination y point 
    0, 
    // destination width 
    width, 
    // destination height 
    height 
    ); 

    onresample(canvas.toDataURL("image/png")); 
    } 

    var context = canvas.getContext("2d"), 
    round = Math.round; 

    return Resample; 

}(
this.document.createElement("canvas")) 
); 

var newCropWidth = 133; 
var newCropHeight = 133; 

function loadImage(data) { 
    document.querySelector('#imgDisplay').src = data; 
} 
function handleFileSelect(evt) { 
    if (evt.target.files.length === 1) { 
    var picFile = evt.target.files[0]; 

    if (picFile.type.match('image.*')) { 
     var fileTracker = new FileReader; 
     fileTracker.onload = function() { 
     Resample(
     this.result, 
     newCropWidth, 
     newCropHeight, 
     loadImage 
     ); 
     } 
     fileTracker.readAsDataURL(picFile); 
    } 
    } 
} 

document.querySelector('#uploadImage').addEventListener('change', handleFileSelect, false); 
+0

你可以看看ImageMagick的對裁剪圖像的選項。但是,您需要使用服務器端語言。 –

+1

您想在將圖像上傳到服務器之前裁剪圖像,請修正?如果您只想顯示圖像的一部分,但不實際更改圖像,則可以通過將圖像放置在使用CSS的較小邊框內來實現。但是,這並不會改變圖像本身,只是希望用戶可以看到它。那會滿足你的需求,還是你真的需要一個真正的作物? – apsillers

+0

apsillers - 是的,我需要一個真正的作物,如果使用畫布怎麼樣?看到上面的編輯代碼,它只是調整它的大小,但不會裁剪圖像,如示例中所示。我想將其轉換爲133px x 133px的大小。以及如何將上面的畫布代碼與我以前的代碼結合起來? – natasha

回答

1

有辦法使用jQuery UI做,但你可以只使用一個插件別人已經作出諸如Cropper

+0

http://stackoverflow.com/questions/43626453/how-to-combine-jquery-with-cropping-code-on-canvas打開這個,我改變了問題 – natasha