Documentation ¶
Index ¶
- Constants
- Variables
- func AddjustBright(img image.Image, option BrightnessOption) image.Image
- func AddjustContrast(img image.Image, option ContrastOption) image.Image
- func AddjustGamma(img image.Image, option GammaOption) image.Image
- func AddjustRotate(img image.Image, option RotateOption) image.Image
- func AddjustSaturation(img image.Image, option SaturationOption) image.Image
- func AddjustSharpen(img image.Image, option SharpenOption) image.Image
- func AdjustBlur(img image.Image, option BlurOption) image.Image
- func CropImage(img image.Image, option CropOption) image.Image
- func CropPositionPoint(img image.Image, node Position, w, h float64) (x, y float64)
- func DecodeImage(file io.Reader, format Format) (img image.Image, err error)
- func DownloadImage(url string) (image.Image, string, error)
- func EncodeImage(img image.Image, w io.Writer, format Format, quality int) error
- func LoadGif(filePath string) (*gif.GIF, error)
- func ParseHexColor(s string) (*color.RGBA, error)
- func ParseRGBAColor(c *color.RGBA) string
- func Process(img image.Image, w io.Writer, originalFormat Format, options []Option) error
- func ProcessGif(gifImg *gif.GIF, w io.Writer, options []Option) error
- func ResizeGif(gifImg *gif.GIF, option ResizeOption)
- func ResizeImage(img image.Image, option ResizeOption) image.Image
- func SaveImage(img image.Image, filePath string, format Format, quality int) error
- func SerializeOptions(options []Option) string
- func TextWatermarkPositionPoint(img image.Image, option TextWatermarkOption, w, h float64) (x, y float64)
- func WarterMarkText(img image.Image, option TextWatermarkOption) image.Image
- type BlurOption
- type BrightnessOption
- type ContrastOption
- type CropOption
- type Format
- type FormatOption
- type GammaOption
- type HueOption
- type Option
- type Parameter
- type Position
- type QualityOption
- type ResizeMode
- type ResizeOption
- type RotateOption
- type SaturationOption
- type SharpenOption
- type TextWatermarkOption
- type WatermarkOption
Constants ¶
const ( JPG = "jpg" JPEG = "jpeg" PNG = "png" BMP = "bmp" GIF = "gif" WEBP = "webp" TIFF = "tiff" )
const ( Lanczos = "lanczos" CatmullRom = "catmullrom" Linear = "linear" Box = "box" NearestNeighbor = "nearestneighbor" )
* Imaging supports image resizing using various resampling filters. The most notable ones:
Lanczos - A high-quality resampling filter for photographic images yielding sharp results. CatmullRom - A sharp cubic filter that is faster than Lanczos filter while providing similar results. MitchellNetravali - A cubic filter that produces smoother results with less ringing artifacts than CatmullRom. Linear - Bilinear resampling filter, produces smooth output. Faster than cubic filters. Box - Simple and fast averaging filter appropriate for downscaling. When upscaling it's similar to NearestNeighbor. NearestNeighbor - Fastest resampling filter, no antialiasing. The full list of supported filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali, CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine. Custom filters can be created using ResampleFilter struct.
Variables ¶
var ErrUnsupportedFormat = errors.New("imaging: unsupported image format")
ErrUnsupportedFormat means the given image format is not supported.
var ErrUnsupportedParameter = errors.New("imaging: unsupported image parameter")
Functions ¶
func AddjustBright ¶
func AddjustBright(img image.Image, option BrightnessOption) image.Image
func AddjustContrast ¶
func AddjustContrast(img image.Image, option ContrastOption) image.Image
func AddjustGamma ¶
func AddjustGamma(img image.Image, option GammaOption) image.Image
func AddjustRotate ¶
func AddjustRotate(img image.Image, option RotateOption) image.Image
func AddjustSaturation ¶
func AddjustSaturation(img image.Image, option SaturationOption) image.Image
func AddjustSharpen ¶
func AddjustSharpen(img image.Image, option SharpenOption) image.Image
func AdjustBlur ¶
func AdjustBlur(img image.Image, option BlurOption) image.Image
func CropPositionPoint ¶
裁剪原点 位置计算方法
func DecodeImage ¶
LoadImageeader 从给定的 io.Reader 中加载指定文件扩展名的图像。 参数:
file: 包含图像数据的 io.Reader。 fileExt: 图像文件的扩展名,用于确定图像格式。
返回值:
img: 成功加载的图像对象。 err: 如果加载失败,返回错误信息。
func DownloadImage ¶
downloadImage downloads an image from a URL and returns it as an image.Image.
func EncodeImage ¶
EncodeImage 根据指定的格式和质量将图像编码并写入指定的输出流。 参数:
img: 需要编码的图像。 w: 写入编码后图像数据的输出流。 format: 图像的格式字符串,如"jpeg"、"png"等。 quality: 图像的压缩质量,取值范围为1到100。
返回值:
如果编码过程中出现错误,返回相应的错误信息。
func ParseHexColor ¶
parseHexColor converts a hex color string to an image/color.RGBA.
func ParseRGBAColor ¶
func ResizeGif ¶
func ResizeGif(gifImg *gif.GIF, option ResizeOption)
func ResizeImage ¶
func ResizeImage(img image.Image, option ResizeOption) image.Image
ResizeImage resizes the given image according to specified parameters.
func TextWatermarkPositionPoint ¶
func TextWatermarkPositionPoint(img image.Image, option TextWatermarkOption, w, h float64) (x, y float64)
获取位置起始点 option.X 指定水印的水平边距, 即距离图片边缘的水平距离 option.Y 指定水印的垂直边距, 即距离图片边缘的垂直距离 w 水印的宽度 h 水印的高度 。_______________X(W) |***(0,h) | | | |****(0,Ymax) Y(H)
func WarterMarkText ¶
func WarterMarkText(img image.Image, option TextWatermarkOption) image.Image
Types ¶
type BlurOption ¶
type BlurOption struct {
Radius int `option:"r,optional"`
}
type BrightnessOption ¶
type BrightnessOption struct {
Value int `option:",optional"`
}
type ContrastOption ¶
type ContrastOption struct {
Value int `option:",optional"`
}
type CropOption ¶
type CropOption struct { Width int `option:"w,optional"` Height int `option:"h,optional"` X int `option:"x,optional"` Y int `option:"y,optional"` Position Position `option:"g,optional"` }
* | **Parameter** | **Description** | **Value range** | | ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | **w** | The width that you want to crop. | [0, image width]Default value: the maximum value. | | **h** | The height that you want to crop. | [0, image height]Default value: the maximum value. | | **x** | The X coordinate of the area that you want to crop. The default value is the X coordinate of the upper-left corner of the image. | [0, image bound] | | **y** | The Y coordinate of the area that you want to crop. The default value is the Y coordinate of the upper-left corner of the image. | [0, image bound] | | **g** | The position of the area that you want to crop in a 3 x 3 grid. The image is located in a 3 x 3 grid. The grid has nine tiles. | nw: upper leftnorth: upper middlene: upper rightwest: middle leftcenter: centereast: middle rightsw: lower leftsouth: lower middlese: lower rightFor more information about how to calculate the position of each tile, see [the following table](https://www.alibabacloud.com/help/en/oss/user-guide/custom-crop?spm=a2c63.p38356.0.0.7d7d6661kGXVLR#table-xdo-tzc-rfu). |
type Format ¶
type Format string
Format is an image file format.
func FormatFromExtension ¶
FormatFromExtension parses image format from filename extension:
func FormatFromFilename ¶
type FormatOption ¶
type FormatOption struct {
Format Format `option:",optional"`
}
* | **Valid value** | **Description** | | --------------- | ------------------------------------------------------------ | | **jpg** | Converts the format of a source image to JPG.**Important**Images in the HEIC format that support alpha channels cannot be converted to JPG images. | | **png** | Converts the format of a source image to PNG. | | **webp** | Converts the format of a source image to WebP. | | **bmp** | Converts the format of a source image to BMP. | | **gif** | Converts the format of a source image to GIF. The conversion takes effect only when the source image is also a GIF image. If the source image is not in the GIF format, the processed image is stored in the original format. | | **tiff** | Converts the format of a source image to TIFF. |
type GammaOption ¶
type GammaOption struct {
Value float64 `option:",optional"`
}
type Option ¶
type Option struct { Parameter Parameter `option:"parameter"` Option interface{} `option:"option"` }
func ParseOptions ¶
根据传入的参数构建不同的图像处理选项 parameter: 图像处理操作的参数,格式为"操作名_参数1_参数2..." 返回一个映射,键为图像处理操作类型,值为相应的处理选项 example: image/resize,h_100,m_lfit/format,jpg
func ParseUrlOptions ¶
parseUrl解析给定的URL字符串,提取并解析其中的"x-oss-process"参数。 参数urlstr是需要解析的URL字符串。 返回值是一个映射,其中包含解析后的参数,以及一个错误对象。 如果解析过程中发生错误,会返回nil和错误对象。 example: https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example.gif?x-oss-process=image/format,png
type Parameter ¶
type Parameter string
const ( Resize Parameter = "resize" Watermark Parameter = "watermark" FormatType Parameter = "format" Crop Parameter = "crop" Blur Parameter = "blur" Brightness Parameter = "bright" Sharpen Parameter = "sharpen" Contrast Parameter = "contrast" Gamma Parameter = "gamma" Saturation Parameter = "saturation" Hue Parameter = "hue" Rotate Parameter = "rotate" Quality Parameter = "quality" )
type QualityOption ¶
type QualityOption struct { Quality int `option:"q,optional"` Fiter imaging.ResampleFilter `option:"f,optional"` }
| **Parameter** | **Description** | **Value range** | | ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | **q** | The quality of the image. | [0,100]Default value: 100. |
type ResizeMode ¶
type ResizeMode string
const ( Lfit ResizeMode = "lfit" // default Mfit ResizeMode = "mfit" Fill ResizeMode = "fill" Pad ResizeMode = "pad" Fixed ResizeMode = "fixed" )
type ResizeOption ¶
type ResizeOption struct { ResizeMode ResizeMode `option:"m"` Width int `option:"w,optional"` Height int `option:"h,optional"` Longer int `option:"l"` // 不适用 Shorter int `option:"s"` // 不适用 Limit int `option:"limit"` Color *color.RGBA `option:"color"` }
* * | **Parameter** | **Required** | **Description** | **Value range** | | ------------- | ------------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | **m** | Yes | Specifies the type of the resize action. | lfit: OSS proportionally resizes the source image as large as possible in a rectangle based on the specified width and height. This is the default value.mfit: OSS proportionally resizes the source image as small as possible outside a rectangle based on the specified width and height.fill: OSS proportionally resizes the source image as small as possible outside a rectangle, and then crops the resized image from the center based on the specified width and height.pad: OSS resizes the source image as large as possible in a rectangle based on the specified width and height, and fills the empty space with a specific color.fixed: OSS forcibly resizes the source image based on the specified width and height.For more information about examples of each resizing type, see [Examples](https://www.alibabacloud.com/help/en/oss/user-guide/resize-images-4?spm=a2c63.p38356.0.0.1ba3f386WmEBnU#li-llf-951-r0b).**Important**If you set this parameter to lfit or mfit, the aspect ratio (the ratio of the width to the height) of the source image is rounded to an integer if the ratio is a decimal.If you specify the m parameter and specify a value for w or h, the values specified for l and s do not take effect. | | **w** | No | Specifies the width to which you want to resize the image. | [1,16384] | | **h** | No | Specifies the height to which you want to resize the image. | [1,16384] | | **l** | Yes | Specifies the length of the longer side to which you want to resize the image.**Note**The longer side is the side for which the ratio of the source length to the target length is larger. For example, if a source image is resized from 400 × 200 pixels to 800 × 100 pixels, the source-to-target size ratios are 0.5 (400/800) and 2 (200/100). 0.5 is smaller than 2. As a result, the side that contains 200 pixels is used as the longer side. | [1,16384] | | **s** | Yes | Specifies the length of the shorter side to which you want to resize the image.**Note**The shorter side is the side for which the ratio of the source length to the target length is smaller. For example, if a source image is resized from 400 × 200 pixels to 800 × 100 pixels, the source-to-target ratios are 0.5 (400/800) and 2 (200/100). 0.5 is smaller than 2. As a result, the side that contains 400 pixels is used as the shorter side. | [1,16384] | | **limit** | No | Specifies whether to resize the image when the resolution of the target image is higher than the resolution of the source image. | 1: This is the default value. OSS returns the image that is processed based on the resolution of the source image. The size of the returned image may be different from the size of the source image.0: OSS resizes the source image based on the specified value.**Note**The size of a GIF image can only be reduced. | | **color** | Yes (only when the value of m is pad) | If you set the resizing type to pad, you can select a color to fill the empty space. | RGB color values. For example, 000000 indicates black, and FFFFFF indicates white.Default value: FFFFFF (white). |
type RotateOption ¶
type RotateOption struct {
Value int `option:",optional"`
}
* The degree by which the image is rotated clockwise.
type SaturationOption ¶
type SaturationOption struct {
Value int `option:",optional"`
}
type SharpenOption ¶
type SharpenOption struct {
Value int `option:",optional"`
}
type TextWatermarkOption ¶
type WatermarkOption ¶
type WatermarkOption struct { Opacity int `option:"t,optional"` Position Position `option:"g,optional"` X int `option:"x,optional"` Y int `option:"y,optional"` Voffset int `option:"voffset,optional"` Fill int `option:"fill,optional"` Padx int `option:"padx,optional"` Pady int `option:"pady,optional"` }
* | **Parameter** | **Required** | **Description** | **Valid value** | | ------------- | ------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | | **t** | No | The opacity of the watermark. | [0,100]Default value: 100. The value 100 specifies that the watermark is opaque. | | **g** | No | The position of the watermark on the image. | nw: upper left.north: upper middle.ne: upper right.west: middle left.center: center.east: middle right.sw: lower left.south: lower middle.se: lower right. This is the default value.For the precise position that is specified by each value, see the following figure. | | **x** | No | The horizontal margin, which indicates the horizontal distance between the watermark and the image edge. This parameter takes effect only when the watermark is on the upper left, middle left, lower left, upper right, middle right, or lower right of the image. | [0,4096]Default value: 10.Unit: pixel. | | **y** | No | The vertical margin that specifies the vertical distance between the watermark and the image edge. This parameter takes effect only when the watermark is on the upper left, upper middle, upper right, lower left, lower middle, or lower right of the image. | [0,4096]Default value: 10.Unit: pixel. | | **voffset** | No | The vertical offset from the middle line. When the watermark is on the middle left, center, or middle right of the image, you can specify the vertical offset of the watermark along the middle line. | [-1000,1000]Default value: 0.Unit: pixel. | | **fill** | No | Specifies whether to tile the image watermarks or text watermarks across the image.**Note**If you want to add tiled watermarks, submit an application at [Quota Center](https://oss.console.aliyun.com/more-tool/quota-setting).. | 1: tiles the image watermarks or text watermarks across the image.0: does not tile the image watermarks or text watermarks across the image. This is the default value. | | **padx** | No | The horizontal spacing between watermarks when the image watermarks or text watermarks are tiled across the image. This parameter is valid only when you set fill to 1. | [0,4096]Default value: 0.Unit: pixel. | | **pady** | No | The vertical spacing between watermarks when the image watermarks or text watermarks are tiled across the image. This parameter is valid only when you set fill to 1. | [0,4096]Default value: 0.Unit: pixel. |