Skip to content

Sending an Image response #466

Answered by sunli829
ShayBox asked this question in Q&A
Dec 25, 2022 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

You must be encoding the image to the PNG format

    #[oai(path = "/hex/generate/:color/:height/:width", method = "get")]
    async fn hex(
        &self,
        color: Path<String>,
        height: Path<u32>,
        width: Path<u32>,
    ) -> Result<ImageResponse> {
        let Ok(color) = color.0.parse::<Color>() else {
              return Err(poem::Error::from_status(StatusCode::BAD_REQUEST))
          };
        let rgba = Rgba([color.red, color.green, color.blue, color.alpha]);
        let image = ImageBuffer::from_pixel(width.0, height.0, rgba);

        let mut png_bytes = Vec::new();
        image
            .write_to(&mut std::io::Cursor::new(&mut png_bytes), ImageFormat::Png)

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@ShayBox
Comment options

Comment options

You must be logged in to vote
1 reply
@ShayBox
Comment options

Answer selected by ShayBox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants