mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
resize images to 100x100 for style preset images
This commit is contained in:
parent
9a4d075074
commit
442fc02429
@ -9,21 +9,33 @@ import { useCallback } from 'react';
|
||||
*/
|
||||
export const useImageUrlToBlob = () => {
|
||||
const imageUrlToBlob = useCallback(
|
||||
async (url: string) =>
|
||||
async (url: string, dimension?: number) =>
|
||||
new Promise<Blob | null>((resolve) => {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
console.log("on load")
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = img.width;
|
||||
canvas.height = img.height;
|
||||
let width = img.width;
|
||||
let height = img.height;
|
||||
|
||||
if (dimension) {
|
||||
const aspectRatio = img.width / img.height;
|
||||
if (img.width > img.height) {
|
||||
width = dimension;
|
||||
height = dimension / aspectRatio;
|
||||
} else {
|
||||
height = dimension;
|
||||
width = dimension * aspectRatio;
|
||||
}
|
||||
}
|
||||
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
const context = canvas.getContext('2d');
|
||||
if (!context) {
|
||||
console.log("no context")
|
||||
return;
|
||||
}
|
||||
context.drawImage(img, 0, 0);
|
||||
context.drawImage(img, 0, 0, width, height);
|
||||
resolve(
|
||||
new Promise<Blob | null>((resolve) => {
|
||||
canvas.toBlob(function (blob) {
|
||||
|
@ -72,7 +72,7 @@ export const useImageActions = (image_name?: string) => {
|
||||
if (image_name && metadata && imageDTO) {
|
||||
const positivePrompt = await handlers.positivePrompt.parse(metadata)
|
||||
const negativePrompt = await handlers.negativePrompt.parse(metadata)
|
||||
const imageBlob = await imageUrlToBlob(imageDTO.image_url)
|
||||
const imageBlob = await imageUrlToBlob(imageDTO.image_url, 100)
|
||||
|
||||
dispatch(prefilledFormDataChanged({ name: "", positivePrompt, negativePrompt, image: imageBlob ? new File([imageBlob], "stylePreset.png", { type: 'image/png', }) : null }))
|
||||
dispatch(isModalOpenChanged(true))
|
||||
|
@ -28,7 +28,7 @@ export const StylePresetListItem = ({ preset }: { preset: StylePresetRecordWithI
|
||||
const { positive_prompt, negative_prompt } = preset_data;
|
||||
let imageBlob = null;
|
||||
if (preset.image) {
|
||||
imageBlob = await imageUrlToBlob(preset.image);
|
||||
imageBlob = await imageUrlToBlob(preset.image, 100);
|
||||
}
|
||||
|
||||
dispatch(
|
||||
|
Loading…
Reference in New Issue
Block a user