forked from GNUsocial/gnu-social
[AVATAR] Fix JS cropping script and save square image, in case other themes need it
This commit is contained in:
@@ -1,37 +1,19 @@
|
||||
function getRoundedCanvas(sourceCanvas) {
|
||||
var canvas = document.createElement('canvas');
|
||||
var context = canvas.getContext('2d');
|
||||
var width = sourceCanvas.width;
|
||||
var height = sourceCanvas.height;
|
||||
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
context.imageSmoothingEnabled = true;
|
||||
context.drawImage(sourceCanvas, 0, 0, width, height);
|
||||
context.globalCompositeOperation = 'destination-in';
|
||||
context.beginPath();
|
||||
context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI, true);
|
||||
context.fill();
|
||||
return canvas;
|
||||
}
|
||||
|
||||
var input = document.getElementById('form_avatar')
|
||||
var container = document.getElementById('img-container')
|
||||
var cropImage = document.getElementById('img-cropped')
|
||||
var cropped = document.getElementById('form_hidden')
|
||||
var input = document.getElementById('save_avatar');
|
||||
var container = document.getElementById('img-container');
|
||||
var cropImage = document.getElementById('img-cropped');
|
||||
var cropped = document.getElementById('save_hidden');
|
||||
|
||||
input.addEventListener('change', function (e) {
|
||||
if (!input.files || !input.files[0]) return;
|
||||
var reader = new FileReader()
|
||||
reader.onload = function (e) {
|
||||
container.style = 'display: block'
|
||||
cropImage.setAttribute('src', e.target.result)
|
||||
container.style = 'display: block';
|
||||
cropImage.setAttribute('src', e.target.result);
|
||||
|
||||
function update() {
|
||||
var croppedCanvas = cropper.getCroppedCanvas()
|
||||
var roundedCanvas = getRoundedCanvas(croppedCanvas)
|
||||
cropped.value = roundedCanvas.toDataURL()
|
||||
input.files = undefined
|
||||
var croppedCanvas = cropper.getCroppedCanvas();
|
||||
cropped.value = croppedCanvas.toDataURL();
|
||||
input.files = undefined;
|
||||
}
|
||||
var cropper = new Cropper(cropImage, {
|
||||
aspectRatio: 1,
|
||||
@@ -40,6 +22,5 @@ input.addEventListener('change', function (e) {
|
||||
ready: update,
|
||||
})
|
||||
}
|
||||
reader.readAsDataURL(input.files[0])
|
||||
})
|
||||
|
||||
reader.readAsDataURL(input.files[0]);
|
||||
});
|
||||
|
Reference in New Issue
Block a user