Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 115 lines (104 sloc) 2.641 kb
0421968 Erik Unger more media and mongomedia
authored
1 package media
2
3 import (
4e5fbdd Erik Unger media image resize
authored
4 "errors"
0421968 Erik Unger more media and mongomedia
authored
5 "github.com/ungerik/go-start/model"
b02ee5e Erik Unger medialib image resizing
authored
6 "github.com/ungerik/go-start/view"
7 "image"
8 "image/jpeg"
9 "image/png"
0421968 Erik Unger more media and mongomedia
authored
10 )
11
ae21dd2 Erik Unger media stuff
authored
12 func newImageVersion(filename, contentType string, sourceRect image.Rectangle, width, height int, grayscale bool) ImageVersion {
13 version := ImageVersion{
14 Filename: model.String(filename),
15 ContentType: model.String(contentType),
16 Width: model.Int(width),
17 Height: model.Int(height),
18 Grayscale: model.Bool(grayscale),
19 }
20 version.SourceRect.SetRectangle(sourceRect)
21 return version
22 }
23
0421968 Erik Unger more media and mongomedia
authored
24 type ImageVersion struct {
ae21dd2 Erik Unger media stuff
authored
25 image *Image
6572821 Erik Unger media image version selection
authored
26 ID model.String `bson:",omitempty"`
27 Filename model.String
28 ContentType model.String
29 SourceRect ModelRect
30 OutsideColor model.Color
31 Width model.Int
32 Height model.Int
33 Grayscale model.Bool
0421968 Erik Unger more media and mongomedia
authored
34 }
35
36 func (self *ImageVersion) URL() string {
37 return View.URL(self.ID.Get(), self.Filename.Get())
38 }
a1b16be Erik Unger fixed build error
authored
39
4e5fbdd Erik Unger media image resize
authored
40 // AspectRatio returns Width / Height
41 func (self *ImageVersion) AspectRatio() float64 {
42 return float64(self.Width) / float64(self.Height)
43 }
44
45 func (self *ImageVersion) SaveImageData(data []byte) error {
46 writer, err := Config.Backend.ImageVersionWriter(self)
47 if err != nil {
48 return err
49 }
50 _, err = writer.Write(data)
51 if err != nil {
52 writer.Close()
53 return err
54 }
55 return writer.Close()
56 }
57
58 func (self *ImageVersion) SaveImage(im image.Image) error {
59 writer, err := Config.Backend.ImageVersionWriter(self)
60 if err != nil {
61 return err
62 }
63 switch self.ContentType {
64 case "image/jpeg":
65 err = jpeg.Encode(writer, im, nil)
66 case "image/png":
67 err = png.Encode(writer, im)
68 default:
69 return errors.New("Can't save content-type: " + self.ContentType.Get())
70 }
71 if err != nil {
72 writer.Close()
73 return err
74 }
75 return writer.Close()
a1b16be Erik Unger fixed build error
authored
76 }
77
78 func (self *ImageVersion) LoadImage() (image.Image, error) {
4e5fbdd Erik Unger media image resize
authored
79 reader, _, err := Config.Backend.ImageVersionReader(self.ID.Get())
80 if err != nil {
81 return nil, err
82 }
83 im, _, err := image.Decode(reader)
84 if err != nil {
85 reader.Close()
86 return nil, err
87 }
88 err = reader.Close()
89 if err != nil {
90 return nil, err
91 }
92 return im, nil
a1b16be Erik Unger fixed build error
authored
93 }
ae21dd2 Erik Unger media stuff
authored
94
95 func (self *ImageVersion) ViewImage(class string) *view.Image {
96 return &view.Image{
97 URL: self.URL(),
98 Width: self.Width.GetInt(),
99 Height: self.Height.GetInt(),
100 Description: self.image.Description.Get(),
101 Class: class,
102 }
103 }
104
105 func (self *ImageVersion) ViewImageLink(imageClass, linkClass string) *view.Link {
106 return &view.Link{
107 Model: &view.StringLink{
108 Url: self.image.Link.Get(),
109 Title: self.image.Description.Get(),
110 Content: self.ViewImage(imageClass),
111 },
112 Class: linkClass,
113 }
114 }
Something went wrong with that request. Please try again.