process.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package main
  2. /*
  3. #cgo pkg-config: vips
  4. #cgo LDFLAGS: -s -w
  5. #include "vips.h"
  6. */
  7. import "C"
  8. import (
  9. "errors"
  10. "log"
  11. "math"
  12. "os"
  13. "runtime"
  14. "unsafe"
  15. )
  16. type imageType int
  17. const (
  18. UNKNOWN imageType = iota
  19. JPEG
  20. PNG
  21. WEBP
  22. GIF
  23. )
  24. var imageTypes = map[string]imageType{
  25. "jpeg": JPEG,
  26. "jpg": JPEG,
  27. "png": PNG,
  28. "webp": WEBP,
  29. "gif": GIF,
  30. }
  31. type gravityType int
  32. const (
  33. CENTER gravityType = iota
  34. NORTH
  35. EAST
  36. SOUTH
  37. WEST
  38. SMART
  39. )
  40. var gravityTypes = map[string]gravityType{
  41. "ce": CENTER,
  42. "no": NORTH,
  43. "ea": EAST,
  44. "so": SOUTH,
  45. "we": WEST,
  46. "sm": SMART,
  47. }
  48. type resizeType int
  49. const (
  50. FIT resizeType = iota
  51. FILL
  52. CROP
  53. )
  54. var resizeTypes = map[string]resizeType{
  55. "fit": FIT,
  56. "fill": FILL,
  57. "crop": CROP,
  58. }
  59. type processingOptions struct {
  60. resize resizeType
  61. width int
  62. height int
  63. gravity gravityType
  64. enlarge bool
  65. format imageType
  66. }
  67. var vipsSupportSmartcrop bool
  68. func initVips() {
  69. runtime.LockOSThread()
  70. defer runtime.UnlockOSThread()
  71. if err := C.vips_initialize(); err != 0 {
  72. C.vips_shutdown()
  73. log.Fatalln("unable to start vips!")
  74. }
  75. C.vips_concurrency_set(1)
  76. C.vips_cache_set_max_mem(100 * 1024 * 1024) // 100Mb
  77. C.vips_cache_set_max(500)
  78. if len(os.Getenv("IMGPROXY_DEBUG_VIPS")) > 0 {
  79. C.vips_cache_set_dump(C.gboolean(1))
  80. C.vips_cache_set_trace(C.gboolean(1))
  81. }
  82. vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
  83. }
  84. func vipsTypeSupportedLoad(imgtype imageType) bool {
  85. switch imgtype {
  86. case JPEG:
  87. return int(C.vips_type_find_load_go(C.JPEG)) != 0
  88. case PNG:
  89. return int(C.vips_type_find_load_go(C.PNG)) != 0
  90. case WEBP:
  91. return int(C.vips_type_find_load_go(C.WEBP)) != 0
  92. case GIF:
  93. return int(C.vips_type_find_load_go(C.GIF)) != 0
  94. }
  95. return false
  96. }
  97. func vipsTypeSupportedSave(imgtype imageType) bool {
  98. switch imgtype {
  99. case JPEG:
  100. return int(C.vips_type_find_save_go(C.JPEG)) != 0
  101. case PNG:
  102. return int(C.vips_type_find_save_go(C.PNG)) != 0
  103. case WEBP:
  104. return int(C.vips_type_find_save_go(C.WEBP)) != 0
  105. }
  106. return false
  107. }
  108. func round(f float64) int {
  109. return int(f + .5)
  110. }
  111. func calcScale(width, height int, po processingOptions) float64 {
  112. if (po.width == width && po.height == height) || (po.resize != FILL && po.resize != FIT) {
  113. return 1
  114. }
  115. fsw, fsh, fow, foh := float64(width), float64(height), float64(po.width), float64(po.height)
  116. wr := fow / fsw
  117. hr := foh / fsh
  118. if po.resize == FIT {
  119. return math.Min(wr, hr)
  120. }
  121. return math.Max(wr, hr)
  122. }
  123. func calcCrop(width, height int, po processingOptions) (left, top int) {
  124. left = (width - po.width + 1) / 2
  125. top = (height - po.height + 1) / 2
  126. if po.gravity == NORTH {
  127. top = 0
  128. }
  129. if po.gravity == EAST {
  130. left = width - po.width
  131. }
  132. if po.gravity == SOUTH {
  133. top = height - po.height
  134. }
  135. if po.gravity == WEST {
  136. left = 0
  137. }
  138. return
  139. }
  140. func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte, error) {
  141. defer keepAlive(data)
  142. err := C.int(0)
  143. var img *C.struct__VipsImage
  144. defer C.clear_image(&img)
  145. defer C.vips_cleanup()
  146. // Load the image
  147. switch imgtype {
  148. case JPEG:
  149. err = C.vips_jpegload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img)
  150. case PNG:
  151. err = C.vips_pngload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img)
  152. case GIF:
  153. err = C.vips_gifload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img)
  154. case WEBP:
  155. err = C.vips_webpload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img)
  156. }
  157. if err != 0 {
  158. return nil, vipsError()
  159. }
  160. imgWidth := int(img.Xsize)
  161. imgHeight := int(img.Ysize)
  162. // Ensure we won't crop out of bounds
  163. if !po.enlarge || po.resize == CROP {
  164. if imgWidth < po.width {
  165. po.width = imgWidth
  166. }
  167. if imgHeight < po.height {
  168. po.height = imgHeight
  169. }
  170. }
  171. if po.width != imgWidth || po.height != imgHeight {
  172. var (
  173. pResize, pCrop int
  174. pScale float64
  175. pSmart int
  176. pLeft, pTop, pWidth, pHeight int
  177. )
  178. if po.resize == FILL || po.resize == FIT {
  179. pResize = 1
  180. pScale = calcScale(imgWidth, imgHeight, po)
  181. } else {
  182. pScale = 1.0
  183. }
  184. if po.resize == FILL || po.resize == CROP {
  185. pCrop = 1
  186. pWidth, pHeight = po.width, po.height
  187. if po.gravity == SMART && vipsSupportSmartcrop {
  188. pSmart = 1
  189. } else {
  190. pLeft, pTop = calcCrop(round(float64(imgWidth)*pScale), round(float64(imgHeight)*pScale), po)
  191. }
  192. }
  193. err = C.vips_process_image(&img, C.int(pResize), C.double(pScale), C.int(pCrop), C.int(pSmart), C.int(pLeft), C.int(pTop), C.int(pWidth), C.int(pHeight))
  194. if err != 0 {
  195. return nil, vipsError()
  196. }
  197. }
  198. // Finally, save
  199. var ptr unsafe.Pointer
  200. defer C.g_free(C.gpointer(ptr))
  201. imgsize := C.size_t(0)
  202. switch po.format {
  203. case JPEG:
  204. err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
  205. case PNG:
  206. err = C.vips_pngsave_go(img, &ptr, &imgsize)
  207. case WEBP:
  208. err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality))
  209. }
  210. if err != 0 {
  211. return nil, vipsError()
  212. }
  213. buf := C.GoBytes(ptr, C.int(imgsize))
  214. return buf, nil
  215. }
  216. func vipsError() error {
  217. return errors.New(C.GoString(C.vips_error_buffer()))
  218. }