2017-12-26 37 views
2

클라이언트 측에서 이미지 조작을 위해 JCrop.js를 사용하고 있습니다. html 페이지에서 이미지를 회전, 크기 조절, 뒤집기 및 자르기해야합니다. 페이지에 파일 선택기가 있으면이 작업을 수행 할 수 있습니다. 내 요구 사항은 로컬 이미지를 선택하지 않고 다른 URL에서로드 된 이미지에 대해서도 동일한 작업을 수행하는 것입니다. 다음은 페이지의 "파일"컨트롤과 잘 작동하는 전체 코드입니다. 로컬 파일을 선택하고 싶지 않습니다. 내 페이지가 다른 소스의 이미지를로드 한 다음로드 된 이미지에서 작업을 수행해야합니다.JCrop : 정적 이미지 회전 및 자르기

<img scr="externalURL" id="imgMain" alt=""/> 
<input type="button" id="cropbutton" value="Crop" /> 
<input type="button" id="rotatebutton" value="Rotate" /> 
<input type="button" id="hflipbutton" value="Flip Horizontal" /> 
<input type="button" id="vflipbutton" value="Flip Vertical" /> 

내가 변경해야 :

var cropWidth = 800; 
var cropHeight = 800; 
var crop_max_width = 400; 
var crop_max_height = 400; 
var jcrop_api; 
var canvas; 
var context; 
var image; 

var prefsize; 
$(document).ready(function() { 
    $("#file").change(function() { 
     loadImage(this); 
    }); 
    $("#btnCrop").click(function() {  
     SaveData(); 
    }); 
}); 

function SaveData() { 
    formData = new FormData($(this)[0]); 
    var blob = dataURLtoBlob(canvas.toDataURL('image/jpeg')); 
    formData.append("cropped_image[]", blob, "CroppedImage.jpeg"); 
    $.ajax({ 
     url: urlToSendData 
     type: "POST", 
     data: formData,   
     contentType: false, 
     cache: false, 
     processData: false, 
     success: function (data) { 
      alert("Image cropped!");    
     }, 
     error: function (data) { 
      alert('There was an error'); 
     } 
    }); 
} 

function loadImage(input) { 
    if (input.files && input.files[0]) { 
     var reader = new FileReader(); 
     canvas = null; 
     reader.onload = function (e) { 
      image = new Image(); 
      image.onload = validateImage; 
      image.src = e.target.result; 
     } 
     reader.readAsDataURL(input.files[0]); 
     applyCrop(); 
    } 
} 

function dataURLtoBlob(dataURL) { 
    var BASE64_MARKER = ';base64,'; 
    if (dataURL.indexOf(BASE64_MARKER) == -1) { 
     var parts = dataURL.split(','); 
     var contentType = parts[0].split(':')[1]; 
     var raw = decodeURIComponent(parts[1]); 

     return new Blob([raw], { 
      type: contentType 
     }); 
    } 
    var parts = dataURL.split(BASE64_MARKER); 
    var contentType = parts[0].split(':')[1]; 
    var raw = window.atob(parts[1]); 
    var rawLength = raw.length; 
    var uInt8Array = new Uint8Array(rawLength); 
    for (var i = 0; i < rawLength; ++i) { 
     uInt8Array[i] = raw.charCodeAt(i); 
    } 

    return new Blob([uInt8Array], { 
     type: contentType 
    }); 
} 

function validateImage() { 
    if (canvas != null) { 
     image = new Image(); 
     image.onload = restartJcrop; 
     image.src = canvas.toDataURL('image/png'); 
    } else restartJcrop(); 
} 

function restartJcrop() { 
    if (jcrop_api != null) { 
     jcrop_api.destroy(); 
    } 
    $("#views").empty(); 
    $("#views").append("<canvas id=\"canvas\">"); 
    canvas = $("#canvas")[0]; 
    context = canvas.getContext("2d"); 
    canvas.width = image.width; 
    canvas.height = image.height; 
    context.drawImage(image, 0, 0); 
    $("#canvas").Jcrop({ 
     onSelect: selectcanvas, 
     onRelease: clearcanvas, 
     boxWidth: crop_max_width, 
     boxHeight: crop_max_height, 
     allowResize: false, 
     allowSelect: false, 
     setSelect: [0, 0, cropWidth, cropHeight], 
     aspectRatio: 1 
    }, function() { 
     jcrop_api = this; 
    }); 
    clearcanvas(); 
} 

function clearcanvas() { 
    prefsize = { 
     x: 0, 
     y: 0, 
     w: canvas.width, 
     h: canvas.height, 
    }; 
} 

function selectcanvas(coords) { 
    prefsize = { 
     x: Math.round(coords.x), 
     y: Math.round(coords.y), 
     w: Math.round(coords.w), 
     h: Math.round(coords.h) 
    }; 
} 

function applyCrop() { 
    canvas.width = prefsize.w; 
    canvas.height = prefsize.h; 
    context.drawImage(image, prefsize.x, prefsize.y, prefsize.w, prefsize.h, 0, 0, canvas.width - 10, canvas.height - 10); 
    validateImage(); 
} 

function applyScale(scale) { 
    if (scale == 1) return; 
    canvas.width = canvas.width * scale; 
    canvas.height = canvas.height * scale; 
    context.drawImage(image, 0, 0, canvas.width, canvas.height); 
    validateImage(); 
} 

function applyRotate() { 
    canvas.width = image.height; 
    canvas.height = image.width; 
    context.clearRect(0, 0, canvas.width, canvas.height); 
    context.translate(image.height/2, image.width/2); 
    context.rotate(Math.PI/2); 
    context.drawImage(image, -image.width/2, -image.height/2); 
    validateImage(); 
} 

function applyHflip() { 
    context.clearRect(0, 0, canvas.width, canvas.height); 
    context.translate(image.width, 0); 
    context.scale(-1, 1); 
    context.drawImage(image, 0, 0); 
    validateImage(); 
} 

function applyVflip() { 
    context.clearRect(0, 0, canvas.width, canvas.height); 
    context.translate(0, image.height); 
    context.scale(1, -1); 
    context.drawImage(image, 0, 0); 
    validateImage(); 
} 

$("#scalebutton").click(function (e) { 
    var scale = prompt("Scale Factor:", "1"); 
    applyScale(scale); 
}); 
$("#rotatebutton").click(function (e) { 
    applyRotate(); 
}); 
$("#hflipbutton").click(function (e) { 
    applyHflip(); 
}); 
$("#vflipbutton").click(function (e) { 
    applyVflip(); 
}); 

그래서, 내 페이지에 난 단지 이미지와 네 개의 버튼을 가지고 있습니까?

답변

0

loadImage은 파일을 읽고 image을 글로벌로 설정합니다. 페이지에 이미지가 이미 있으므로이 단계를 건너 뛸 수 있어야합니다. ready의 이미지를 다음과 같이 설정할 수 있습니다.

$(document).ready(function() { 
    // (this code replaces the file selecting) 
    image = $("#imgMain"); // set image from the page 
    canvas = null; // this was done in loadImage 
    applyCrop(); // this was called from loadImage 
    // (this code is the same as before) 
    $("#btnCrop").click(function() {  
     SaveData(); 
    }); 
});