12 Star 38 Fork 17

Gitee 极速下载/unioffice

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
_examples
chart
cmd
color
common
testdata
appproperties.go
appproperties_test.go
contenttypes.go
contenttypes_test.go
coreproperties.go
coreproperties_test.go
doc.go
docbase.go
image.go
relationship.go
relationships.go
relationships_test.go
theme.go
theme_test.go
document
drawing
measurement
presentation
schema
spreadsheet
testdata
testhelper
vmldrawing
zippkg
.gitignore
.travis.yml
CLA.md
LICENSE
README.md
build-examples.sh
codecov.yml
creator.go
creator_test.go
doc.go
example_test.go
filenames.go
optional.go
optional_test.go
schemas.go
test-coverage.sh
update-godoc.sh
xml.go
xml_test.go
xsdany.go
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/unidoc/unioffice
克隆/下载
image.go 3.02 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package common
import (
"fmt"
"image"
"os"
"baliance.com/gooxml/measurement"
// Add image format support
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"baliance.com/gooxml"
)
// Image is a container for image information. It's used as we need format and
// and size information to use images.
type Image struct {
Size image.Point
Format string
Path string
}
// ImageRef is a reference to an image within a document.
type ImageRef struct {
d *DocBase
rels Relationships
img Image
}
// MakeImageRef constructs an image reference which is a reference to a
// particular image file inside a document. The same image can be used multiple
// times in a document by re-useng the ImageRef.
func MakeImageRef(img Image, d *DocBase, rels Relationships) ImageRef {
return ImageRef{img: img, d: d, rels: rels}
}
// RelID returns the relationship ID.
func (i ImageRef) RelID() string {
for imgIdx, ir := range i.d.Images {
if ir.img == i.img {
imgID := i.rels.FindRIDForN(imgIdx, gooxml.ImageType)
return imgID
}
}
return ""
}
// Format returns the format of the underlying image
func (i ImageRef) Format() string {
return i.img.Format
}
// Path returns the path to an image file
func (i ImageRef) Path() string {
return i.img.Path
}
// Path returns the path to an image file
func (i ImageRef) Size() image.Point {
return i.img.Size
}
// RelativeHeight returns the relative height of an image given a fixed width.
// This is used when setting image to a fixed width to calculate the height
// required to keep the same image aspect ratio.
func (i ImageRef) RelativeHeight(w measurement.Distance) measurement.Distance {
hScale := float64(i.Size().Y) / float64(i.Size().X)
return w * measurement.Distance(hScale)
}
// RelativeWidth returns the relative width of an image given a fixed height.
// This is used when setting image to a fixed height to calculate the width
// required to keep the same image aspect ratio.
func (i ImageRef) RelativeWidth(h measurement.Distance) measurement.Distance {
wScale := float64(i.Size().X) / float64(i.Size().Y)
return h * measurement.Distance(wScale)
}
// ImageFromFile reads an image from a file on disk. It doesn't keep the image
// in memory and only reads it to determine the format and size. You can also
// construct an Image directly if the file and size are known.
func ImageFromFile(path string) (Image, error) {
f, err := os.Open(path)
r := Image{}
if err != nil {
return r, fmt.Errorf("error reading image: %s", err)
}
defer f.Close()
imgDec, ifmt, err := image.Decode(f)
if err != nil {
return r, fmt.Errorf("unable to parse image: %s", err)
}
r.Path = path
r.Format = ifmt
r.Size = imgDec.Bounds().Size()
return r, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/unioffice.git
git@gitee.com:mirrors/unioffice.git
mirrors
unioffice
unioffice
v0.2.0

搜索帮助