imageprocess

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

README

中文 | English

imgaeprocess

本项目基于以下开源库: [imaging](disintegration/imaging: Imaging is a simple image processing package for Go (github.com))

About Imaging is a simple image processing package for Go

webp

WebP decoder and encoder for Go (Zero Dependencies).

gg

Go Graphics - 2D rendering in Go with a simple API.

Imageprocess是一个简单的Go图像处理包。支持 WEBP,JPG,JPEG,PNG,BMP,TIFF,GIF。提供了类似阿里云oss的图片处理能力包括:

图片缩放

图片水印

自定义裁剪

质量变换

格式转换

模糊效果

旋转

亮度

锐化

对比度

参数兼容阿里云oss图片处理参数,可以用于搭建本地文件oss图片处理系统。

0.安装

go get github.com/AndsGo/imageprocess

1.代码示例

代码Api使用示例

// 改变大小,高100px,宽300px,模式等比缩放匹配最大边
img = imageprocess.ResizeImage(img, ResizeOption{ResizeMode: Lfit, Width: 10, Height: 100)
// 将原图缩放成宽高100 px:`resize,h_100,w_100` 缩放模式fill:`m_fill`
img = imageprocess.ResizeImage(img, ResizeOption{ResizeMode: Fill, Width: 10, Height: 100)
// 将原图缩放成宽高100 px:`resize,h_100,w_100` 缩放模式pad:`m_pad`。 以红色填充:`color_FF0000`
img = imageprocess.ResizeImage(img, ResizeOption{ResizeMode: Pad, Width: 10, Height: 100 Color: &color.RGBA{R: 255, G: 0, B: 0, A: 255}})
// 将example.jpg缩略为宽高300:`resize,w_300,h_300` 水印内容为“Hello World”:`text_Hello%20World 水印文字颜色为白色、字体大小为30:`color_FFFFFF,size_30` ` 水印文字位置是右下、水平边距10、中线垂直偏移10:`g_se,x_10,y_10`
// 多模式串行处理可以使用Process或(ProcessGif)方法,你也可以全部使用Process或(ProcessGif)方法进行处理
options := make([]Option, 0)
options = append(options, Option{Resize, ResizeOption{Width: 300, Height: 300)
options = append(options, Option{Watermark, TextWatermarkOption{WatermarkOption: WatermarkOption{
    Opacity: 20, Position: Center, X: 10, Y: 10,
}, Color: &color.RGBA{255, 255, 255, 1}, Size: 30, Text: "Hello World"}})
imageprocess.Process(img, file, options)
// 裁剪起点为(800,500):`crop,x_800,y_500` 裁减范围300 px*300 px:`w_300,h_300`
img = imageprocess.CropImage(img, CropOption{X:800,Y:500,Width: 300, Height: 300})
// 裁剪起点为原图右下角:`crop,g_se` 裁减范围900 px*900 px:`w_900,h_900`
img = imageprocess.CropImage(img, CropOption{Position: SouthEast,Width: 900, Height: 900})
// 原图缩放为宽100 px:`resize,w_100,h_100` 图片相对质量设置为80%:`quality,q_80`
options := make([]Option, 0)
options = append(options, Option{Resize, ResizeOption{Width: 100, Height: 100)
// 质量
options = append(options, Option{Quality, QualityOption{Quality: 80}})
// 格式转换为PNG
options = append(options, Option{FormatType, FormatOption{Format: PNG}})
imageprocess.Process(img, file, options)
// 将原图按顺时针旋转90° 
img = imageprocess.AddjustRotate(img,RotateOption{Value:90})
// 将图片亮度提高50
img = imageprocess.AddjustBright(img,BrightnessOption{Value:50})
// 对原图进行锐化处理,锐化参数为100
img = imageprocess.AddjustSharpen(img,SharpenOption{Value:100})
// 对比度提高50
img = imageprocess.AddjustContrast(img,ContrastOption{Value:50})     

综合代码示例请查看

process_test.go

package imageprocess

import (
	"image/color"
	"os"
	"testing"
)

func Test_Process(t *testing.T) {
	img, f, err := LoadImage("examples/example.jpg")
	if err != nil {
		t.Error(err)
	}
	file, _ := os.Create("examples/out.jpg")
	options := make([]Option, 0)
	// 格式转换为PNG
	options = append(options, Option{FormatType, FormatOption{Format: PNG}})
	//  改变大小
	options = append(options, Option{Resize, ResizeOption{ResizeMode: Pad, Width: 300, Height: 300, Color: &color.RGBA{R: 255, G: 255, B: 0, A: 255}}})
	// 裁剪
	options = append(options, Option{Crop, CropOption{Width: 200, Height: 200, X: 0, Y: 0, Position: Center}})
	// 水印
	options = append(options, Option{Watermark, TextWatermarkOption{WatermarkOption: WatermarkOption{
		Opacity: 100, Position: South, X: 10, Y: 10,
	}, Color: &color.RGBA{111, 222, 111, 1}, Size: 40, Text: "hello watermark"}})
	// 模糊
	options = append(options, Option{Blur, GammaOption{Value: 5}})
	// 质量
	options = append(options, Option{Quality, QualityOption{Quality: 500}})
	err = Process(img, file, f, options)
	if err != nil {
		t.Error(err)
	}
}

func Test_ProcessGif(t *testing.T) {
	img, err := LoadGif("examples/example.gif")
	if err != nil {
		t.Error(err)
	}
	file, _ := os.Create("examples/out.gif")
	options := make([]Option, 0)
	// 格式转换为GIF
	options = append(options, Option{FormatType, FormatOption{Format: GIF}})
	options = append(options, Option{Gamma, GammaOption{Value: 500}})
	options = append(options, Option{Resize, ResizeOption{ResizeMode: Pad, Width: 300, Height: 300, Color: &color.RGBA{R: 255, G: 255, B: 255, A: 1}}})
	options = append(options, Option{Crop, CropOption{Width: 200, Height: 200, X: 0, Y: 0, Position: Center}})
	options = append(options, Option{Watermark, TextWatermarkOption{WatermarkOption: WatermarkOption{
		Opacity: 20, Position: Center, X: 10, Y: 10,
	}, Color: &color.RGBA{0, 0, 0, 1}, Size: 40, Text: "hello watermark"}})
	err = ProcessGif(img, file, options)
	if err != nil {
		t.Error(err)
	}
}

func Test_UrlOptions(t *testing.T) {
	img, f, err := LoadImage("examples/example.jpg")
	if err != nil {
		t.Error(err)
	}
	file, _ := os.Create("examples/out.jpg")
	// 增加水印,然后修改大小
	options, err := ParseOptions("image/watermark,t_30,g_center,x_10,y_10,text_hello watermark,color_1366ec,size_200/resize,m_pad,h_100,w_100,color_FF0000")
	if err != nil {
		t.Error(err)
	}
	err = Process(img, file, f, options)
	if err != nil {
		t.Error(err)
	}

}
Meaning Image
改变大小,高100px,宽300px,模式等比缩放匹配最大边 break
将原图缩放成宽高100 px:resize,h_100,w_100 缩放模式fill:m_fill 自动裁剪
将原图缩放成宽高100 px:resize,h_100,w_100 缩放模式pad:m_pad。 以红色填充:color_FF0000 填充红色
将example.jpg缩略为宽高300:resize,w_300,h_300 水印内容为“Hello World”:text_Hello%20World 水印文字颜色为白色、字体大小为30:color_FFFFFF,size_30 水印文字位置是右下、水平边距10、中线垂直偏移10:g_se,x_10,y_10 图片处理1
裁剪起点为(800,500):crop,x_800,y_500 裁减范围300 px*300 px:w_300,h_300 裁剪2
裁剪起点为原图右下角:crop,g_se 裁减范围900 px*900 px:w_900,h_900 裁剪3
原图缩放为宽100 px:resize,w_100,h_100 图片相对质量设置为80%:quality,q_80 变换1
将原图转换为PNG格式 png
将原图按顺时针旋转90° 旋转1
将图片亮度提高50 亮度1
对原图进行锐化处理,锐化参数为100 锐化1
对比度提高50 对比度2

2.参数示例

url参数示例

格式转换可以在process_test中进行测试 ,测试代码如下

func Test_UrlOptions(t *testing.T) {
	img, f, err := LoadImage("examples/example.jpg")
	if err != nil {
		t.Error(err)
	}
	file, _ := os.Create("examples/out.jpg")
	// 增加水印,然后修改大小
	options, err := ParseOptions("image/watermark,t_30,g_center,x_10,y_10,text_hello watermark,color_1366ec,size_200/resize,m_pad,h_100,w_100,color_FF0000")
	if err != nil {
		t.Error(err)
	}
    // 处理图片
	err = Process(img, file, f, options)
	if err != nil {
		t.Error(err)
	}
}
Options Meaning Image
resize,h_100,w_300,m_lfit 改变大小,高100px,宽300px,模式等比缩放匹配最大边 break
resize,m_fill,h_100,w_100 将原图缩放成宽高100 px:resize,h_100,w_100 缩放模式fill:m_fill 自动裁剪
resize,m_pad,h_100,w_100,color_FF0000 将原图缩放成宽高100 px:resize,h_100,w_100 缩放模式pad:m_pad。 以红色填充:color_FF0000 填充红色
resize,w_300,h_300/watermark,size_30,text_Hello World,color_FFFFFF,g_se,x_10,y_10 将example.jpg缩略为宽高300:resize,w_300,h_300 水印内容为“Hello World”:text_Hello%20World 水印文字颜色为白色、字体大小为30:color_FFFFFF,size_30 水印文字位置是右下、水平边距10、中线垂直偏移10:g_se,x_10,y_10 图片处理1
crop,x_800,y_500,w_300,h_300 裁剪起点为(800,500):crop,x_800,y_500 裁减范围300 px*300 px:w_300,h_300 裁剪2
crop,w_900,h_900,g_se 裁剪起点为原图右下角:crop,g_se 裁减范围900 px*900 px:w_900,h_900 裁剪3
resize,w_100/quality,q_80 原图缩放为宽100 px:resize,w_100 图片相对质量设置为80%:quality,q_80 变换1
format,png 将原图转换为PNG格式 png
rotate,90 将原图按顺时针旋转90° 旋转1
bright,50 将图片亮度提高50 亮度1
sharpen,100 对原图进行锐化处理,锐化参数为100 锐化1
contrast,-50 对比度提高50 对比度2

3.综合示例

这是一个简单文件服务器的例子,代码位于examples文件夹下

cd examples
go run example.go

原图: 2500*1875 image

访问:

http://127.0.0.1:8080/file/example.jpg?x-oss-process=image/resize,w_500,h_300/watermark,t_80,g_se,x_10,y_10,text_hello,color_FFFFFF,size_40/format,webp

结果: 400*300 65.4k

imgae

转换代码表示:

resize,w_500,h_300 转换宽500,高300

watermark,t_80,g_se,x_10,y_10,text_hello,color_FFFFFF,size_40 增加水印,水印位置位于右下,离边缘距离为10,水印内容为hello,颜色为FFFFFF,文字大小为40

format,webp格式转换为 webp

示例代码(你测试自己的图片需要修改 fileFolders):

package main

import (
	"fmt"
	"image/gif"
	"io"
	"net/http"
	"os"
	"strings"

	"github.com/AndsGo/imageprocess"
)

// 文件夹,you need change it
var fileFolders = "./"

func main() {
	http.HandleFunc("/file/", fileHandler)

	fmt.Println("Starting server on :8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		fmt.Println("Server failed:", err)
	}
}

func fileHandler(w http.ResponseWriter, r *http.Request) {
	// 获取文件名称
	fileName := strings.TrimPrefix(r.URL.Path, "/file/")
	// 打开文件
	file, err := os.Open(fmt.Sprintf("%s%s", fileFolders, fileName))
	if err != nil {
		http.Error(w, "File not found", http.StatusNotFound)
		return
	}
	defer file.Close()
	// 获取参数
	// 获取文件后缀
	f, err := imageprocess.FormatFromExtension(fileName)
	if err != nil {
		// 将处理后的文件内容写入响应
		if _, err := io.Copy(w, file); err != nil {
			http.Error(w, "Failed to send file", http.StatusInternalServerError)
		}
		return
	}
	//处理处理参数
	ossParams := r.URL.Query().Get("x-oss-process")
	if ossParams == "" {
		//无需处理
		if _, err := io.Copy(w, file); err != nil {
			http.Error(w, "Failed to send file", http.StatusInternalServerError)
		}
		return
	}
	options, err := imageprocess.ParseOptions(ossParams)
	if err != nil {
		http.Error(w, fmt.Sprintf("ParseOptions %s", err.Error()), http.StatusInternalServerError)
		return
	}
	if len(options) == 0 {
		//无需处理
		if _, err := io.Copy(w, file); err != nil {
			http.Error(w, "Failed to send file", http.StatusInternalServerError)
		}
		return
	}
	//处理图片
	err = processImg(file, w, f, options)
	if err != nil {
		http.Error(w, fmt.Sprintf("processFile %s", err.Error()), http.StatusInternalServerError)
	}
}

// 进行转换
func processImg(file io.Reader, w io.Writer, f imageprocess.Format, options []imageprocess.Option) error {
	if f == imageprocess.GIF {
		imgGif, err := gif.DecodeAll(file)
		if err != nil {
			return err
		}
		return imageprocess.ProcessGif(imgGif, w, options)
	} else {
		img, err := imageprocess.DecodeImage(file, f)
		if err != nil {
			return err
		}
		return imageprocess.Process(img, w, f, options)
	}
}

Documentation

Index

Constants

View Source
const (
	JPG  = "jpg"
	JPEG = "jpeg"
	PNG  = "png"
	BMP  = "bmp"
	GIF  = "gif"
	WEBP = "webp"
	TIFF = "tiff"
)
View Source
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

View Source
var ErrUnsupportedFormat = errors.New("imaging: unsupported image format")

ErrUnsupportedFormat means the given image format is not supported.

View Source
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 CropImage

func CropImage(img image.Image, option CropOption) image.Image

func CropPositionPoint

func CropPositionPoint(img image.Image, node Position, w, h float64) (x, y float64)

裁剪原点 位置计算方法

func DecodeImage

func DecodeImage(file io.Reader, format Format) (img image.Image, err error)

LoadImageeader 从给定的 io.Reader 中加载指定文件扩展名的图像。 参数:

file: 包含图像数据的 io.Reader。
fileExt: 图像文件的扩展名,用于确定图像格式。

返回值:

img: 成功加载的图像对象。
err: 如果加载失败,返回错误信息。

func DownloadImage

func DownloadImage(url string) (image.Image, string, error)

downloadImage downloads an image from a URL and returns it as an image.Image.

func EncodeImage

func EncodeImage(img image.Image, w io.Writer, format Format, quality int) error

EncodeImage 根据指定的格式和质量将图像编码并写入指定的输出流。 参数:

img: 需要编码的图像。
w: 写入编码后图像数据的输出流。
format: 图像的格式字符串,如"jpeg"、"png"等。
quality: 图像的压缩质量,取值范围为1到100。

返回值:

如果编码过程中出现错误,返回相应的错误信息。

func LoadGif

func LoadGif(filePath string) (*gif.GIF, error)

func ParseHexColor

func ParseHexColor(s string) (*color.RGBA, error)

parseHexColor converts a hex color string to an image/color.RGBA.

func ParseRGBAColor

func ParseRGBAColor(c *color.RGBA) string

func Process

func Process(img image.Image, w io.Writer, originalFormat Format, options []Option) error

func ProcessGif

func ProcessGif(gifImg *gif.GIF, w io.Writer, options []Option) error

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 SaveImage

func SaveImage(img image.Image, filePath string, format Format, quality int) error

func SerializeOptions

func SerializeOptions(options []Option) string

序列化Options

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

func FormatFromExtension(ext string) (Format, error)

FormatFromExtension parses image format from filename extension:

func FormatFromFilename

func FormatFromFilename(filename string) (Format, error)

func LoadImage

func LoadImage(filePath string) (image.Image, Format, error)

LoadImage loads an image from the specified file path.

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 HueOption

type HueOption struct {
	Value int `option:",optional"`
}

type Option

type Option struct {
	Parameter Parameter   `option:"parameter"`
	Option    interface{} `option:"option"`
}

func ParseOptions

func ParseOptions(parameterStr string) (options []Option, err error)

根据传入的参数构建不同的图像处理选项 parameter: 图像处理操作的参数,格式为"操作名_参数1_参数2..." 返回一个映射,键为图像处理操作类型,值为相应的处理选项 example: image/resize,h_100,m_lfit/format,jpg

func ParseUrlOptions

func ParseUrlOptions(urlstr string) ([]Option, error)

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 Position

type Position string
const (
	NorthWest Position = "nw"
	North     Position = "north"
	NorthEast Position = "ne"
	West      Position = "west"
	Center    Position = "center"
	East      Position = "east"
	SouthWest Position = "sw"
	South     Position = "south"
	SouthEast Position = "se" // default
)
const (
	Face Position = "face"
	Auto Position = "auto"
)

not support

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 TextWatermarkOption struct {
	WatermarkOption
	Text   string      `option:"text"`
	Type   string      `option:"type,optional"`
	Color  *color.RGBA `option:"color,optional"`
	Size   int         `option:"size,optional"`
	Shadow int         `option:"shadow,optional"`
	Rotate int         `option:"rotate,optional"`
}

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. |

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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