Skip to content

Commit

Permalink
Show image size on view page (#25884)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
JakobDev committed Jul 31, 2023
1 parent 983167c commit aba9096
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import (
gocontext "context"
"encoding/base64"
"fmt"
"image"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"

_ "image/gif" // for processing gif images
_ "image/jpeg" // for processing jpeg images
_ "image/png" // for processing png images

activities_model "code.gitea.io/gitea/models/activities"
admin_model "code.gitea.io/gitea/models/admin"
asymkey_model "code.gitea.io/gitea/models/asymkey"
Expand Down Expand Up @@ -44,6 +49,9 @@ import (
issue_service "code.gitea.io/gitea/services/issue"

"github.com/nektos/act/pkg/model"

_ "golang.org/x/image/bmp" // for processing bmp images
_ "golang.org/x/image/webp" // for processing webp images
)

const (
Expand Down Expand Up @@ -578,6 +586,15 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
}
}

if fInfo.st.IsImage() && !fInfo.st.IsSvgImage() {
img, _, err := image.DecodeConfig(bytes.NewReader(buf))
if err == nil {
// There are Image formats go can't decode
// Instead of throwing an error in that case, we show the size only when we can decode
ctx.Data["ImageSize"] = fmt.Sprintf("%dx%dpx", img.Width, img.Height)
}
}

if ctx.Repo.CanEnableEditor(ctx, ctx.Doer) {
if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
ctx.Data["CanDeleteFile"] = false
Expand Down
5 changes: 5 additions & 0 deletions templates/repo/file_info.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@
{{.locale.Tr "repo.executable_file"}}
</div>
{{end}}
{{if .ImageSize}}
<div class="file-info-entry">
{{.ImageSize}}
</div>
{{end}}
</div>

0 comments on commit aba9096

Please sign in to comment.