From bfa8c2a9d76834c4ae434210ed03caf8ea07c5cb Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sun, 27 Jul 2014 23:07:15 +0200 Subject: Background image will now be resized, if very large --- README.markdown | 10 +++++++- earthporn.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/README.markdown b/README.markdown index 74f1feb..065645d 100644 --- a/README.markdown +++ b/README.markdown @@ -9,7 +9,7 @@ A simple start page with a background image from [/r/EarthPorn](http://www.reddi ## Configuration -The startpage configuration is located in the file ~/.startpagerc. It is a list of commands. A command has a name and can optionally have parameters separated by spaces or tabs. A backspace `\\` will interpret the next charcter literally (can be used to escape whitespace, linebreaks and backspaces). Commands are separated by newlines. +The startpage configuration is located in the file ~/.startpagerc. It is a list of commands. A command has a name and can optionally have parameters separated by spaces or tabs. A backspace `\` will interpret the next charcter literally (can be used to escape whitespace, linebreaks and backspaces). Commands are separated by newlines. These commands are implemented: @@ -38,6 +38,14 @@ Example: set-earthporn-savepath /home/foobar/Pictures/earthporn +### `set-maxdim` + +Sets the maximum width/height of an EarthPorn image. If the image is larger, it will be resized for the background (it will be saved in it's original size). + +Example: + + set-maxdim 4000 + ## Running If `$GOPATH/bin` is in your `$PATH`, you can run startpage with the command `startpage`. By default, startpage listens on port 25145. You can change that with a command line switch: `startpage -laddr :` \ No newline at end of file diff --git a/earthporn.go b/earthporn.go index 8267bc8..ad7576c 100644 --- a/earthporn.go +++ b/earthporn.go @@ -5,12 +5,18 @@ import ( "encoding/json" "errors" "fmt" + "github.com/nfnt/resize" + "image" + _ "image/gif" + "image/jpeg" + _ "image/png" "io" "log" "mime" "net/http" "os" "path" + "strconv" "strings" ) @@ -23,13 +29,13 @@ type redditList struct { } type EarthPorn struct { - Title string `json:"title"` - URL string `json:"url,omitempty"` - Permalink string `json:"permalink"` - Domain string `json:"domain"` - Saved bool - data []byte - mediatype string + Title string `json:"title"` + URL string `json:"url,omitempty"` + Permalink string `json:"permalink"` + Domain string `json:"domain"` + Saved bool + data, origdata []byte + mediatype string } const earthPornURL = "http://www.reddit.com/r/EarthPorn.json" @@ -99,10 +105,62 @@ func (p *EarthPorn) fetch() bool { } p.mediatype = t - p.data = buf.Bytes() + p.origdata = buf.Bytes() + p.data = p.origdata + p.resize() return true } +var maxdim = 2500 + +func setMaxdimCmd(params []string) error { + if len(params) != 1 { + return errors.New("set-maxdim needs one parameter, which must be a positive number") + } + + newmaxdim, err := strconv.ParseUint(params[0], 10, 32) + if err != nil { + return fmt.Errorf("1st parameter of set-maxdim could not be parsed as a number: %s", err) + } + + return nil +} + +// resize resizes image, if it's very large +func (p *EarthPorn) resize() { + im, _, err := image.Decode(bytes.NewReader(p.origdata)) + if err != nil { + log.Printf("Failed decoding in resize(): %s", err) + return + } + + size := im.Bounds().Size() + if !(size.X > maxdim || size.Y > maxdim) { + return + } + + var w, h int + if size.X > size.Y { + h = maxdim * (size.Y / size.X) + w = maxdim + } else { + w = maxdim * (size.X / size.Y) + h = maxdim + } + + im = resize.Resize(uint(w), uint(h), im, resize.Bicubic) + + buf := new(bytes.Buffer) + + if err := jpeg.Encode(buf, im, &jpeg.Options{Quality: 90}); err != nil { + log.Printf("Failed encoding in resize(): %s", err) + return + } + + p.data = buf.Bytes() + p.mediatype = "image/jpeg" +} + var extensions = map[string]string{ "image/png": "png", "image/jpeg": "jpg", @@ -147,7 +205,7 @@ func (p *EarthPorn) save() error { } defer f.Close() - if _, err := f.Write(p.data); err != nil { + if _, err := f.Write(p.origdata); err != nil { return fmt.Errorf("Could not save earthporn: %s", err) } -- cgit v1.2.3-70-g09d2