process.go 18 KB

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