Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download Image function added #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const Output: React.FC<Props> = ({ children }) => {
};

export const Pre = styled("pre", {
display: "flex",
// display: "flex",
justifyContent: "center",
alignItems: "center",
textAlign: "center",
Expand Down
45 changes: 44 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ export default function Home() {
return response
}

const downloadImageJpg = () => {
if (!output) return;
const a = document.createElement("a");
a.href = output;
a.download = `${output}.jpg`;
a.click();
};

const downloadImagePng = () => {
if (!output) return;
const a = document.createElement("a");
a.href = output;
a.download = `${output}.png`;
a.click();
};

const Button = styled("button", {
all: "unset",
boxSizing: "border-box",
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
borderRadius: 4,
padding: "0 15px",
fontSize: 15,
lineHeight: 1,
fontWeight: 500,
height: 35,
width: "100%",
backgroundColor: "$purple500",
color: "white",
});

return (
<Box css={{ paddingY: '$6' }}>
<Head>
Expand All @@ -96,7 +129,17 @@ export default function Home() {
isGenerating={isGenerating}
/>
<Output>
{output && <img src={output} alt="Generated Image" />}
{
output && (<>
<img src={output} alt="Generated Image" />
<Button css={{ marginTop: 10 }} onClick={downloadImageJpg}>
Download Image in JPG
</Button>
<Button css={{ marginTop: 10 }} onClick={downloadImagePng}>
Download Image in PNG
</Button>
</>)
}
</Output>
</Container>
</Box>
Expand Down