-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add img2img feature for new model (#34)
- Loading branch information
Showing
17 changed files
with
669 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { createPreviewMedia, resizeImage } from "@/core/utils/upload"; | ||
import useProjectContext from "@/hooks/use-project-context"; | ||
import { | ||
Button, | ||
ButtonGroup, | ||
Center, | ||
Icon, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
Text, | ||
useDisclosure, | ||
} from "@chakra-ui/react"; | ||
import { useS3Upload } from "next-s3-upload"; | ||
import { useRef, useState } from "react"; | ||
import { | ||
FixedCropper, | ||
FixedCropperRef, | ||
ImageRestriction, | ||
} from "react-advanced-cropper"; | ||
import { useDropzone } from "react-dropzone"; | ||
import { | ||
BsCloud, | ||
BsCloudArrowDown, | ||
BsCloudArrowUp, | ||
BsImage, | ||
} from "react-icons/bs"; | ||
import { FilePreview } from "../dashboard/Uploader"; | ||
|
||
import "react-advanced-cropper/dist/style.css"; | ||
import "react-advanced-cropper/dist/themes/compact.css"; | ||
|
||
const PromptImage = () => { | ||
const [isLoading, setLoading] = useState(false); | ||
const { isOpen, onOpen, onClose } = useDisclosure({ | ||
onClose: () => { | ||
setImagePreview(undefined); | ||
}, | ||
}); | ||
|
||
const { setPromptImageUrl } = useProjectContext(); | ||
const cropperRef = useRef<FixedCropperRef>(null); | ||
|
||
const [imagePreview, setImagePreview] = useState<FilePreview>(); | ||
|
||
const { uploadToS3 } = useS3Upload(); | ||
|
||
const { getRootProps, getInputProps } = useDropzone({ | ||
accept: { | ||
"image/png": [".png"], | ||
"image/jpeg": [".jpeg", ".jpg"], | ||
}, | ||
maxSize: 10000000, | ||
multiple: false, | ||
onDrop: (acceptedFiles) => { | ||
if (acceptedFiles?.[0]) { | ||
setImagePreview(createPreviewMedia(acceptedFiles[0])); | ||
} | ||
}, | ||
}); | ||
|
||
const handleSubmit = async () => { | ||
if (!cropperRef.current) { | ||
return; | ||
} | ||
|
||
setLoading(true); | ||
const canvas = cropperRef.current.getCanvas({ | ||
height: 512, | ||
width: 512, | ||
})!; | ||
|
||
canvas.toBlob(async (blob) => { | ||
const croppedImage = createPreviewMedia(blob!); | ||
const file = await resizeImage(croppedImage as File); | ||
|
||
const { url } = await uploadToS3(file); | ||
setLoading(false); | ||
|
||
setPromptImageUrl(url); | ||
onClose(); | ||
}, "image/jpeg"); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button | ||
rightIcon={<BsImage />} | ||
variant="outline" | ||
size="sm" | ||
onClick={onOpen} | ||
> | ||
Use image as model | ||
</Button> | ||
|
||
<Modal isOpen={isOpen} onClose={onClose}> | ||
<ModalOverlay backdropFilter="auto" backdropBlur="4px" /> | ||
<ModalContent | ||
as="form" | ||
onSubmit={async (e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
handleSubmit(); | ||
}} | ||
> | ||
<ModalHeader>Use image as model</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody> | ||
{imagePreview ? ( | ||
<FixedCropper | ||
ref={cropperRef} | ||
src={imagePreview && imagePreview.preview} | ||
stencilSize={{ width: 512, height: 512 }} | ||
imageRestriction={ImageRestriction.stencil} | ||
/> | ||
) : ( | ||
<Center | ||
_hover={{ | ||
bg: "whiteAlpha.800", | ||
}} | ||
fontSize="sm" | ||
transition="all 0.2s" | ||
backgroundColor="whiteAlpha.500" | ||
cursor="pointer" | ||
borderRadius="xl" | ||
border="1px dashed gray" | ||
p={10} | ||
flexDirection="column" | ||
{...getRootProps({ className: "dropzone" })} | ||
> | ||
<input {...getInputProps()} /> | ||
<Icon as={BsCloudArrowUp} fontSize="3xl" /> | ||
<Text>Upload reference image</Text> | ||
</Center> | ||
)} | ||
</ModalBody> | ||
|
||
<ModalFooter> | ||
<ButtonGroup> | ||
<Button onClick={onClose} variant="ghost"> | ||
Cancel | ||
</Button> | ||
<Button | ||
disabled={!imagePreview} | ||
isLoading={isLoading} | ||
variant="brand" | ||
type="submit" | ||
> | ||
Use image | ||
</Button> | ||
</ButtonGroup> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
export default PromptImage; |
Oops, something went wrong.
926fc81
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
photoshot – ./
photoshot-shinework.vercel.app
photoshot.vercel.app
photoshot-git-main-shinework.vercel.app
www.photoshot.app
photoshot.app