imageKit

package
v3.26.124 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Decode 解码图片(部分特殊格式不支持; path => image.Image).
	/*
	   @param r 类型可以是: *os.File(用完记得调用Close())
	   @return 第1个: image.Image实例
	   		第2个: 表示图像的格式名称,例如 "png"、"jpeg" 等(不带"." && 转为小写)
	   		第3个: error(可能为nil)
	*/
	Decode func(r io.Reader) (img image.Image, format string, err error) = image.Decode
)

Functions

func Base64ToFile

func Base64ToFile(base64Str, outputPath string) error

Base64ToFile 将base64字符串转换为图片文件 参数:

  • base64Str: base64编码的图片字符串,可能包含"data:"前缀
  • outputPath: 输出文件路径

返回:

  • error: 错误信息,成功则返回nil

func Clip

func Clip(img image.Image, x, y, width, height int) (image.Image, error)

Clip 裁剪图片.

@param img				原始图片实例(不能为nil!)
@param x, y				裁剪区域左上角坐标
@param width, height	裁剪区域的宽度和高度

func ClipWithPath added in v3.26.103

func ClipWithPath(srcPath, dstPath string, x, y, width, height int) error

ClipWithPath 裁剪图片.

func DecodeFromBase64

func DecodeFromBase64(base64 []byte) ([]byte, error)

DecodeFromBase64 图片的base64数据 => 图片的数据(可以直接存储到硬盘上)

@param base64 带不带前缀都无所谓(e.g. "data:image/png;base64,"、"data:image/jpeg;base64,"、"data:image/gif;base64,")

func DecodeFromPath

func DecodeFromPath(path string) (img image.Image, format string, err error)

DecodeFromPath 解码图片.

@return 第2个: 图片的格式名称,例如 "png"、"jpeg" 等(不带"." && 转为小写)

func DecodeToImageFile

func DecodeToImageFile(base64 []byte, dest string, perm os.FileMode) error

DecodeToImageFile

@param target 要生成的图片的路径

func DecodeWithBytes

func DecodeWithBytes(imgData []byte) (img image.Image, format string, err error)

DecodeWithBytes []byte => image.Image

func Encode

func Encode(file io.Writer, img image.Image, ext string) (err error)

Encode image.Image => 文件

@param ext 文件扩展名(带'.'且小写字母化) e.g. ".png"

func EncodeToBase64String

func EncodeToBase64String(data []byte) (string, error)

func EncodeToPath

func EncodeToPath(path string, img image.Image) (err error)

EncodeToPath image.Image => 文件

func GetBase64ImageExt

func GetBase64ImageExt(base64Str string) (string, error)

GetBase64ImageExt 从 base64 字符串中识别图片格式并返回文件后缀

@return 文件扩展名(小写 && 带"."),e.g. ".jpg" ".png"

func GetImageBase64

func GetImageBase64(imagePath string) (string, error)

GetImageBase64 (硬盘上的)图片 => base64字符串

参考: golang 将图片生成Base64 https://blog.csdn.net/weixin_40292098/article/details/126029489

func GetSize

func GetSize(path string) (width int, height int, err error)

GetSize 获取图片的宽和高.

PS: 单位为像素(px).

func GetWebImageBase64

func GetWebImageBase64(url string) (string, error)

GetWebImageBase64 网络图片 => base64字符串

参考: golang 将图片生成Base64 https://blog.csdn.net/weixin_40292098/article/details/126029489

@param url e.g."https://img.redocn.com/sheying/20150507/pugongying_4267498.jpg"

func Resize

func Resize(srcPath, dstPath string, width, height int) (err error)

Resize 缩放图片到指定尺寸(不保证纵横比).

@param srcPath	源图片文件路径
@param dstPath	目标图片文件路径
@param width	目标宽度(像素)
@param height	目标高度(像素)
@return 返回错误信息,如果成功则返回 nil

func ResizeByHeight

func ResizeByHeight(srcPath, dstPath string, height int) (err error)

ResizeByHeight 按高度等比例缩放图片.

func ResizeByWidth

func ResizeByWidth(srcPath, dstPath string, width int) (err error)

ResizeByWidth 按宽度等比例缩放图片.

func ResizeImage

func ResizeImage(srcImg image.Image, width, height int) (image.Image, error)

ResizeImage 缩放图片到指定尺寸(不保证纵横比).

func ResizeImageByHeight

func ResizeImageByHeight(srcImg image.Image, height int) (image.Image, error)

ResizeImageByHeight 按高度等比例缩放图片.

@param src		源图片对象
@param height	目标高度

func ResizeImageByWidth

func ResizeImageByWidth(srcImg image.Image, width int) (image.Image, error)

ResizeImageByWidth 按宽度等比例缩放图片.

@param src		源图片对象
@param width	目标宽度

func ResizeImageKeepAspectRatio

func ResizeImageKeepAspectRatio(srcImg image.Image, maxWidth, maxHeight int) (image.Image, error)

ResizeImageKeepAspectRatio 按比例调整图片大小(保证纵横比;适应指定尺寸).

@param	src			源图片对象
@param	maxWidth	最大宽度
@param	maxHeight	最大高度

func ResizeImageWithScale

func ResizeImageWithScale(srcImg image.Image, scale float64) (image.Image, error)

ResizeImageWithScale 按指定比例缩放图片(保证纵横比).

func ResizeKeepAspectRatio

func ResizeKeepAspectRatio(srcPath, dstPath string, maxWidth, maxHeight int) (err error)

ResizeKeepAspectRatio 按比例调整图片大小(保证纵横比;适应指定尺寸).

func ResizeWithScale

func ResizeWithScale(srcPath, dstPath string, scale float64) (err error)

ResizeWithScale 按指定比例缩放图片(保证纵横比).

func ToBmp

func ToBmp(src, dest string) error

ToBmp 将图片格式转换为".bmp".

func ToGrayscale added in v3.26.121

func ToGrayscale(src image.Image) image.Image

ToGrayscale 灰度处理:image.Image => 灰度图

使用标准 Rec.601 亮度公式:Y = 0.299R + 0.587G + 0.114B

func ToGrayscaleWithPath added in v3.26.121

func ToGrayscaleWithPath(input, output string) error

ToGrayscaleWithPath 灰度处理:图片 => 灰度图

func ToJpeg

func ToJpeg(src, dest string, qualityArgs ...int8) error

ToJpeg 将图片格式转换为".jpg"(||".jpeg").

@param qualityArgs (默认: 100; 取值范围: [1, 100])生成jpeg图片的质量

func ToPng

func ToPng(src, dest string) error

ToPng 将图片格式转换为".png".

func ToTiff

func ToTiff(src, dest string, opts *tiff.Options) error

ToTiff 将图片格式转换为".tiff".

func TrimTransparentForPng

func TrimTransparentForPng(inputPath, outputPath string) error

TrimTransparentForPng 裁剪 .png 图片的透明边缘.

@param inputPath 输入图片的路径,要求后缀为.png @param outputPath 输出图片的路径,为空则覆盖原文件

Types

type Info

type Info struct {
	ExtName    string      `json:"extName"`
	ColorModel color.Model `json:"colorModel"`

	Width  int `json:"width"`
	Height int `json:"height"`
}

func GetInfo

func GetInfo(path string) (*Info, error)

GetInfo 获取图片的信息(宽、高、后缀名).

【图像处理】Golang 获取常用图像的宽高总结

https://www.cnblogs.com/voipman/p/16108320.html

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL