Skip to content

Commit

Permalink
Resize favicons before storing them
Browse files Browse the repository at this point in the history
Some websites are using images of O(10kB) when not )O(100kB) for their
favicons. As miniflux only displays them with a 16x16 resolution, let's do our
best to resize them before storing them in the database. This should make
miniflux consume less bandwidth when serving pages, for the joy of mobile users
on a small data plan.
  • Loading branch information
jvoisin committed Dec 9, 2024
1 parent d5cfcf8 commit b87368c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/prometheus/client_golang v1.20.5
github.com/tdewolff/minify/v2 v2.21.2
golang.org/x/crypto v0.30.0
golang.org/x/image v0.23.0
golang.org/x/net v0.32.0
golang.org/x/oauth2 v0.24.0
golang.org/x/term v0.27.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=
golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
42 changes: 42 additions & 0 deletions internal/reader/icon/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
package icon // import "miniflux.app/v2/internal/reader/icon"

import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"log/slog"
"net/url"
Expand All @@ -19,6 +25,7 @@ import (
"miniflux.app/v2/internal/urllib"

"github.com/PuerkitoBio/goquery"
"golang.org/x/image/draw"
"golang.org/x/net/html/charset"
)

Expand Down Expand Up @@ -180,9 +187,44 @@ func (f *IconFinder) DownloadIcon(iconURL string) (*model.Icon, error) {
Content: responseBody,
}

icon = resizeIcon(icon)

return icon, nil
}

func resizeIcon(icon *model.Icon) *model.Icon {
var src image.Image
var err error

r := bytes.NewReader(icon.Content)
switch icon.MimeType {
case "image/jpeg":
src, err = jpeg.Decode(r)
case "image/png":
src, err = png.Decode(r)
case "image/gif":
src, err = gif.Decode(r)
default:
return icon
}
if err != nil {
return icon
}

dst := image.NewRGBA(image.Rect(0, 0, 16, 16))
draw.BiLinear.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)

var b bytes.Buffer
w := bufio.NewWriter(&b)
if err = png.Encode(w, dst); err == nil {
icon.Content = b.Bytes()
icon.MimeType = "image/png"
}

return icon

}

Check failure on line 226 in internal/reader/icon/finder.go

View workflow job for this annotation

GitHub Actions / Golang Linters

unnecessary trailing newline (whitespace)

func findIconURLsFromHTMLDocument(body io.Reader, contentType string) ([]string, error) {
queries := []string{
"link[rel='icon' i]",
Expand Down

0 comments on commit b87368c

Please sign in to comment.