process.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. checkTimeout(ctx)
  330. if !iccImported {
  331. if err = img.ImportColourProfile(false); err != nil {
  332. return err
  333. }
  334. }
  335. if err = img.RgbColourspace(); err != nil {
  336. return err
  337. }
  338. transparrentBg := po.Format.SupportsAlpha() && !po.Flatten
  339. if hasAlpha && !transparrentBg {
  340. if err = img.Flatten(po.Background); err != nil {
  341. return err
  342. }
  343. }
  344. if err = img.CopyMemory(); err != nil {
  345. return err
  346. }
  347. checkTimeout(ctx)
  348. if po.Blur > 0 {
  349. if err = img.Blur(po.Blur); err != nil {
  350. return err
  351. }
  352. }
  353. if po.Sharpen > 0 {
  354. if err = img.Sharpen(po.Sharpen); err != nil {
  355. return err
  356. }
  357. }
  358. if po.Extend.Enabled && (po.Width > img.Width() || po.Height > img.Height()) {
  359. offX, offY := calcPosition(po.Width, po.Height, img.Width(), img.Height(), &po.Extend.Gravity, false)
  360. if err = img.Embed(po.Width, po.Height, offX, offY, po.Background, transparrentBg); err != nil {
  361. return err
  362. }
  363. }
  364. if po.Padding.Enabled {
  365. paddingTop := scaleInt(po.Padding.Top, po.Dpr)
  366. paddingRight := scaleInt(po.Padding.Right, po.Dpr)
  367. paddingBottom := scaleInt(po.Padding.Bottom, po.Dpr)
  368. paddingLeft := scaleInt(po.Padding.Left, po.Dpr)
  369. if err = img.Embed(
  370. img.Width()+paddingLeft+paddingRight,
  371. img.Height()+paddingTop+paddingBottom,
  372. paddingLeft,
  373. paddingTop,
  374. po.Background,
  375. transparrentBg,
  376. ); err != nil {
  377. return err
  378. }
  379. }
  380. checkTimeout(ctx)
  381. if po.Watermark.Enabled && watermark != nil {
  382. if err = applyWatermark(img, watermark, &po.Watermark, 1); err != nil {
  383. return err
  384. }
  385. }
  386. return img.RgbColourspace()
  387. }
  388. func transformAnimated(ctx context.Context, img *vipsImage, data []byte, po *processingOptions, imgtype imageType) error {
  389. if po.Trim.Enabled {
  390. logWarning("Trim is not supported for animated images")
  391. po.Trim.Enabled = false
  392. }
  393. imgWidth := img.Width()
  394. frameHeight, err := img.GetInt("page-height")
  395. if err != nil {
  396. return err
  397. }
  398. framesCount := minInt(img.Height()/frameHeight, conf.MaxAnimationFrames)
  399. // Double check dimensions because animated image has many frames
  400. if err = checkDimensions(imgWidth, frameHeight*framesCount); err != nil {
  401. return err
  402. }
  403. // Vips 8.8+ supports n-pages and doesn't load the whole animated image on header access
  404. if nPages, _ := img.GetInt("n-pages"); nPages > 0 {
  405. scale := 1.0
  406. // Don't do scale on load if we need to crop
  407. if po.Crop.Width == 0 && po.Crop.Height == 0 {
  408. scale = calcScale(imgWidth, frameHeight, po, imgtype)
  409. }
  410. if nPages > framesCount || canScaleOnLoad(imgtype, scale) {
  411. logNotice("Animated scale on load")
  412. // Do some scale-on-load and load only the needed frames
  413. if err = img.Load(data, imgtype, 1, scale, framesCount); err != nil {
  414. return err
  415. }
  416. }
  417. imgWidth = img.Width()
  418. frameHeight, err = img.GetInt("page-height")
  419. if err != nil {
  420. return err
  421. }
  422. }
  423. delay, err := img.GetInt("gif-delay")
  424. if err != nil {
  425. return err
  426. }
  427. loop, err := img.GetInt("gif-loop")
  428. if err != nil {
  429. return err
  430. }
  431. watermarkEnabled := po.Watermark.Enabled
  432. po.Watermark.Enabled = false
  433. defer func() { po.Watermark.Enabled = watermarkEnabled }()
  434. frames := make([]*vipsImage, framesCount)
  435. defer func() {
  436. for _, frame := range frames {
  437. if frame != nil {
  438. frame.Clear()
  439. }
  440. }
  441. }()
  442. for i := 0; i < framesCount; i++ {
  443. frame := new(vipsImage)
  444. if err = img.Extract(frame, 0, i*frameHeight, imgWidth, frameHeight); err != nil {
  445. return err
  446. }
  447. frames[i] = frame
  448. if err = transformImage(ctx, frame, nil, po, imgtype); err != nil {
  449. return err
  450. }
  451. frame.CopyMemory()
  452. }
  453. checkTimeout(ctx)
  454. if err = img.Arrayjoin(frames); err != nil {
  455. return err
  456. }
  457. if watermarkEnabled && watermark != nil {
  458. if err = applyWatermark(img, watermark, &po.Watermark, framesCount); err != nil {
  459. return err
  460. }
  461. }
  462. img.SetInt("page-height", frames[0].Height())
  463. img.SetInt("gif-delay", delay)
  464. img.SetInt("gif-loop", loop)
  465. img.SetInt("n-pages", framesCount)
  466. return nil
  467. }
  468. func getIcoData(imgdata *imageData) (*imageData, error) {
  469. icoMeta, err := imagemeta.DecodeIcoMeta(bytes.NewReader(imgdata.Data))
  470. if err != nil {
  471. return nil, err
  472. }
  473. offset := icoMeta.BestImageOffset()
  474. size := icoMeta.BestImageSize()
  475. data := imgdata.Data[offset : offset+size]
  476. var format string
  477. meta, err := imagemeta.DecodeMeta(bytes.NewReader(data))
  478. if err != nil {
  479. // Looks like it's BMP with an incomplete header
  480. if d, err := imagemeta.FixBmpHeader(data); err == nil {
  481. format = "bmp"
  482. data = d
  483. } else {
  484. return nil, err
  485. }
  486. } else {
  487. format = meta.Format()
  488. }
  489. if imgtype, ok := imageTypes[format]; ok && vipsTypeSupportLoad[imgtype] {
  490. return &imageData{
  491. Data: data,
  492. Type: imgtype,
  493. }, nil
  494. }
  495. return nil, fmt.Errorf("Can't load %s from ICO", meta.Format())
  496. }
  497. func saveImageToFitBytes(po *processingOptions, img *vipsImage) ([]byte, context.CancelFunc, error) {
  498. var diff float64
  499. quality := po.Quality
  500. img.CopyMemory()
  501. for {
  502. result, cancel, err := img.Save(po.Format, quality, po.StripMetadata)
  503. if len(result) <= po.MaxBytes || quality <= 10 || err != nil {
  504. return result, cancel, err
  505. }
  506. cancel()
  507. delta := float64(len(result)) / float64(po.MaxBytes)
  508. switch {
  509. case delta > 3:
  510. diff = 0.25
  511. case delta > 1.5:
  512. diff = 0.5
  513. default:
  514. diff = 0.75
  515. }
  516. quality = int(float64(quality) * diff)
  517. }
  518. }
  519. func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
  520. runtime.LockOSThread()
  521. defer runtime.UnlockOSThread()
  522. if newRelicEnabled {
  523. newRelicCancel := startNewRelicSegment(ctx, "Processing image")
  524. defer newRelicCancel()
  525. }
  526. if prometheusEnabled {
  527. defer startPrometheusDuration(prometheusProcessingDuration)()
  528. }
  529. defer vipsCleanup()
  530. po := getProcessingOptions(ctx)
  531. imgdata := getImageData(ctx)
  532. if po.Format == imageTypeUnknown {
  533. switch {
  534. case po.PreferWebP && imageTypeSaveSupport(imageTypeWEBP):
  535. po.Format = imageTypeWEBP
  536. case imageTypeSaveSupport(imgdata.Type) && imageTypeGoodForWeb(imgdata.Type):
  537. po.Format = imgdata.Type
  538. default:
  539. po.Format = imageTypeJPEG
  540. }
  541. } else if po.EnforceWebP && imageTypeSaveSupport(imageTypeWEBP) {
  542. po.Format = imageTypeWEBP
  543. }
  544. if po.Format == imageTypeSVG {
  545. if imgdata.Type != imageTypeSVG {
  546. return []byte{}, func() {}, errConvertingNonSvgToSvg
  547. }
  548. return imgdata.Data, func() {}, nil
  549. }
  550. if imgdata.Type == imageTypeSVG && !vipsTypeSupportLoad[imageTypeSVG] {
  551. return []byte{}, func() {}, errSourceImageTypeNotSupported
  552. }
  553. if imgdata.Type == imageTypeICO {
  554. icodata, err := getIcoData(imgdata)
  555. if err != nil {
  556. return nil, func() {}, err
  557. }
  558. imgdata = icodata
  559. }
  560. if !vipsSupportSmartcrop {
  561. if po.Gravity.Type == gravitySmart {
  562. logWarning(msgSmartCropNotSupported)
  563. po.Gravity.Type = gravityCenter
  564. }
  565. if po.Crop.Gravity.Type == gravitySmart {
  566. logWarning(msgSmartCropNotSupported)
  567. po.Crop.Gravity.Type = gravityCenter
  568. }
  569. }
  570. if po.ResizingType == resizeCrop {
  571. logWarning("`crop` resizing type is deprecated and will be removed in future versions. Use `crop` processing option instead")
  572. po.Crop.Width, po.Crop.Height = po.Width, po.Height
  573. po.ResizingType = resizeFit
  574. po.Width, po.Height = 0, 0
  575. }
  576. if po.Format == imageTypeWEBP {
  577. webpLimitShrink := float64(maxInt(po.Width, po.Height)) * po.Dpr / webpMaxDimension
  578. if webpLimitShrink > 1.0 {
  579. po.Width = int(float64(po.Width) / webpLimitShrink)
  580. po.Height = int(float64(po.Height) / webpLimitShrink)
  581. logWarning("WebP dimension size is limited to %d. Requested dimensions are rescaled to %dx%d", int(webpMaxDimension), po.Width, po.Height)
  582. }
  583. }
  584. animationSupport := conf.MaxAnimationFrames > 1 && vipsSupportAnimation(imgdata.Type) && vipsSupportAnimation(po.Format)
  585. pages := 1
  586. if animationSupport {
  587. pages = -1
  588. }
  589. img := new(vipsImage)
  590. defer img.Clear()
  591. if err := img.Load(imgdata.Data, imgdata.Type, 1, 1.0, pages); err != nil {
  592. return nil, func() {}, err
  593. }
  594. if animationSupport && img.IsAnimated() {
  595. if err := transformAnimated(ctx, img, imgdata.Data, po, imgdata.Type); err != nil {
  596. return nil, func() {}, err
  597. }
  598. } else {
  599. if err := transformImage(ctx, img, imgdata.Data, po, imgdata.Type); err != nil {
  600. return nil, func() {}, err
  601. }
  602. }
  603. checkTimeout(ctx)
  604. if po.Format == imageTypeGIF {
  605. if err := img.CastUchar(); err != nil {
  606. return nil, func() {}, err
  607. }
  608. checkTimeout(ctx)
  609. }
  610. if po.MaxBytes > 0 && canFitToBytes(po.Format) {
  611. return saveImageToFitBytes(po, img)
  612. }
  613. return img.Save(po.Format, po.Quality, po.StripMetadata)
  614. }