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

透明な画像を上げると背景が黒くなる問題の修正 #532

Merged
merged 2 commits into from
May 16, 2023
Merged
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
19 changes: 15 additions & 4 deletions router/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"net/http"
"strconv"

"github.com/disintegration/imaging"
"github.com/labstack/echo"
"github.com/traPtitech/booQ/model"
"github.com/traPtitech/booQ/storage"
"net/http"
"strconv"
)

// アップロードを許可するMIMEタイプ
Expand Down Expand Up @@ -46,9 +50,16 @@ func PostFile(c echo.Context) error {
// 不正な画像
return c.JSON(http.StatusBadRequest, errors.New("不正なファイルです"))
}
// 背景を透明にする
newImg := image.NewRGBA(orig.Bounds())
draw.Draw(newImg, newImg.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)
draw.Draw(newImg, newImg.Bounds(), orig, orig.Bounds().Min, draw.Over)
Comment on lines +55 to +56
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

draw.DrawMask(newImg, newImg.Bounds(), orig, image.Point{}, &draw.Op{draw.Src}, nil, &draw.SrcOver{})

とかでできそうな気配あるけどどうですか:eyes:(試してない)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&draw.Op{draw.Src}&draw.SrcOver{}がうまく行かず

b := &bytes.Buffer{}
_ = imaging.Encode(b, imaging.Fit(orig, 360, 480, imaging.Linear), imaging.JPEG, imaging.JPEGQuality(85))

err = imaging.Encode(b, imaging.Fit(newImg, 360, 480, imaging.Linear), imaging.JPEG, imaging.JPEGQuality(85))
if err != nil {
return c.NoContent(http.StatusInternalServerError)
}

// 保存
f, err := model.CreateFile(user.ID, b, "jpg")
if err != nil {
Expand Down