process.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "math"
  7. "runtime"
  8. "github.com/imgproxy/imgproxy/v2/imagemeta"
  9. "golang.org/x/sync/errgroup"
  10. )
  11. const msgSmartCropNotSupported = "Smart crop is not supported by used version of libvips"
  12. var errConvertingNonSvgToSvg = newError(422, "Converting non-SVG images to SVG is not supported", "Converting non-SVG images to SVG is not supported")
  13. func imageTypeLoadSupport(imgtype imageType) bool {
  14. return imgtype == imageTypeSVG ||
  15. imgtype == imageTypeICO ||
  16. vipsTypeSupportLoad[imgtype]
  17. }
  18. func imageTypeSaveSupport(imgtype imageType) bool {
  19. return imgtype == imageTypeSVG || vipsTypeSupportSave[imgtype]
  20. }
  21. func imageTypeGoodForWeb(imgtype imageType) bool {
  22. return imgtype != imageTypeTIFF &&
  23. imgtype != imageTypeBMP
  24. }
  25. func extractMeta(img *vipsImage) (int, int, int, bool) {
  26. width := img.Width()
  27. height := img.Height()
  28. angle := vipsAngleD0
  29. flip := false
  30. orientation := img.Orientation()
  31. if orientation >= 5 && orientation <= 8 {
  32. width, height = height, width
  33. }
  34. if orientation == 3 || orientation == 4 {
  35. angle = vipsAngleD180
  36. }
  37. if orientation == 5 || orientation == 6 {
  38. angle = vipsAngleD90
  39. }
  40. if orientation == 7 || orientation == 8 {
  41. angle = vipsAngleD270
  42. }
  43. if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
  44. flip = true
  45. }
  46. return width, height, angle, flip
  47. }
  48. func calcScale(width, height int, po *processingOptions, imgtype imageType) float64 {
  49. var shrink float64
  50. srcW, srcH := float64(width), float64(height)
  51. dstW, dstH := float64(po.Width), float64(po.Height)
  52. if po.Width == 0 {
  53. dstW = srcW
  54. }
  55. if po.Height == 0 {
  56. dstH = srcH
  57. }
  58. if dstW == srcW && dstH == srcH {
  59. shrink = 1
  60. } else {
  61. wshrink := srcW / dstW
  62. hshrink := srcH / dstH
  63. rt := po.ResizingType
  64. if rt == resizeAuto {
  65. srcD := width - height
  66. dstD := po.Width - po.Height
  67. if (srcD >= 0 && dstD >= 0) || (srcD < 0 && dstD < 0) {
  68. rt = resizeFill
  69. } else {
  70. rt = resizeFit
  71. }
  72. }
  73. switch {
  74. case po.Width == 0:
  75. shrink = hshrink
  76. case po.Height == 0:
  77. shrink = wshrink
  78. case rt == resizeFit:
  79. shrink = math.Max(wshrink, hshrink)
  80. default:
  81. shrink = math.Min(wshrink, hshrink)
  82. }
  83. }
  84. if !po.Enlarge && shrink < 1 && imgtype != imageTypeSVG {
  85. shrink = 1
  86. }
  87. shrink /= po.Dpr
  88. if shrink > srcW {
  89. shrink = srcW
  90. }
  91. if shrink > srcH {
  92. shrink = srcH
  93. }
  94. return 1.0 / shrink
  95. }
  96. func canScaleOnLoad(imgtype imageType, scale float64) bool {
  97. if imgtype == imageTypeSVG {
  98. return true
  99. }
  100. if conf.DisableShrinkOnLoad || scale >= 1 {
  101. return false
  102. }
  103. return imgtype == imageTypeJPEG || imgtype == imageTypeWEBP
  104. }
  105. func canFitToBytes(imgtype imageType) bool {
  106. switch imgtype {
  107. case imageTypeJPEG, imageTypeWEBP, imageTypeHEIC, imageTypeTIFF:
  108. return true
  109. default:
  110. return false
  111. }
  112. }
  113. func calcJpegShink(scale float64, imgtype imageType) int {
  114. shrink := int(1.0 / scale)
  115. switch {
  116. case shrink >= 8:
  117. return 8
  118. case shrink >= 4:
  119. return 4
  120. case shrink >= 2:
  121. return 2
  122. }
  123. return 1
  124. }
  125. func calcPosition(width, height, innerWidth, innerHeight int, gravity *gravityOptions, allowOverflow bool) (left, top int) {
  126. if gravity.Type == gravityFocusPoint {
  127. pointX := scaleInt(width, gravity.X)
  128. pointY := scaleInt(height, gravity.Y)
  129. left = pointX - innerWidth/2
  130. top = pointY - innerHeight/2
  131. } else {
  132. offX, offY := int(gravity.X), int(gravity.Y)
  133. left = (width-innerWidth+1)/2 + offX
  134. top = (height-innerHeight+1)/2 + offY
  135. if gravity.Type == gravityNorth || gravity.Type == gravityNorthEast || gravity.Type == gravityNorthWest {
  136. top = 0 + offY
  137. }
  138. if gravity.Type == gravityEast || gravity.Type == gravityNorthEast || gravity.Type == gravitySouthEast {
  139. left = width - innerWidth - offX
  140. }
  141. if gravity.Type == gravitySouth || gravity.Type == gravitySouthEast || gravity.Type == gravitySouthWest {
  142. top = height - innerHeight - offY
  143. }
  144. if gravity.Type == gravityWest || gravity.Type == gravityNorthWest || gravity.Type == gravitySouthWest {
  145. left = 0 + offX
  146. }
  147. }
  148. var minX, maxX, minY, maxY int
  149. if allowOverflow {
  150. minX, maxX = -innerWidth+1, width-1
  151. minY, maxY = -innerHeight+1, height-1
  152. } else {
  153. minX, maxX = 0, width-innerWidth
  154. minY, maxY = 0, height-innerHeight
  155. }
  156. left = maxInt(minX, minInt(left, maxX))
  157. top = maxInt(minY, minInt(top, maxY))
  158. return
  159. }
  160. func cropImage(img *vipsImage, cropWidth, cropHeight int, gravity *gravityOptions) error {
  161. if cropWidth == 0 && cropHeight == 0 {
  162. return nil
  163. }
  164. imgWidth, imgHeight := img.Width(), img.Height()
  165. cropWidth = minNonZeroInt(cropWidth, imgWidth)
  166. cropHeight = minNonZeroInt(cropHeight, imgHeight)
  167. if cropWidth >= imgWidth && cropHeight >= imgHeight {
  168. return nil
  169. }
  170. if gravity.Type == gravitySmart {
  171. if err := img.CopyMemory(); err != nil {
  172. return err
  173. }
  174. if err := img.SmartCrop(cropWidth, cropHeight); err != nil {
  175. return err
  176. }
  177. // Applying additional modifications after smart crop causes SIGSEGV on Alpine
  178. // so we have to copy memory after it
  179. return img.CopyMemory()
  180. }
  181. left, top := calcPosition(imgWidth, imgHeight, cropWidth, cropHeight, gravity, false)
  182. return img.Crop(left, top, cropWidth, cropHeight)
  183. }
  184. func prepareWatermark(wm *vipsImage, wmData *imageData, opts *watermarkOptions, imgWidth, imgHeight int) error {
  185. if err := wm.Load(wmData.Data, wmData.Type, 1, 1.0, 1); err != nil {
  186. return err
  187. }
  188. po := newProcessingOptions()
  189. po.ResizingType = resizeFit
  190. po.Dpr = 1
  191. po.Enlarge = true
  192. po.Format = wmData.Type
  193. if opts.Scale > 0 {
  194. po.Width = maxInt(scaleInt(imgWidth, opts.Scale), 1)
  195. po.Height = maxInt(scaleInt(imgHeight, opts.Scale), 1)
  196. }
  197. if err := transformImage(context.Background(), wm, wmData.Data, po, wmData.Type); err != nil {
  198. return err
  199. }
  200. if err := wm.EnsureAlpha(); err != nil {
  201. return nil
  202. }
  203. if opts.Replicate {
  204. return wm.Replicate(imgWidth, imgHeight)
  205. }
  206. left, top := calcPosition(imgWidth, imgHeight, wm.Width(), wm.Height(), &opts.Gravity, true)
  207. return wm.Embed(imgWidth, imgHeight, left, top, rgbColor{0, 0, 0})
  208. }
  209. func applyWatermark(img *vipsImage, wmData *imageData, opts *watermarkOptions, framesCount int) error {
  210. if err := img.RgbColourspace(); err != nil {
  211. return err
  212. }
  213. if err := img.CopyMemory(); err != nil {
  214. return err
  215. }
  216. wm := new(vipsImage)
  217. defer wm.Clear()
  218. width := img.Width()
  219. height := img.Height()
  220. if err := prepareWatermark(wm, wmData, opts, width, height/framesCount); err != nil {
  221. return err
  222. }
  223. if framesCount > 1 {
  224. if err := wm.Replicate(width, height); err != nil {
  225. return err
  226. }
  227. }
  228. opacity := opts.Opacity * conf.WatermarkOpacity
  229. return img.ApplyWatermark(wm, opacity)
  230. }
  231. func transformImage(ctx context.Context, img *vipsImage, data []byte, po *processingOptions, imgtype imageType) error {
  232. var (
  233. err error
  234. trimmed bool
  235. )
  236. if po.Trim.Enabled {
  237. if err = img.Trim(po.Trim.Threshold, po.Trim.Smart, po.Trim.Color, po.Trim.EqualHor, po.Trim.EqualVer); err != nil {
  238. return err
  239. }
  240. trimmed = true
  241. }
  242. srcWidth, srcHeight, angle, flip := extractMeta(img)
  243. cropWidth, cropHeight := po.Crop.Width, po.Crop.Height
  244. cropGravity := po.Crop.Gravity
  245. if cropGravity.Type == gravityUnknown {
  246. cropGravity = po.Gravity
  247. }
  248. widthToScale := minNonZeroInt(cropWidth, srcWidth)
  249. heightToScale := minNonZeroInt(cropHeight, srcHeight)
  250. scale := calcScale(widthToScale, heightToScale, po, imgtype)
  251. cropWidth = scaleInt(cropWidth, scale)
  252. cropHeight = scaleInt(cropHeight, scale)
  253. if cropGravity.Type != gravityFocusPoint {
  254. cropGravity.X *= scale
  255. cropGravity.Y *= scale
  256. }
  257. if !trimmed && scale != 1 && data != nil && canScaleOnLoad(imgtype, scale) {
  258. if imgtype == imageTypeWEBP || imgtype == imageTypeSVG {
  259. // Do some scale-on-load
  260. if err = img.Load(data, imgtype, 1, scale, 1); err != nil {
  261. return err
  262. }
  263. } else if imgtype == imageTypeJPEG {
  264. // Do some shrink-on-load
  265. if shrink := calcJpegShink(scale, imgtype); shrink != 1 {
  266. if err = img.Load(data, imgtype, shrink, 1.0, 1); err != nil {
  267. return err
  268. }
  269. }
  270. }
  271. // Update scale after scale-on-load
  272. newWidth, newHeight, _, _ := extractMeta(img)
  273. widthToScale = scaleInt(widthToScale, float64(newWidth)/float64(srcWidth))
  274. heightToScale = scaleInt(heightToScale, float64(newHeight)/float64(srcHeight))
  275. scale = calcScale(widthToScale, heightToScale, po, imgtype)
  276. }
  277. if err = img.Rad2Float(); err != nil {
  278. return err
  279. }
  280. iccImported := false
  281. convertToLinear := conf.UseLinearColorspace && (scale != 1 || po.Dpr != 1)
  282. if convertToLinear || !img.IsSRGB() {
  283. if err = img.ImportColourProfile(true); err != nil {
  284. return err
  285. }
  286. iccImported = true
  287. }
  288. if convertToLinear {
  289. if err = img.LinearColourspace(); err != nil {
  290. return err
  291. }
  292. } else {
  293. if err = img.RgbColourspace(); err != nil {
  294. return err
  295. }
  296. }
  297. hasAlpha := img.HasAlpha()
  298. if scale != 1 {
  299. if err = img.Resize(scale, hasAlpha); err != nil {
  300. return err
  301. }
  302. }
  303. checkTimeout(ctx)
  304. if angle != vipsAngleD0 || flip {
  305. if err = img.CopyMemory(); err != nil {
  306. return err
  307. }
  308. if angle != vipsAngleD0 {
  309. if err = img.Rotate(angle); err != nil {
  310. return err
  311. }
  312. }
  313. if flip {
  314. if err = img.Flip(); err != nil {
  315. return err
  316. }
  317. }
  318. }
  319. checkTimeout(ctx)
  320. dprWidth := scaleInt(po.Width, po.Dpr)
  321. dprHeight := scaleInt(po.Height, po.Dpr)
  322. if err = cropImage(img, cropWidth, cropHeight, &cropGravity); err != nil {
  323. return err
  324. }
  325. if err = cropImage(img, dprWidth, dprHeight, &po.Gravity); err != nil {
  326. return err
  327. }
  328. checkTimeout(ctx)
  329. if !iccImported {
  330. if err = img.ImportColourProfile(false); err != nil {
  331. return err
  332. }
  333. }
  334. if err = img.RgbColourspace(); err != nil {
  335. return err
  336. }
  337. if hasAlpha && (po.Flatten || po.Format == imageTypeJPEG) {
  338. if err = img.Flatten(po.Background); err != nil {
  339. return err
  340. }
  341. }
  342. if po.Blur > 0 {
  343. if err = img.Blur(po.Blur); err != nil {
  344. return err
  345. }
  346. }
  347. if po.Sharpen > 0 {
  348. if err = img.Sharpen(po.Sharpen); err != nil {
  349. return err
  350. }
  351. }
  352. if po.Extend.Enabled && (po.Width > img.Width() || po.Height > img.Height()) {
  353. offX, offY := calcPosition(po.Width, po.Height, img.Width(), img.Height(), &po.Extend.Gravity, false)
  354. if err = img.Embed(po.Width, po.Height, offX, offY, po.Background); err != nil {
  355. return err
  356. }
  357. }
  358. checkTimeout(ctx)
  359. if po.Watermark.Enabled && watermark != nil {
  360. if err = applyWatermark(img, watermark, &po.Watermark, 1); err != nil {
  361. return err
  362. }
  363. }
  364. return img.RgbColourspace()
  365. }
  366. func transformAnimated(ctx context.Context, img *vipsImage, data []byte, po *processingOptions, imgtype imageType) error {
  367. if po.Trim.Enabled {
  368. logWarning("Trim is not supported for animated images")
  369. po.Trim.Enabled = false
  370. }
  371. imgWidth := img.Width()
  372. frameHeight, err := img.GetInt("page-height")
  373. if err != nil {
  374. return err
  375. }
  376. framesCount := minInt(img.Height()/frameHeight, conf.MaxAnimationFrames)
  377. // Double check dimensions because animated image has many frames
  378. if err = checkDimensions(imgWidth, frameHeight*framesCount); err != nil {
  379. return err
  380. }
  381. // Vips 8.8+ supports n-pages and doesn't load the whole animated image on header access
  382. if nPages, _ := img.GetInt("n-pages"); nPages > 0 {
  383. scale := 1.0
  384. // Don't do scale on load if we need to crop
  385. if po.Crop.Width == 0 && po.Crop.Height == 0 {
  386. scale = calcScale(imgWidth, frameHeight, po, imgtype)
  387. }
  388. if nPages > framesCount || canScaleOnLoad(imgtype, scale) {
  389. logNotice("Animated scale on load")
  390. // Do some scale-on-load and load only the needed frames
  391. if err = img.Load(data, imgtype, 1, scale, framesCount); err != nil {
  392. return err
  393. }
  394. }
  395. imgWidth = img.Width()
  396. frameHeight, err = img.GetInt("page-height")
  397. if err != nil {
  398. return err
  399. }
  400. }
  401. delay, err := img.GetInt("gif-delay")
  402. if err != nil {
  403. return err
  404. }
  405. loop, err := img.GetInt("gif-loop")
  406. if err != nil {
  407. return err
  408. }
  409. frames := make([]*vipsImage, framesCount)
  410. defer func() {
  411. for _, frame := range frames {
  412. frame.Clear()
  413. }
  414. }()
  415. watermarkEnabled := po.Watermark.Enabled
  416. po.Watermark.Enabled = false
  417. defer func() { po.Watermark.Enabled = watermarkEnabled }()
  418. var errg errgroup.Group
  419. for i := 0; i < framesCount; i++ {
  420. ind := i
  421. errg.Go(func() error {
  422. frame := new(vipsImage)
  423. if err = img.Extract(frame, 0, ind*frameHeight, imgWidth, frameHeight); err != nil {
  424. return err
  425. }
  426. if err = transformImage(ctx, frame, nil, po, imgtype); err != nil {
  427. return err
  428. }
  429. frames[ind] = frame
  430. return nil
  431. })
  432. }
  433. if err = errg.Wait(); err != nil {
  434. return err
  435. }
  436. checkTimeout(ctx)
  437. if err = img.Arrayjoin(frames); err != nil {
  438. return err
  439. }
  440. if watermarkEnabled && watermark != nil {
  441. if err = applyWatermark(img, watermark, &po.Watermark, framesCount); err != nil {
  442. return err
  443. }
  444. }
  445. img.SetInt("page-height", frames[0].Height())
  446. img.SetInt("gif-delay", delay)
  447. img.SetInt("gif-loop", loop)
  448. img.SetInt("n-pages", framesCount)
  449. return nil
  450. }
  451. func getIcoData(imgdata *imageData) (*imageData, error) {
  452. icoMeta, err := imagemeta.DecodeIcoMeta(bytes.NewReader(imgdata.Data))
  453. if err != nil {
  454. return nil, err
  455. }
  456. offset := icoMeta.BestImageOffset()
  457. size := icoMeta.BestImageSize()
  458. data := imgdata.Data[offset : offset+size]
  459. var format string
  460. meta, err := imagemeta.DecodeMeta(bytes.NewReader(data))
  461. if err != nil {
  462. // Looks like it's BMP with an incomplete header
  463. if d, err := imagemeta.FixBmpHeader(data); err == nil {
  464. format = "bmp"
  465. data = d
  466. } else {
  467. return nil, err
  468. }
  469. } else {
  470. format = meta.Format()
  471. }
  472. if imgtype, ok := imageTypes[format]; ok && vipsTypeSupportLoad[imgtype] {
  473. return &imageData{
  474. Data: data,
  475. Type: imgtype,
  476. }, nil
  477. }
  478. return nil, fmt.Errorf("Can't load %s from ICO", meta.Format())
  479. }
  480. func saveImageToFitBytes(po *processingOptions, img *vipsImage) ([]byte, context.CancelFunc, error) {
  481. var diff float64
  482. quality := po.Quality
  483. img.CopyMemory()
  484. for {
  485. result, cancel, err := img.Save(po.Format, quality, conf.StripMetadata)
  486. if len(result) <= po.MaxBytes || quality <= 10 || err != nil {
  487. return result, cancel, err
  488. }
  489. cancel()
  490. delta := float64(len(result)) / float64(po.MaxBytes)
  491. switch {
  492. case delta > 3:
  493. diff = 0.25
  494. case delta > 1.5:
  495. diff = 0.5
  496. default:
  497. diff = 0.75
  498. }
  499. quality = int(float64(quality) * diff)
  500. }
  501. }
  502. func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
  503. runtime.LockOSThread()
  504. defer runtime.UnlockOSThread()
  505. if newRelicEnabled {
  506. newRelicCancel := startNewRelicSegment(ctx, "Processing image")
  507. defer newRelicCancel()
  508. }
  509. if prometheusEnabled {
  510. defer startPrometheusDuration(prometheusProcessingDuration)()
  511. }
  512. defer vipsCleanup()
  513. po := getProcessingOptions(ctx)
  514. imgdata := getImageData(ctx)
  515. if po.Format == imageTypeUnknown {
  516. switch {
  517. case po.PreferWebP && imageTypeSaveSupport(imageTypeWEBP):
  518. po.Format = imageTypeWEBP
  519. case imageTypeSaveSupport(imgdata.Type) && imageTypeGoodForWeb(imgdata.Type):
  520. po.Format = imgdata.Type
  521. default:
  522. po.Format = imageTypeJPEG
  523. }
  524. } else if po.EnforceWebP && imageTypeSaveSupport(imageTypeWEBP) {
  525. po.Format = imageTypeWEBP
  526. }
  527. if po.Format == imageTypeSVG {
  528. if imgdata.Type != imageTypeSVG {
  529. return []byte{}, func() {}, errConvertingNonSvgToSvg
  530. }
  531. return imgdata.Data, func() {}, nil
  532. }
  533. if imgdata.Type == imageTypeSVG && !vipsTypeSupportLoad[imageTypeSVG] {
  534. return []byte{}, func() {}, errSourceImageTypeNotSupported
  535. }
  536. if imgdata.Type == imageTypeICO {
  537. icodata, err := getIcoData(imgdata)
  538. if err != nil {
  539. return nil, func() {}, err
  540. }
  541. imgdata = icodata
  542. }
  543. if !vipsSupportSmartcrop {
  544. if po.Gravity.Type == gravitySmart {
  545. logWarning(msgSmartCropNotSupported)
  546. po.Gravity.Type = gravityCenter
  547. }
  548. if po.Crop.Gravity.Type == gravitySmart {
  549. logWarning(msgSmartCropNotSupported)
  550. po.Crop.Gravity.Type = gravityCenter
  551. }
  552. }
  553. if po.ResizingType == resizeCrop {
  554. logWarning("`crop` resizing type is deprecated and will be removed in future versions. Use `crop` processing option instead")
  555. po.Crop.Width, po.Crop.Height = po.Width, po.Height
  556. po.ResizingType = resizeFit
  557. po.Width, po.Height = 0, 0
  558. }
  559. animationSupport := conf.MaxAnimationFrames > 1 && vipsSupportAnimation(imgdata.Type) && vipsSupportAnimation(po.Format)
  560. pages := 1
  561. if animationSupport {
  562. pages = -1
  563. }
  564. img := new(vipsImage)
  565. defer img.Clear()
  566. if err := img.Load(imgdata.Data, imgdata.Type, 1, 1.0, pages); err != nil {
  567. return nil, func() {}, err
  568. }
  569. if animationSupport && img.IsAnimated() {
  570. if err := transformAnimated(ctx, img, imgdata.Data, po, imgdata.Type); err != nil {
  571. return nil, func() {}, err
  572. }
  573. } else {
  574. if err := transformImage(ctx, img, imgdata.Data, po, imgdata.Type); err != nil {
  575. return nil, func() {}, err
  576. }
  577. }
  578. checkTimeout(ctx)
  579. if po.Format == imageTypeGIF {
  580. if err := img.CastUchar(); err != nil {
  581. return nil, func() {}, err
  582. }
  583. checkTimeout(ctx)
  584. }
  585. if po.MaxBytes > 0 && canFitToBytes(po.Format) {
  586. return saveImageToFitBytes(po, img)
  587. }
  588. return img.Save(po.Format, po.Quality, conf.StripMetadata)
  589. }