web-based iasip title card generator https://iasip.szy.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.6KB

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "image/jpeg"
  6. "net/http"
  7. "os"
  8. "strconv"
  9. "github.com/gin-gonic/gin"
  10. "github.com/bclindner/iasipgenerator/iasipgen"
  11. )
  12. type bindText struct {
  13. Text string `form:"text" binding:"required"`
  14. Quote string `form:"quote"`
  15. }
  16. func setupRouter() *gin.Engine {
  17. r := gin.Default()
  18. r.Static("/", "./public")
  19. r.POST("/make", func(c *gin.Context) {
  20. var formData bindText
  21. if err := c.ShouldBind(&formData); err != nil {
  22. c.String(http.StatusBadRequest, "error: no text given")
  23. return
  24. }
  25. text := formData.Text
  26. quote := formData.Quote
  27. if quote == "true" {
  28. // Adds curly quotes with a U+200A HAIR SPACE between them and the text
  29. text = "\u201c\u200a" + text + "\u200a\u201d"
  30. }
  31. img, err := iasipgen.Generate(text)
  32. if err != nil {
  33. c.String(http.StatusInternalServerError, fmt.Sprintf("error generating image: %v", err))
  34. return
  35. }
  36. buffer := new(bytes.Buffer)
  37. if err := jpeg.Encode(buffer, img, nil); err != nil {
  38. c.String(http.StatusInternalServerError, fmt.Sprintf("error encoding image: %v", err))
  39. return
  40. }
  41. w := c.Writer
  42. header := w.Header()
  43. header.Set("Content-Type", "image/jpeg")
  44. header.Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))
  45. header.Set("Content-Disposition", `attachment; filename="titlecard.jpg"`)
  46. w.WriteHeader(http.StatusOK)
  47. w.Write(buffer.Bytes())
  48. w.(http.Flusher).Flush()
  49. })
  50. return r
  51. }
  52. func main() {
  53. err := iasipgen.LoadFont("Textile_patched.ttf")
  54. if err != nil {
  55. fmt.Println("Couldn't load font: ", err)
  56. os.Exit(1)
  57. }
  58. r := setupRouter()
  59. r.Run(":42069") // nice
  60. }