Skip to content

Commit

Permalink
修正封面
Browse files Browse the repository at this point in the history
  • Loading branch information
TruthHun committed Apr 21, 2024
1 parent 44c472c commit 6e62aff
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
24 changes: 24 additions & 0 deletions cmd/fixCover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"moredoc/service"

"github.com/spf13/cobra"
)

// fixCoverCmd represents the fixCover command
var fixCoverCmd = &cobra.Command{
Use: "fixCover",
Short: "修正封面大小",
Long: `修正已有图片的封面大小,使其符合要求,特别是PPT类的文档。`,
Run: func(cmd *cobra.Command, args []string) {
service.FixCover(cfg, logger)
},
}

func init() {
rootCmd.AddCommand(fixCoverCmd)
}
2 changes: 1 addition & 1 deletion model/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func (m *DBModel) ConvertDocument() (err error) {
coverBig := baseDir + "/cover.big.png"
util.CopyFile(pages[0].PagePath, coverBig)
util.CopyFile(pages[0].PagePath, cover)
util.CropImage(cover, DocumentCoverWidth, DocumentCoverHeight)
util.CropImage(cover, DocumentCoverWidth, DocumentCoverHeight, true)
document.Width, document.Height, _ = util.GetImageSize(coverBig) // 页面宽高
}

Expand Down
48 changes: 48 additions & 0 deletions service/fixCover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package service

import (
"moredoc/conf"
"moredoc/model"
"moredoc/util"
"os"
"path/filepath"
"strings"

"go.uber.org/zap"
"gorm.io/gorm"
)

func FixCover(cfg *conf.Config, logger *zap.Logger) {
lg := logger.Named("FixCover")
lg.Info("start...")
dbModel, err := model.NewDBModel(&cfg.Database, logger)
if err != nil {
lg.Fatal("NewDBModel", zap.Error(err))
return
}

page := 1
size := 100
for {
var attachments []model.Attachment
err := dbModel.DB().Where("type = ?", model.AttachmentTypeDocument).Select("hash", "path").Group("hash").Offset((page - 1) * size).Limit(size).Order("id asc").Find(&attachments).Error
if err != nil && err != gorm.ErrRecordNotFound {
lg.Error("查询附件失败", zap.Error(err))
break
}
if len(attachments) == 0 {
break
}

for _, attachment := range attachments {
coverBig := strings.TrimLeft(strings.TrimSuffix(attachment.Path, filepath.Ext(attachment.Path)), "./") + "/cover.big.png"
cover := strings.TrimLeft(strings.TrimSuffix(attachment.Path, filepath.Ext(attachment.Path)), "./") + "/cover.png"
os.Remove(cover)
util.CopyFile(coverBig, cover)
util.CropImage(cover, model.DocumentCoverWidth, model.DocumentCoverHeight, true)
lg.Info("fixed", zap.String("cover", cover))
}
page++
}
lg.Info("done!")
}
19 changes: 17 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"image"
"image/color"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -81,16 +82,30 @@ func GetGRPCUserAgent(ctx context.Context) (userAgent string) {
}

// 图片缩放居中裁剪
func CropImage(file string, width, height int) (err error) {
func CropImage(file string, width, height int, adjust ...bool) (err error) {
var img image.Image
img, err = imaging.Open(file)
if err != nil {
return
}

if img.Bounds().Max.X == width && img.Bounds().Max.Y == height {
return
}

ext := strings.ToLower(filepath.Ext(file))
switch ext {
case ".jpeg", ".jpg", ".png", ".gif":
img = imaging.Fill(img, width, height, imaging.Center, imaging.CatmullRom)
if len(adjust) > 0 && adjust[0] { // 按指定宽高裁剪图片,但必须包括图片的所有内容。如果图片尺寸小于指定尺寸,则填充空白
img = imaging.Fit(img, width, height, imaging.CatmullRom)
if img.Bounds().Max.X < width { // 水平居中
img = imaging.Paste(imaging.New(width, height, color.White), img, image.Pt((width-img.Bounds().Max.X)/2, 0))
} else if img.Bounds().Max.Y < height { // 垂直居中
img = imaging.Paste(imaging.New(width, height, color.White), img, image.Pt(0, (height-img.Bounds().Max.Y)/2))
}
} else {
img = imaging.Fill(img, width, height, imaging.Center, imaging.CatmullRom)
}
default:
err = errors.New("unsupported image format")
return
Expand Down

0 comments on commit 6e62aff

Please sign in to comment.