Przez jakiś czas używałem takiej funkcji do zmieniania wielkości obrazu do max 1MB i wysyłania go do express a następnie zapisywania w s3:
export const postImage = async (image_id, image) => {
const axiosConfig = {
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
responseType: 'arraybuffer'
}
// Create a new Image object
const img = new Image();
// Set the source of the Image object to the base64 string
img.src = image;
// Wait for the image to load
img.onload = () => {
// Create a new HTML5 Canvas object
const canvas = document.createElement('canvas');
// Calculate the new dimensions of the image
let newWidth = img.width;
let newHeight = img.height;
const maxFileSize = 1 * 1024 * 1024; // 1mb in bytes
const scaleFactor = Math.min(1, Math.sqrt(maxFileSize / (img.width * img.height * 4)));
newWidth = Math.floor(scaleFactor * img.width);
newHeight = Math.floor(scaleFactor * img.height);
// Set the dimensions of the canvas
canvas.width = newWidth;
canvas.height = newHeight;
// Draw the image onto the canvas at the new dimensions
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, newWidth, newHeight);
// Convert the canvas to a new base64 string
const resizedBase64 = canvas.toDataURL("image/jpg");
// Use the resized base64 string as needed
console.log(image)
console.log(resizedBase64);
return beStructionApi.post(`/image/${image_id}`, { data: resizedBase64 }, axiosConfig).then((result) => {
console.log(result)
return result
})
};
}
Przez kilka tygodni wszystko działałao. Wczoraj zauważyłem, że obrazy które wysyłam mają znacznie gorszą jakość niż wcześniej. Dla porównania wysłałem tą samą grafikę, którą wysyłałem miesiąc temu. I następnie załadowałem obie z s3. Różnica jest ogromna. Ten 2 obraz wygląda jakby ważył kilka kb co najwyżej. W ogóle nie zmieniałem kodu, zaktualizowałem biblioteki. Czy ktoś wie co może być przyczyną?