41 lines
946 B
Go
41 lines
946 B
Go
|
|
package design
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"strconv"
|
|||
|
|
"strings"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
DefaultGeneratedImageSize = "1024x1024"
|
|||
|
|
DefaultGeneratedImageWidth = 1024
|
|||
|
|
DefaultGeneratedImageHeight = 1024
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func ParseImageSize(size string) (float64, float64, bool) {
|
|||
|
|
size = strings.ToLower(strings.TrimSpace(size))
|
|||
|
|
if size == "" {
|
|||
|
|
return 0, 0, false
|
|||
|
|
}
|
|||
|
|
size = strings.NewReplacer(" ", "", "\t", "", "\n", "", "×", "x", "*", "x").Replace(size)
|
|||
|
|
parts := strings.Split(size, "x")
|
|||
|
|
if len(parts) != 2 {
|
|||
|
|
return 0, 0, false
|
|||
|
|
}
|
|||
|
|
width, err := strconv.ParseFloat(parts[0], 64)
|
|||
|
|
if err != nil || width <= 0 {
|
|||
|
|
return 0, 0, false
|
|||
|
|
}
|
|||
|
|
height, err := strconv.ParseFloat(parts[1], 64)
|
|||
|
|
if err != nil || height <= 0 {
|
|||
|
|
return 0, 0, false
|
|||
|
|
}
|
|||
|
|
return width, height, true
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func GeneratedImageDimensions(size string) (float64, float64) {
|
|||
|
|
if width, height, ok := ParseImageSize(size); ok {
|
|||
|
|
return width, height
|
|||
|
|
}
|
|||
|
|
return DefaultGeneratedImageWidth, DefaultGeneratedImageHeight
|
|||
|
|
}
|