resize images to 100x100 for style preset images

This commit is contained in:
Mary Hipp 2024-08-08 12:56:55 -04:00
parent 9a4d075074
commit 442fc02429
3 changed files with 20 additions and 8 deletions

View File

@ -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) {

View File

@ -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))

View File

@ -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(