Skip to content

Commit

Permalink
Fix #26: mangarock images are cut in the final part using PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
Girbons committed Jun 1, 2019
1 parent 6f1fb0e commit 58aff13
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 38 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ windows-build: # Creates Windows executable
linux-build: # Creates Linux executable
@GOOS=linux GOARCH=amd64 go build -ldflags=${LDFLAGS} -o build/comics-downloader ./cmd/downloader

raspbian-build-arm: # Creates Raspbian executable
@GOOS=linux GOARCH=arm go build -ldflags=${LDFLAGS} -o build/comics-downloader-raspbian-arm ./cmd/downloader

raspbian-build-arm64: # Creates Raspbian executable
@GOOS=linux GOARCH=arm64 go build -ldflags=${LDFLAGS} -o build/comics-downloader-raspbian-arm64 ./cmd/downloader

osx-gui-build: # Creates OSX Gui executable
@GOOS=darwin GOARCH=amd64 go build -ldflags=${LDFLAGS} -o build/comics-downloader-gui-osx ./cmd/gui

Expand Down
5 changes: 0 additions & 5 deletions cmd/app/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ func Run(link, format, country string, all, bindLogsToChannel bool) {
sendToChannel(bindLogsToChannel, msg)
}

// TODO: This doesn't seem necessary
if !strings.HasSuffix(link, ",") {
link = link + ","
}

for _, u := range strings.Split(link, ",") {
if u != "" {
// check if the link is supported
Expand Down
33 changes: 16 additions & 17 deletions pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func (comic *Comic) retrieveImageFromResponse(response *http.Response) (io.Reade
}

imgData := new(bytes.Buffer)
if err := util.ConvertTo8BitPNG(img, imgData); err != nil {
if err := util.ConvertToJPG(img, imgData); err != nil {
return content, tp, err
}

content = imgData
tp = "png"
tp = "jpg"
default:
content = response.Body
tp = util.ImageType(response.Header["Content-Type"][0])
Expand Down Expand Up @@ -200,23 +200,22 @@ func (comic *Comic) makePDF() error {
for _, link := range comic.Links {
if link != "" {
rsp, err := http.Get(link)
if err == nil {
defer rsp.Body.Close()
// add a new PDF page
pdf.AddPage()
content, tp, err := comic.retrieveImageFromResponse(rsp)
if err != nil {
return err
}
// The image is directly added to the pdf without being saved to the disk
imageOptions := gofpdf.ImageOptions{ImageType: tp, ReadDpi: false, AllowNegativePosition: true}
pdf.RegisterImageOptionsReader(link, imageOptions, content)
// set the image position on the pdf page
pdf.Image(link, 0, 0, 210, 0, false, tp, 0, "")
// increase the progressbar
} else {
if err != nil {
return err
}

defer rsp.Body.Close()
// add a new PDF page
pdf.AddPage()
content, tp, err := comic.retrieveImageFromResponse(rsp)
if err != nil {
return err
}
imageOptions := gofpdf.ImageOptions{ImageType: tp, ReadDpi: true, AllowNegativePosition: false}
pdf.RegisterImageOptionsReader(link, imageOptions, content)
// set the image position on the pdf page
pdf.Image(link, 0, 0, 210, 297, false, tp, 0, "")
// increase the progressbar
}
if barErr := bar.Add(1); barErr != nil {
log.Error(barErr)
Expand Down
19 changes: 4 additions & 15 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"bytes"
"fmt"
"image"
"image/color"
"image/png"
"image/jpeg"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -55,19 +54,9 @@ func IsValueInSlice(valueToCheck string, values []string) bool {
return false
}

// ConvertTo8BitPNG converts an image of any type to a PNG with 8-bit color depth
func ConvertTo8BitPNG(img image.Image, imgData *bytes.Buffer) error {
b := img.Bounds()
imgSet := image.NewRGBA(b)
// Converts each pixel to a 32-bit RGBA pixel
for y := 0; y < b.Max.Y; y++ {
for x := 0; x < b.Max.X; x++ {
newPixel := color.RGBAModel.Convert(img.At(x, y))
imgSet.Set(x, y, newPixel)
}
}

err := png.Encode(imgData, imgSet)
// ConvertToJPG converts an image to jpeg
func ConvertToJPG(img image.Image, imgData *bytes.Buffer) error {
err := jpeg.Encode(imgData, img, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestConvertImage(t *testing.T) {

img, _ := png.Decode(resp.Body)
imgData := new(bytes.Buffer)
err = ConvertTo8BitPNG(img, imgData)
err = ConvertToJPG(img, imgData)

assert.Nil(t, err)
}
Expand Down

0 comments on commit 58aff13

Please sign in to comment.