Here is the array that works when I send it to my PHP server. The images are not compressed in this array.
Array ( [name] => Array ( [0] => IMG_3272.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => C:\xampp\tmp\phpE857.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 2267094 ) )
Below is the array that does not work. At this point the images are compressed.
Array ( [name] => Array ( [0] => IMG_3270.jpg ) [type] => Array ( [0] => img/jpg ) [tmp_name] => Array ( [0] => C:\xampp\tmp\php4129.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 12 ) )
I successfully send the email with the image attachments. However, when I get the email and try to open it I get the error "It appears we don't support this file format".
When the images are not compressed the attachments to the emails are viewable.
Here is the function that is supposed to compress the images and add them to a hidden input in the HTML form .
async function handleImageUpload() {
var fileUpload = document.getElementById("fileUpload");
const options = {
maxSizeMB: 1,
maxWidthOrHeight: 1920,
useWebWorker: true
}
const compressedImages = [];
for(let i = 0; i < fileUpload.files.length; i++){
const imageFile = fileUpload.files[i];
try {
const compressedFile = await imageCompression(imageFile, options);
compressedImages.push(compressedFile)
// await uploadToServer(compressedFile); // write your own logic
} catch (error) {
console.log(error);
}
}
var fileUploads = document.getElementById("fileUploads");
const dataTransfer = new DataTransfer();
const file = new File([compressedImages[0]['name']], compressedImages[0]['name'], {type: 'img/jpg'})
dataTransfer.items.add(file);
fileUploads.files = dataTransfer.files;
console.log(fileUploads.files)
}
The reason for all of this is due to funky logic happening in the backend. For whatever reason when I get an Img array of close 6mb multiple emails get sent.
Here is the relevant PHP code. In a post request route
$files = $_FILES['uploaded_files'];
print_r($files);
die();
``
For anyone who comes across this post.
Note that the ultimate goal is to have multiple files compressed. Right now I can compress just one file. I think I will be able to do this to multiple files via a for loop.
front-end javascript--
async function handleImageUpload() {
var fileUpload = document.getElementById("fileUpload");
const options = {
maxSizeMB: 1,
maxWidthOrHeight: 1920,
useWebWorker: true
}
const compressedImages = [];
for(let i = 0; i < fileUpload.files.length; i++){
const imageFile = fileUpload.files[i];
try {
console.log(imageFile)
const compressedFile = await imageCompression(imageFile, options);
compressedImages.push(compressedFile)
console.log(compressedImages[0])
} catch (error) {
console.log(error);
}
}
//This is a hidden input in the html document. This is what will be sent to the server.
var fileUploads = document.getElementById("fileUploads");
const dataTransfer = new DataTransfer();
const file = new File([compressedImages[0]], 'Hello_world.jpg',{type:"image/jpeg"})
dataTransfer.items.add(file);
fileUploads.files = dataTransfer.files;
console.log(fileUploads.files)
}
I used this npm package via CDN https://www.npmjs.com/package/browser-image-compression