positioning image instead of creating view size image

This commit is contained in:
Celogeek 2023-04-22 12:14:49 +02:00
parent 88eea07747
commit 418d51662c
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
6 changed files with 36 additions and 83 deletions

View File

@ -97,9 +97,10 @@ func (e *ePub) writeImage(wz *epubZip, img *Image) error {
err := wz.WriteFile(
fmt.Sprintf("OEBPS/%s", img.TextPath()),
e.render(textTmpl, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
"ImagePath": img.ImgPath(),
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
"ImagePath": img.ImgPath(),
"ImageStyle": img.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga),
}),
)

View File

@ -20,7 +20,6 @@ func NewGift(options *ImageOptions) *gift.GIFT {
}
g.Add(
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
filters.Position(options.ViewWidth, options.ViewHeight, filters.PositionCenter),
filters.Pixel(),
)
return g
@ -37,7 +36,7 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT {
filters.CropSplitDoublePage(!options.Manga),
)
for i, g := range gifts {
for _, g := range gifts {
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))
}
@ -45,14 +44,8 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT {
g.Add(gift.Brightness(float32(options.Brightness)))
}
position := filters.PositionLeft
if (i == 1) == options.Manga {
position = filters.PositionRight
}
g.Add(
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
filters.Position(options.ViewWidth, options.ViewHeight, position),
)
}

View File

@ -53,6 +53,36 @@ func (i *Image) ImgPath() string {
return fmt.Sprintf("Images/%d_p%d.jpg", i.Id, i.Part)
}
func (i *Image) ImgStyle(viewWidth, viewHeight int, manga bool) string {
marginW, marginH := float64(viewWidth-i.Width)/2, float64(viewHeight-i.Height)/2
left, top := marginW*100/float64(viewWidth), marginH*100/float64(viewHeight)
var align string
switch i.Part {
case 0:
align = fmt.Sprintf("left:%.2f%%", left)
case 1:
if manga {
align = "left:0"
} else {
align = "right:0"
}
case 2:
if manga {
align = "right:0"
} else {
align = "left:0"
}
}
return fmt.Sprintf(
"width:%dpx; height:%dpx; top:%.2f%%; %s;",
i.Width,
i.Height,
top,
align,
)
}
func (i *Image) SpacePath() string {
return fmt.Sprintf("Text/%d_sp.xhtml", i.Id)
}

View File

@ -1,67 +0,0 @@
package filters
import (
"image"
"image/draw"
"github.com/disintegration/gift"
)
const (
PositionCenter = iota
PositionLeft
PositionRight
)
func Position(viewWidth, viewHeight int, align int) gift.Filter {
return &positionFilter{
viewWidth, viewHeight, align,
}
}
type positionFilter struct {
viewWidth, viewHeight, align int
}
func (p *positionFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
w, h := p.viewWidth, p.viewHeight
srcw, srch := srcBounds.Dx(), srcBounds.Dy()
if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 {
return image.Rect(0, 0, 0, 0)
}
return image.Rect(0, 0, w, h)
}
func (p *positionFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
if dst.Bounds().Dx() == 0 || dst.Bounds().Dy() == 0 {
return
}
draw.Draw(dst, dst.Bounds(), image.White, dst.Bounds().Min, draw.Over)
srcBounds := src.Bounds()
left, top := 0, (dst.Bounds().Dy()-srcBounds.Dy())/2
if p.align == PositionCenter {
left = (dst.Bounds().Dx() - srcBounds.Dx()) / 2
}
if p.align == PositionRight {
left = dst.Bounds().Dx() - srcBounds.Dx()
}
draw.Draw(
dst,
image.Rect(
left,
top,
dst.Bounds().Dx(),
dst.Bounds().Dy(),
),
src,
srcBounds.Min,
draw.Over,
)
}

View File

@ -15,8 +15,4 @@ img {
margin:0;
padding:0;
z-index:0;
top:0;
left:0;
width: {{ .PageWidth }}px;
height: {{ .PageHeight }}px;
}

View File

@ -9,7 +9,7 @@
</head>
<body>
<div>
<img src="../{{ .ImagePath }}" alt="{{ .Title }}"/>
<img src="../{{ .ImagePath }}" alt="{{ .Title }}" style="{{ .ImageStyle }}"/>
</div>
</body>
</html>