forked from extern/easydiffusion
Fix a bug where setting an initial image would mess up the width and height field
This commit is contained in:
parent
b8328b6071
commit
b97c906128
@ -2162,6 +2162,7 @@ function checkRandomSeed() {
|
|||||||
randomSeedField.addEventListener("input", checkRandomSeed)
|
randomSeedField.addEventListener("input", checkRandomSeed)
|
||||||
checkRandomSeed()
|
checkRandomSeed()
|
||||||
|
|
||||||
|
// warning: the core plugin `image-editor-improvements.js:172` replaces loadImg2ImgFromFile() with a custom version
|
||||||
function loadImg2ImgFromFile() {
|
function loadImg2ImgFromFile() {
|
||||||
if (initImageSelector.files.length === 0) {
|
if (initImageSelector.files.length === 0) {
|
||||||
return
|
return
|
||||||
|
@ -124,35 +124,17 @@
|
|||||||
// Draw the image with centered coordinates
|
// Draw the image with centered coordinates
|
||||||
context.drawImage(imageObj, x, y, this.width, this.height);
|
context.drawImage(imageObj, x, y, this.width, this.height);
|
||||||
|
|
||||||
initImagePreview.src = canvas.toDataURL('image/png');
|
let bestWidth = maxCroppedWidth - maxCroppedWidth % 8
|
||||||
|
let bestHeight = maxCroppedHeight - maxCroppedHeight % 8
|
||||||
|
|
||||||
// Get the options from widthField and heightField
|
addImageSizeOption(bestWidth)
|
||||||
const widthOptions = Array.from(widthField.options).map(option => parseInt(option.value));
|
addImageSizeOption(bestHeight)
|
||||||
const heightOptions = Array.from(heightField.options).map(option => parseInt(option.value));
|
|
||||||
|
|
||||||
// Find the closest aspect ratio and closest to original dimensions
|
|
||||||
let bestWidth = widthOptions[0];
|
|
||||||
let bestHeight = heightOptions[0];
|
|
||||||
let minDifference = Math.abs(maxCroppedWidth / maxCroppedHeight - bestWidth / bestHeight);
|
|
||||||
let minDistance = Math.abs(maxCroppedWidth - bestWidth) + Math.abs(maxCroppedHeight - bestHeight);
|
|
||||||
|
|
||||||
for (const width of widthOptions) {
|
|
||||||
for (const height of heightOptions) {
|
|
||||||
const difference = Math.abs(maxCroppedWidth / maxCroppedHeight - width / height);
|
|
||||||
const distance = Math.abs(maxCroppedWidth - width) + Math.abs(maxCroppedHeight - height);
|
|
||||||
|
|
||||||
if (difference < minDifference || (difference === minDifference && distance < minDistance)) {
|
|
||||||
minDifference = difference;
|
|
||||||
minDistance = distance;
|
|
||||||
bestWidth = width;
|
|
||||||
bestHeight = height;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the width and height to the closest aspect ratio and closest to original dimensions
|
// Set the width and height to the closest aspect ratio and closest to original dimensions
|
||||||
widthField.value = bestWidth;
|
widthField.value = bestWidth;
|
||||||
heightField.value = bestHeight;
|
heightField.value = bestHeight;
|
||||||
|
|
||||||
|
initImagePreview.src = canvas.toDataURL('image/png');
|
||||||
};
|
};
|
||||||
|
|
||||||
function handlePaste(e) {
|
function handlePaste(e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user