|
|
@ -47,27 +47,27 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
|
|
|
imgWidth := bounds.Max.X
|
|
|
|
imgWidth := bounds.Max.X
|
|
|
|
imgHeight := bounds.Max.Y
|
|
|
|
imgHeight := bounds.Max.Y
|
|
|
|
|
|
|
|
|
|
|
|
// 计算缩放比例
|
|
|
|
// Calculating scaling
|
|
|
|
scaleWidth := float64(maxWidth) / float64(imgWidth)
|
|
|
|
scaleWidth := float64(maxWidth) / float64(imgWidth)
|
|
|
|
scaleHeight := float64(maxHeight) / float64(imgHeight)
|
|
|
|
scaleHeight := float64(maxHeight) / float64(imgHeight)
|
|
|
|
|
|
|
|
|
|
|
|
// 如果都为0,则不缩放,返回原始图片
|
|
|
|
// If both are 0, then no scaling is done and the original image is returned
|
|
|
|
if maxWidth == 0 && maxHeight == 0 {
|
|
|
|
if maxWidth == 0 && maxHeight == 0 {
|
|
|
|
return img
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果宽度和高度都大于0,则选择较小的缩放比例,以保持宽高比
|
|
|
|
// If both width and height are greater than 0, select a smaller zoom ratio to maintain the aspect ratio
|
|
|
|
if maxWidth > 0 && maxHeight > 0 {
|
|
|
|
if maxWidth > 0 && maxHeight > 0 {
|
|
|
|
scale := scaleWidth
|
|
|
|
scale := scaleWidth
|
|
|
|
if scaleHeight < scaleWidth {
|
|
|
|
if scaleHeight < scaleWidth {
|
|
|
|
scale = scaleHeight
|
|
|
|
scale = scaleHeight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 计算缩略图尺寸
|
|
|
|
// Calculate Thumbnail Size
|
|
|
|
thumbnailWidth := int(float64(imgWidth) * scale)
|
|
|
|
thumbnailWidth := int(float64(imgWidth) * scale)
|
|
|
|
thumbnailHeight := int(float64(imgHeight) * scale)
|
|
|
|
thumbnailHeight := int(float64(imgHeight) * scale)
|
|
|
|
|
|
|
|
|
|
|
|
// 使用"image"库的Resample方法生成缩略图
|
|
|
|
// Thumbnails are generated using the Resample method of the "image" library.
|
|
|
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
|
|
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
|
|
|
for y := 0; y < thumbnailHeight; y++ {
|
|
|
|
for y := 0; y < thumbnailHeight; y++ {
|
|
|
|
for x := 0; x < thumbnailWidth; x++ {
|
|
|
|
for x := 0; x < thumbnailWidth; x++ {
|
|
|
@ -80,12 +80,12 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
|
|
|
return thumbnail
|
|
|
|
return thumbnail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果只指定了宽度或高度,则根据最大不超过的规则生成缩略图
|
|
|
|
// If only width or height is specified, thumbnails are generated based on the maximum not to exceed rule
|
|
|
|
if maxWidth > 0 {
|
|
|
|
if maxWidth > 0 {
|
|
|
|
thumbnailWidth := maxWidth
|
|
|
|
thumbnailWidth := maxWidth
|
|
|
|
thumbnailHeight := int(float64(imgHeight) * scaleWidth)
|
|
|
|
thumbnailHeight := int(float64(imgHeight) * scaleWidth)
|
|
|
|
|
|
|
|
|
|
|
|
// 使用"image"库的Resample方法生成缩略图
|
|
|
|
// Thumbnails are generated using the Resample method of the "image" library.
|
|
|
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
|
|
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
|
|
|
for y := 0; y < thumbnailHeight; y++ {
|
|
|
|
for y := 0; y < thumbnailHeight; y++ {
|
|
|
|
for x := 0; x < thumbnailWidth; x++ {
|
|
|
|
for x := 0; x < thumbnailWidth; x++ {
|
|
|
@ -102,7 +102,7 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
|
|
|
thumbnailWidth := int(float64(imgWidth) * scaleHeight)
|
|
|
|
thumbnailWidth := int(float64(imgWidth) * scaleHeight)
|
|
|
|
thumbnailHeight := maxHeight
|
|
|
|
thumbnailHeight := maxHeight
|
|
|
|
|
|
|
|
|
|
|
|
// 使用"image"库的Resample方法生成缩略图
|
|
|
|
// Thumbnails are generated using the Resample method of the "image" library.
|
|
|
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
|
|
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
|
|
|
for y := 0; y < thumbnailHeight; y++ {
|
|
|
|
for y := 0; y < thumbnailHeight; y++ {
|
|
|
|
for x := 0; x < thumbnailWidth; x++ {
|
|
|
|
for x := 0; x < thumbnailWidth; x++ {
|
|
|
@ -115,6 +115,6 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
|
|
|
return thumbnail
|
|
|
|
return thumbnail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 默认情况下,返回原始图片
|
|
|
|
// By default, the original image is returned
|
|
|
|
return img
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
}
|
|
|
|