MCPcopy
hub / github.com/willnorris/imageproxy / transformImage

Function transformImage

transform.go:274–326  ·  view source on GitHub ↗

transformImage modifies the image m based on the transformations specified in opt.

(m image.Image, opt Options)

Source from the content-addressed store, hash-verified

272// transformImage modifies the image m based on the transformations specified
273// in opt.
274func transformImage(m image.Image, opt Options) image.Image {
275 timer := prometheus.NewTimer(metricTransformationDuration)
276 defer timer.ObserveDuration()
277
278 // trim
279 if opt.Trim {
280 m = trimEdges(m)
281 }
282
283 // Parse crop and resize parameters before applying any transforms.
284 // This is to ensure that any percentage-based values are based off the
285 // size of the original image.
286 rect := cropParams(m, opt)
287 w, h, resize := resizeParams(m, opt)
288
289 // crop if needed
290 if !m.Bounds().Eq(rect) {
291 m = imaging.Crop(m, rect)
292 }
293 // resize if needed
294 if resize {
295 if opt.Fit {
296 m = imaging.Fit(m, w, h, resampleFilter)
297 } else {
298 if w == 0 || h == 0 {
299 m = imaging.Resize(m, w, h, resampleFilter)
300 } else {
301 m = imaging.Thumbnail(m, w, h, resampleFilter)
302 }
303 }
304 }
305
306 // rotate
307 rotate := float64(opt.Rotate) - math.Floor(float64(opt.Rotate)/360)*360
308 switch rotate {
309 case 90:
310 m = imaging.Rotate90(m)
311 case 180:
312 m = imaging.Rotate180(m)
313 case 270:
314 m = imaging.Rotate270(m)
315 }
316
317 // flip
318 if opt.FlipVertical {
319 m = imaging.FlipV(m)
320 }
321 if opt.FlipHorizontal {
322 m = imaging.FlipH(m)
323 }
324
325 return m
326}
327
328// trimEdges returns a new image with solid color borders of the image removed.
329// The pixel at the top left corner is used to match the border color.

Callers 2

TransformFunction · 0.85
TestTransformImageFunction · 0.85

Calls 3

trimEdgesFunction · 0.85
cropParamsFunction · 0.85
resizeParamsFunction · 0.85

Tested by 1

TestTransformImageFunction · 0.68