in the hooks/tablename-dv.js we can add the following code:
Code: Select all
$j(function() {
var fileInput = $j('#imagefield);
fileInput.on('change', function(e) {
var file = e.target.files[0];
if (file && file.type.match('image.*')) {
// Maximum allowed size in bytes (1MB)
const MAX_SIZE = 1048576;
if (file.size <= MAX_SIZE) {
return;
}
var reader = new FileReader();
reader.onload = function(readerEvent) {
var image = new Image();
image.onload = function(imageEvent) {
var canvas = document.createElement('canvas'),
max_width = 1280, // Set maximum desired width
max_height = 1024, // Set maximum desired height
width = image.width,
height = image.height;
if (width > height) {
if (width > max_width) {
height *= max_width / width;
width = max_width;
}
} else {
if (height > max_height) {
width *= max_height / height;
height = max_height;
}
}
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, width, height);
canvas.toBlob(function(blob) {
if (blob.size > MAX_SIZE) {
alert('The resized image is still too large. Please choose a smaller image or crop it.');
$j('#imagefield').val('');
return;
}
var newFile = new File([blob], file.name, {
type: blob.type,
lastModified: Date.now()
});
var dataTransfer = new DataTransfer();
dataTransfer.items.add(newFile);
e.target.files = dataTransfer.files;
console.log('Original size:', file.size, 'New size:', newFile.size);
}, 'image/jpeg', 0.7);
};
image.src = readerEvent.target.result;
};
reader.readAsDataURL(file);
}
});
});Note: Ignore the warning and save the record.
