mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-09-18 17:48:00 +08:00
完善功能
This commit is contained in:
49
graphics/copy.go
Normal file
49
graphics/copy.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package graphics
|
||||
|
||||
import (
|
||||
"image"
|
||||
"os"
|
||||
"errors"
|
||||
|
||||
"github.com/nfnt/resize"
|
||||
)
|
||||
|
||||
func ImageCopy(src image.Image,x, y ,w, h int) (image.Image,error) {
|
||||
|
||||
var subImg image.Image
|
||||
|
||||
if rgbImg,ok := src.(*image.YCbCr); ok {
|
||||
subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
|
||||
}else if rgbImg,ok := src.(*image.RGBA); ok {
|
||||
subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
|
||||
}else if rgbImg,ok := src.(*image.NRGBA); ok {
|
||||
subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
|
||||
} else {
|
||||
|
||||
return subImg,errors.New("图片解码失败")
|
||||
}
|
||||
|
||||
return subImg,nil
|
||||
}
|
||||
|
||||
func ImageCopyFromFile(p string,x, y ,w, h int) (image.Image,error) {
|
||||
var src image.Image
|
||||
|
||||
file, err := os.Open(p)
|
||||
if err != nil {
|
||||
return src, err
|
||||
}
|
||||
defer file.Close()
|
||||
src, _, err = image.Decode(file)
|
||||
|
||||
return ImageCopy(src, x, y, w, h)
|
||||
}
|
||||
|
||||
func ImageResize(src image.Image,w,h int) (image.Image) {
|
||||
return resize.Resize(uint(w), uint(h), src, resize.Lanczos3)
|
||||
}
|
||||
func ImageResizeSaveFile(src image.Image,width,height int,p string) error {
|
||||
dst := resize.Resize(uint(width), uint(height), src, resize.Lanczos3)
|
||||
|
||||
return SaveImage(p,dst)
|
||||
}
|
32
graphics/file.go
Normal file
32
graphics/file.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package graphics
|
||||
|
||||
import (
|
||||
"image"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"image/gif"
|
||||
)
|
||||
// 将图片保存到指定的路径
|
||||
func SaveImage(p string, src image.Image) error {
|
||||
|
||||
f, err := os.OpenFile( p, os.O_SYNC | os.O_RDWR | os.O_CREATE, 0666)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
ext := filepath.Ext(p)
|
||||
|
||||
if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") {
|
||||
|
||||
err = jpeg.Encode(f, src, &jpeg.Options{Quality : 100 })
|
||||
} else if strings.EqualFold(ext, ".png") {
|
||||
err = png.Encode(f, src)
|
||||
} else if strings.EqualFold(ext, ".gif") {
|
||||
err = gif.Encode(f, src, &gif.Options{NumColors : 256})
|
||||
}
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user