the code is partially working, it collects images and resize them into base64 in a loop but i am not getting the images to send when i append it in form... but when i post simple pictures by removing the resize function it sends every picture... i don't understand that what is the issue..
heres the html:
Upload Pictures <input type="file" name="file[]" id="file" multiple/>
heres i am collecting pictures:
<script>
$(document).on('change','#file',function(){
files = this.files;
ajax_file_upload(files);
});
</script>
heres the code where i am resizing them in a loop and sending them through ajax: note: in the resize function i am getting the resized picture in the console... i will attach a picture of console as well...
<script>
function ajax_file_upload(file_obj) {
if(file_obj != undefined) {
var status='true';
var image = new FormData();
var match = ['image/jpeg', 'image/png', 'image/jpg'];
for(i=0; i<file_obj.length; i++) {
var fileType = file_obj[i].type;
// alert(fileType)
if(!((fileType == match[0]) || (fileType == match[1]) || (fileType == match[2]) || (fileType == match[3]) || (fileType == match[4]) || (fileType == match[5]))){
var status='false';
}
resizeImages(file_obj[i],function(dataUrl){
image.append('file[]',dataUrl);
});
}
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{ route('resize') }}",
type: "POST",
// data: {pictures:pictures},
data:image,
cache:false,
processData: false,
contentType: false,
datatype:'html',
success: function (response) {
return true;
}
});
}
}
</script>
here the resize function i am using to resize the images and getting dataurl:
<script>
function resizeImages(file,complete) {
// read file as dataUrl
//////// 2. Read the file as a data Url
var reader = new FileReader();
// file read
reader.onload = function(e) {
// create img to store data url
////// 3 - 1 Create image object for canvas to use
var img = new Image();
img.onload = function() {
/////////// 3-2 send image object to function for manipulation
complete(resizeInCanvas(img));
};
img.src = e.target.result;
}
// read file
reader.readAsDataURL(file);
}
function resizeInCanvas(img){
///////// 3-3 manipulate image
var perferedWidth = 1200;
var ratio = perferedWidth / img.width;
var canvas = $("<canvas>")[0];
canvas.width = img.width * ratio;
canvas.height = img.height * ratio;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0,0,canvas.width, canvas.height);
//////////4. export as dataUrl
return canvas.toDataURL('image/jpeg', 0.6);
}
</script>
this function is working perfectly... now when i send it to the server it doesn't posts anything.. tried it many ways researched but didn't got anything.. please guide me thanks
