vips.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package vips
  2. import (
  3. "bytes"
  4. "io"
  5. "path"
  6. "strings"
  7. "github.com/disintegration/imaging"
  8. "github.com/qor5/admin/media"
  9. "github.com/theplant/bimg"
  10. )
  11. var (
  12. EnableGenerateWebp = false
  13. PNGtoWebpQuality = 90
  14. JPEGtoWebpQuality = 80
  15. GIFtoWebpQuality = 85
  16. JPEGQuality = 80
  17. PNGQuality = 90
  18. PNGCompression = 9
  19. )
  20. type Config struct {
  21. EnableGenerateWebp bool
  22. PNGtoWebpQuality int
  23. JPEGtoWebpQuality int
  24. JPEGQuality int
  25. PNGQuality int
  26. PNGCompression int
  27. }
  28. type bimgImageHandler struct{}
  29. func (bimgImageHandler) CouldHandle(media media.Media) bool {
  30. return media.IsImage()
  31. }
  32. // Crop & Resize
  33. func (bimgImageHandler) Handle(m media.Media, file media.FileInterface, option *media.Option) (err error) {
  34. buffer := new(bytes.Buffer)
  35. if _, err := io.Copy(buffer, file); err != nil {
  36. return err
  37. }
  38. fileSizes := m.GetFileSizes()
  39. fileSizes["original"] = buffer.Len()
  40. // Auto Rotate by EXIF
  41. img := copyImage(buffer.Bytes())
  42. metaData, _ := img.Metadata()
  43. media.SetWeightHeight(m, metaData.Size.Width, metaData.Size.Height)
  44. if metaData.EXIF.Orientation > 1 {
  45. rotatedBuf, err := img.AutoRotate()
  46. if err != nil {
  47. return err
  48. }
  49. buffer.Reset()
  50. if _, err := io.Copy(buffer, bytes.NewBuffer(rotatedBuf)); err != nil {
  51. return err
  52. }
  53. }
  54. // Save Original Image
  55. {
  56. if err = m.Store(m.URL("original"), option, bytes.NewReader(buffer.Bytes())); err != nil {
  57. return err
  58. }
  59. img := copyImage(buffer.Bytes())
  60. if err = generateWebp(m, option, bimg.Options{}, img, "original"); err != nil {
  61. return err
  62. }
  63. }
  64. // TODO support crop gif
  65. if isGif(m.URL()) {
  66. if err = m.Store(m.URL(), option, file); err != nil {
  67. return err
  68. }
  69. img := copyImage(buffer.Bytes())
  70. bimgOption := bimg.Options{Palette: true, Compression: PNGCompression}
  71. if err = generateWebp(m, option, bimgOption, img); err != nil {
  72. return err
  73. }
  74. for key, _ := range m.GetSizes() {
  75. if key == "original" {
  76. continue
  77. }
  78. img := copyImage(buffer.Bytes())
  79. if err = m.Store(m.URL(key), option, file); err != nil {
  80. return err
  81. }
  82. fileSizes[key] = buffer.Len()
  83. if err = generateWebp(m, option, bimgOption, img, key); err != nil {
  84. return err
  85. }
  86. }
  87. media.SetFileSizes(m, fileSizes)
  88. return
  89. }
  90. quality := getQualityByImageType(m.URL())
  91. // Handle default image
  92. {
  93. img := copyImage(buffer.Bytes())
  94. bimgOption := bimg.Options{Quality: quality, Palette: true, Compression: PNGCompression}
  95. // Crop original image if specified
  96. if cropOption := m.GetCropOption("original"); cropOption != nil {
  97. options := bimg.Options{
  98. Quality: 100, // Don't compress twice
  99. Top: cropOption.Min.Y,
  100. Left: cropOption.Min.X,
  101. AreaWidth: cropOption.Max.X - cropOption.Min.X,
  102. AreaHeight: cropOption.Max.Y - cropOption.Min.Y,
  103. }
  104. if options.Top == 0 && options.Left == 0 {
  105. options.Top = -1
  106. }
  107. if _, err := img.Process(options); err != nil {
  108. return err
  109. }
  110. }
  111. copy := copyImage(img.Image())
  112. if buf, err := img.Process(bimgOption); err == nil {
  113. if err = m.Store(m.URL(), option, bytes.NewReader(buf)); err != nil {
  114. return err
  115. }
  116. fileSizes[media.DefaultSizeKey] = len(buf)
  117. } else {
  118. return err
  119. }
  120. if err = generateWebp(m, option, bimgOption, copy); err != nil {
  121. return err
  122. }
  123. }
  124. // Handle size images
  125. for key, size := range m.GetSizes() {
  126. if key == "original" {
  127. continue
  128. }
  129. img := copyImage(buffer.Bytes())
  130. if cropOption := m.GetCropOption(key); cropOption != nil {
  131. options := bimg.Options{
  132. Quality: 100, // Don't compress twice
  133. Top: cropOption.Min.Y,
  134. Left: cropOption.Min.X,
  135. AreaWidth: cropOption.Max.X - cropOption.Min.X,
  136. AreaHeight: cropOption.Max.Y - cropOption.Min.Y,
  137. }
  138. if options.Top == 0 && options.Left == 0 {
  139. options.Top = -1
  140. }
  141. if _, err := img.Process(options); err != nil {
  142. return err
  143. }
  144. }
  145. copy := copyImage(img.Image())
  146. bimgOption := bimg.Options{
  147. Width: size.Width,
  148. Height: size.Height,
  149. Quality: quality,
  150. Compression: PNGCompression,
  151. Palette: true,
  152. Enlarge: true,
  153. }
  154. // Process & Save size image
  155. if buf, err := img.Process(bimgOption); err == nil {
  156. if err = m.Store(m.URL(key), option, bytes.NewReader(buf)); err != nil {
  157. return err
  158. }
  159. fileSizes[key] = len(buf)
  160. } else {
  161. return err
  162. }
  163. if err = generateWebp(m, option, bimgOption, copy, key); err != nil {
  164. return err
  165. }
  166. }
  167. media.SetFileSizes(m, fileSizes)
  168. return
  169. }
  170. func generateWebp(m media.Media, option *media.Option, bimgOption bimg.Options, img *bimg.Image, size ...string) (err error) {
  171. if !EnableGenerateWebp {
  172. return
  173. }
  174. bimgOption.Type = bimg.WEBP
  175. bimgOption.Quality = getWebpQualityByImageType(m.URL())
  176. if buf, err := img.Process(bimgOption); err == nil {
  177. url := m.URL(size...)
  178. ext := path.Ext(url)
  179. extArr := strings.Split(ext, "?")
  180. i := strings.LastIndex(url, ext)
  181. webpUrl := url[:i] + strings.Replace(url[i:], extArr[0], ".webp", 1)
  182. m.Store(webpUrl, option, bytes.NewReader(buf))
  183. } else {
  184. return err
  185. }
  186. return
  187. }
  188. func copyImage(buffer []byte) (img *bimg.Image) {
  189. bs := make([]byte, len(buffer))
  190. copy(bs, buffer)
  191. img = bimg.NewImage(bs)
  192. return
  193. }
  194. func getQualityByImageType(url string) int {
  195. imgType, err := media.GetImageFormat(url)
  196. if err != nil {
  197. return 0
  198. }
  199. switch *imgType {
  200. case imaging.JPEG:
  201. return JPEGQuality
  202. case imaging.PNG:
  203. return PNGQuality
  204. }
  205. return 0
  206. }
  207. func getWebpQualityByImageType(url string) int {
  208. imgType, err := media.GetImageFormat(url)
  209. if err != nil {
  210. return 0
  211. }
  212. switch *imgType {
  213. case imaging.JPEG:
  214. return JPEGtoWebpQuality
  215. case imaging.PNG:
  216. return PNGtoWebpQuality
  217. case imaging.GIF:
  218. return GIFtoWebpQuality
  219. }
  220. return 0
  221. }
  222. func isGif(url string) bool {
  223. imgType, err := media.GetImageFormat(url)
  224. return err == nil && *imgType == imaging.GIF
  225. }
  226. func UseVips(cfg Config) {
  227. if cfg.EnableGenerateWebp {
  228. EnableGenerateWebp = true
  229. }
  230. if cfg.JPEGtoWebpQuality > 0 && cfg.JPEGtoWebpQuality <= 100 {
  231. JPEGtoWebpQuality = cfg.JPEGtoWebpQuality
  232. }
  233. if cfg.PNGtoWebpQuality > 0 && cfg.PNGtoWebpQuality <= 100 {
  234. PNGtoWebpQuality = cfg.PNGtoWebpQuality
  235. }
  236. if cfg.JPEGQuality > 0 && cfg.JPEGQuality <= 100 {
  237. JPEGQuality = cfg.JPEGQuality
  238. }
  239. if cfg.PNGQuality > 0 && cfg.PNGQuality <= 100 {
  240. PNGQuality = cfg.PNGQuality
  241. }
  242. if cfg.PNGCompression > 0 && cfg.PNGCompression <= 9 {
  243. PNGCompression = cfg.PNGCompression
  244. }
  245. bimg.VipsCacheSetMax(0)
  246. bimg.VipsCacheSetMaxMem(0)
  247. media.RegisterMediaHandler("image_handler", bimgImageHandler{})
  248. }