Using simpleCropper.js to crop the image and using ajax upload the image in PHP in simpleCropper the image file is converted to base64. But try to upload the large image file, it shows errors like Image corrupt or truncated. URI in this note truncated due to length.
In ajax response it shows error like Request Entity Too Large
The small cropped image is not uploaded properly and it is stored in a 10-byte size.
in the ajax upload file it returns the null value.
if (file.type.match(imageType)) {
var reader = new FileReader();
image_filename = file.name;
reader.onload = function (e) {
var base64data = reader.result;
$.ajax({
url: "upload.php",
method: "POST",
data: {imagefile: base64data},
dataType:"text",
success: function(data){
console.log(data);
}
});
Upload.php
if(isset($_POST["imagefile"]))
{
$data = $_POST["imagefile"];
$image_array_1 = explode(";", $data);
$image_array_2 = explode(",", $image_array_1[1]);
$data = base64_decode($image_array_2[1]);
$filename = time() . '.png';
if(stripos($data, 'image/png') !== false) {
$filename = time() . '.png';
} elseif(stripos($data, 'image/jpg') !== false || stripos($data, 'image/jpeg') !== false) {
$filename = time() . '.jpg';
}
$imageName = 'assets/uploads/' . $filename;
file_put_contents($imageName, $data);
echo $filename;
}