I have an image cropper that crops the uploaded image, Before sending it to the server using PHP and AJAX.
Here is a live fiddle to preview a working cropping example.
Here is the code:
// Create cropped part.
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;
}
// On uploading a file
$("#input").on("change", function(e) {
var _URL = window.URL || window.webkitURL,
file = this.files[0],
image = new Image();
image.src = _URL.createObjectURL(file);
image.onload = function(e) {
var image = document.getElementById('image'),
button = document.getElementById('button');
$('#image').attr('src', _URL.createObjectURL(file));
$('#image').show();
$('#button').show();
var cropper = new Cropper(image, {
aspectRatio: 1,
movable: false,
cropBoxResizable: true,
dragMode: 'move',
ready: function () {
croppable = true;
button.onclick = function () {
var croppedCanvas;
var roundedCanvas;
var roundedImage;
if (!croppable) {
return;
}
// Crop
croppedCanvas = cropper.getCroppedCanvas();
cropper.getCroppedCanvas({
fillColor: '#fff',
imageSmoothingEnabled: true,
imageSmoothingQuality: 'high',
});
// Round
roundedCanvas = getRoundedCanvas(croppedCanvas);
// Show
roundedImage = document.createElement('img');
roundedImage.src = roundedCanvas.toDataURL();
result.innerHTML = '';
result.appendChild(roundedImage);
}
}
});
}
});
#image,
#button{
display:none
}
<!-- Cropper CSS -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.4.1/cropper.min.css">
<!-- Cropper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.4.1/cropper.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<!-- File input -->
<input type="file" id="input" name="image">
<!-- Selected image will be displayed here -->
<img id="image" src="" alt="Picture">
<!-- Button to scrop the image -->
<button type="button" id="button">Crop</button>
<!-- Preview cropped part -->
<div id="result"></div>
Upload an image and click crop, The cropped part would be displayed beneath it.
Then I use the following code to send a blob to PHP using Ajax:
cropper.getCroppedCanvas().toBlob(function (blob) {
var formData = new FormData();
formData.append('avatar', blob);
// Use `jQuery.ajax` method
$.ajax('upload.php', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function (response) {
},
error: function () {
}
});
});
In upload.php:
if( isset($_FILES['avatar']) and !$_FILES['avatar']['error'] ){
file_put_contents( "uploads/image.png", file_get_contents($_FILES['avatar']['tmp_name']) );
}
Should I make some checks for security or just the dimensions and size checks?