psd.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package renderer
  2. import (
  3. "errors"
  4. "image"
  5. "image/jpeg"
  6. "os"
  7. "path/filepath"
  8. "github.com/nfnt/resize"
  9. "github.com/oliamb/cutter"
  10. _ "github.com/oov/psd"
  11. )
  12. func generateThumbnailForPSD(inputFile string, outputFolder string) error {
  13. if _, err := os.Stat(inputFile); os.IsNotExist(err) {
  14. return errors.New("Input file does not exist")
  15. }
  16. outputFile := filepath.Join(outputFolder, filepath.Base(inputFile)+".jpg")
  17. f, err := os.Open(outputFile)
  18. if err != nil {
  19. return err
  20. }
  21. //Decode the image content with PSD decoder
  22. img, _, err := image.Decode(f)
  23. if err != nil {
  24. return err
  25. }
  26. f.Close()
  27. //Check boundary to decide resize mode
  28. b := img.Bounds()
  29. imgWidth := b.Max.X
  30. imgHeight := b.Max.Y
  31. var m image.Image
  32. if imgWidth > imgHeight {
  33. m = resize.Resize(0, 480, img, resize.Lanczos3)
  34. } else {
  35. m = resize.Resize(480, 0, img, resize.Lanczos3)
  36. }
  37. //Crop out the center
  38. croppedImg, err := cutter.Crop(m, cutter.Config{
  39. Width: 480,
  40. Height: 480,
  41. Mode: cutter.Centered,
  42. })
  43. outf, err := os.Create(outputFile)
  44. if err != nil {
  45. return err
  46. }
  47. opt := jpeg.Options{
  48. Quality: 90,
  49. }
  50. err = jpeg.Encode(outf, croppedImg, &opt)
  51. if err != nil {
  52. return err
  53. }
  54. outf.Close()
  55. return nil
  56. }