ParseOptions parses str as a list of comma separated transformation options. The options can be specified in in order, with duplicate options overwriting previous values. # Rectangle Crop There are four options controlling rectangle crop: cx{x} - X coordinate of top left rectangle corner (d
(str string)
| 250 | // cw100,ch100 - crop image to 100px square, starting at (0,0) |
| 251 | // cx10,cy20,cw100,ch200 - crop image starting at (10,20) is 100px wide and 200px tall |
| 252 | func ParseOptions(str string) Options { |
| 253 | var options Options |
| 254 | |
| 255 | for _, opt := range strings.Split(str, ",") { |
| 256 | switch { |
| 257 | case len(opt) == 0: // do nothing |
| 258 | case opt == optFit: |
| 259 | options.Fit = true |
| 260 | case opt == optFlipVertical: |
| 261 | options.FlipVertical = true |
| 262 | case opt == optFlipHorizontal: |
| 263 | options.FlipHorizontal = true |
| 264 | case opt == optScaleUp: // this option is intentionally not documented above |
| 265 | options.ScaleUp = true |
| 266 | case opt == optFormatJPEG, opt == optFormatPNG, opt == optFormatTIFF: |
| 267 | options.Format = opt |
| 268 | case opt == optSmartCrop: |
| 269 | options.SmartCrop = true |
| 270 | case opt == optTrim: |
| 271 | options.Trim = true |
| 272 | case strings.HasPrefix(opt, optRotatePrefix): |
| 273 | value := strings.TrimPrefix(opt, optRotatePrefix) |
| 274 | options.Rotate, _ = strconv.Atoi(value) |
| 275 | case strings.HasPrefix(opt, optQualityPrefix): |
| 276 | value := strings.TrimPrefix(opt, optQualityPrefix) |
| 277 | options.Quality, _ = strconv.Atoi(value) |
| 278 | case strings.HasPrefix(opt, optSignaturePrefix): |
| 279 | options.Signature = strings.TrimPrefix(opt, optSignaturePrefix) |
| 280 | case strings.HasPrefix(opt, optCropX): |
| 281 | value := strings.TrimPrefix(opt, optCropX) |
| 282 | options.CropX, _ = strconv.ParseFloat(value, 64) |
| 283 | case strings.HasPrefix(opt, optCropY): |
| 284 | value := strings.TrimPrefix(opt, optCropY) |
| 285 | options.CropY, _ = strconv.ParseFloat(value, 64) |
| 286 | case strings.HasPrefix(opt, optCropWidth): |
| 287 | value := strings.TrimPrefix(opt, optCropWidth) |
| 288 | options.CropWidth, _ = strconv.ParseFloat(value, 64) |
| 289 | case strings.HasPrefix(opt, optCropHeight): |
| 290 | value := strings.TrimPrefix(opt, optCropHeight) |
| 291 | options.CropHeight, _ = strconv.ParseFloat(value, 64) |
| 292 | case strings.Contains(opt, optSizeDelimiter): |
| 293 | size := strings.SplitN(opt, optSizeDelimiter, 2) |
| 294 | if w := size[0]; w != "" { |
| 295 | options.Width, _ = strconv.ParseFloat(w, 64) |
| 296 | } |
| 297 | if h := size[1]; h != "" { |
| 298 | options.Height, _ = strconv.ParseFloat(h, 64) |
| 299 | } |
| 300 | default: |
| 301 | if size, err := strconv.ParseFloat(opt, 64); err == nil { |
| 302 | options.Width = size |
| 303 | options.Height = size |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return options |
| 309 | } |
no outgoing calls