diff --git a/kernel/model/assets.go b/kernel/model/assets.go index 15de3f68b..5f6dfdabf 100644 --- a/kernel/model/assets.go +++ b/kernel/model/assets.go @@ -51,6 +51,24 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) +func GetAssetImgSize(assetPath string) (width, height int) { + absPath, err := GetAssetAbsPath(assetPath) + if err != nil { + logging.LogErrorf("get asset [%s] abs path failed: %s", assetPath, err) + return + } + + img, err := imaging.Open(absPath) + if err != nil { + logging.LogErrorf("open asset image [%s] failed: %s", absPath, err) + return + } + + width = img.Bounds().Dx() + height = img.Bounds().Dy() + return +} + func GetAssetPathByHash(hash string) string { assetHash := cache.GetAssetHash(hash) if nil == assetHash { diff --git a/kernel/model/export.go b/kernel/model/export.go index dc53d7cbe..1c8781d32 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -2831,10 +2831,14 @@ func exportTree(tree *parse.Tree, wysiwyg, keepFold, avHiddenCol bool, img.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(a.Content)}) img.AppendChild(&ast.Node{Type: ast.NodeCloseParen}) mdTableCell.AppendChild(img) - height := "height: 128px;" - spanIAL := &ast.Node{Type: ast.NodeKramdownSpanIAL, Tokens: []byte("style=\"" + height + "\"")} - mdTableCell.AppendChild(spanIAL) - img.SetIALAttr("style", height) + img.SetIALAttr("style", "max-height: 128px;") + + width, height := GetAssetImgSize(a.Content) + if height > 128 { + img.SetIALAttr("height", "128px") + newWidth := int(float64(width) * (128.0 / float64(height))) + img.SetIALAttr("width", strconv.Itoa(newWidth)+"px") + } } else if av.AssetTypeFile == a.Type { linkText := strings.TrimSpace(a.Name) if "" == linkText {