Skip to content

Commit aba9096

Browse files
authored
Show image size on view page (#25884)
This simply shows the Image size on the view page. This is useful, if you search a image with a specific size. ![grafik](https://github.com/go-gitea/gitea/assets/15185051/9868e361-1c2e-447f-b824-70aa28bafcbc)
1 parent 983167c commit aba9096

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

routers/web/repo/view.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import (
99
gocontext "context"
1010
"encoding/base64"
1111
"fmt"
12+
"image"
1213
"io"
1314
"net/http"
1415
"net/url"
1516
"path"
1617
"strings"
1718
"time"
1819

20+
_ "image/gif" // for processing gif images
21+
_ "image/jpeg" // for processing jpeg images
22+
_ "image/png" // for processing png images
23+
1924
activities_model "code.gitea.io/gitea/models/activities"
2025
admin_model "code.gitea.io/gitea/models/admin"
2126
asymkey_model "code.gitea.io/gitea/models/asymkey"
@@ -44,6 +49,9 @@ import (
4449
issue_service "code.gitea.io/gitea/services/issue"
4550

4651
"github.com/nektos/act/pkg/model"
52+
53+
_ "golang.org/x/image/bmp" // for processing bmp images
54+
_ "golang.org/x/image/webp" // for processing webp images
4755
)
4856

4957
const (
@@ -578,6 +586,15 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
578586
}
579587
}
580588

589+
if fInfo.st.IsImage() && !fInfo.st.IsSvgImage() {
590+
img, _, err := image.DecodeConfig(bytes.NewReader(buf))
591+
if err == nil {
592+
// There are Image formats go can't decode
593+
// Instead of throwing an error in that case, we show the size only when we can decode
594+
ctx.Data["ImageSize"] = fmt.Sprintf("%dx%dpx", img.Width, img.Height)
595+
}
596+
}
597+
581598
if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) {
582599
if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
583600
ctx.Data["CanDeleteFile"] = false

templates/repo/file_info.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@
3030
{{.locale.Tr "repo.executable_file"}}
3131
</div>
3232
{{end}}
33+
{{if .ImageSize}}
34+
<div class="file-info-entry">
35+
{{.ImageSize}}
36+
</div>
37+
{{end}}
3338
</div>

0 commit comments

Comments
 (0)