ires 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c60cd16be066b1b90bb6f5245891d53fc4b85145
4
- data.tar.gz: 0ada5ab904e40c026a37cd7b6f97d0494ceaadb3
3
+ metadata.gz: b257d1482a34c83c67287636a7370c3531aba5f2
4
+ data.tar.gz: 55d3577bf53d61dd0e1fa9084f1b0fe5889b96a1
5
5
  SHA512:
6
- metadata.gz: a163c28c0d024e8cc28037f27b6f243c8676c219b204111775f9e7e8639e9a9e576ae5bd2acf7751d48cd92e7376a99defcf543121f8f59222545b3d38940d52
7
- data.tar.gz: d6130e58d10d07b0155f1719c506508f138cdbe8a83823affabedc23a539affd46c292f71c8beca4236d98de9386d1065c19d3efb62e969da67045cd30f1e86a
6
+ metadata.gz: 2aea5907a83563bf3cb4513ab58b8da397e40364e2b3048eb81171c5dbaed1fe48623d61bf21e4f12759161346a65de1d1efca10d0659afe6b0be798d39bd63e
7
+ data.tar.gz: 14471ef705b607b8277cccf03b68fa47a7b11de6c37cd9ac5b9d424525779c786eb3309451fd5ff361cd720932d99b0a046e038a97ef69d70a12f9495c883e29
data/README.md CHANGED
@@ -7,6 +7,8 @@
7
7
 
8
8
  ## Usage
9
9
 
10
+ ### View
11
+
10
12
  ```erb
11
13
  <!-- Usually -->
12
14
  <%= ires_tag( path: "image_01.jpg", width: 90, height: 120 ) %>
@@ -15,6 +17,13 @@
15
17
  <%= ires_tag( path: "http://example.com/image_02.jpg", width: 200, height: 200, mode: "crop", alt: "example image" ) %>
16
18
  ```
17
19
 
20
+ ### Get resize path
21
+
22
+ ```ruby
23
+ Ires::Service.path( path: "<FULL IMAGE PATH>", width: 400, height: 300)
24
+ => /ires/<resize image path>
25
+ ```
26
+
18
27
  ### Select mode
19
28
 
20
29
  | info |    mode     |
data/ext/ires/image.go CHANGED
@@ -19,36 +19,36 @@ import (
19
19
 
20
20
 
21
21
  // Input image
22
- func InputImage(i *Ires) (image.Image, string, bool) {
22
+ func inputImage(i *Ires) (image.Image, string, bool) {
23
23
  if i.IsLocal {
24
- img, format := LocalImage(i.Uri)
24
+ img, format := localImage(i.Uri)
25
25
  return img, format, true
26
26
  } else {
27
- return DownloadImage(i)
27
+ return downloadImage(i)
28
28
  }
29
29
  }
30
30
 
31
31
 
32
32
  // Save http image
33
- func DownloadImage(i *Ires) (image.Image, string, bool) {
33
+ func downloadImage(i *Ires) (image.Image, string, bool) {
34
34
  res, err := http.Get(i.Uri)
35
35
  if err != nil {
36
36
  panic(err)
37
37
  }
38
38
  defer res.Body.Close()
39
39
 
40
- header, r := CopyReader(res.Body)
41
- format := FormatSearch(r)
40
+ header, r := copyReader(res.Body)
41
+ format := formatSearch(r)
42
42
 
43
43
  img, _, err := image.Decode(io.MultiReader(header, res.Body))
44
44
  if err != nil {
45
45
  return nil, "", false
46
46
  }
47
- return CreateImage(img, i.ImagePath(IMAGE_MODE_ORIGINAL), format), format, true
47
+ return createImage(img, i.imagePath(IMAGE_MODE_ORIGINAL), format), format, true
48
48
  }
49
49
 
50
50
 
51
- func CreateImage(img image.Image, path, format string) image.Image {
51
+ func createImage(img image.Image, path, format string) image.Image {
52
52
  file, err := os.Create(path)
53
53
  if err != nil {
54
54
  panic(err)
@@ -70,7 +70,7 @@ func CreateImage(img image.Image, path, format string) image.Image {
70
70
 
71
71
 
72
72
  // Load image
73
- func LocalImage(uri string) (image.Image, string) {
73
+ func localImage(uri string) (image.Image, string) {
74
74
  file, err := os.Open(uri)
75
75
  if err != nil{
76
76
  panic(err)
@@ -78,8 +78,8 @@ func LocalImage(uri string) (image.Image, string) {
78
78
  defer file.Close()
79
79
 
80
80
  // Decode jpeg into image.Image
81
- header, r := CopyReader(file)
82
- format := FormatSearch(r)
81
+ header, r := copyReader(file)
82
+ format := formatSearch(r)
83
83
 
84
84
  img, _, err := image.Decode(io.MultiReader(header, file))
85
85
  if err != nil {
@@ -90,15 +90,15 @@ func LocalImage(uri string) (image.Image, string) {
90
90
 
91
91
 
92
92
  // Resizing & Cropping
93
- func ResizeToCrop(i *Ires, inputImg image.Image) image.Image {
93
+ func resizeToCrop(i *Ires, inputImg image.Image) image.Image {
94
94
  var outputImg image.Image
95
95
  var imagePath string
96
96
  if i.IsLocal {
97
97
  imagePath = i.Uri
98
98
  } else {
99
- imagePath = i.ImagePath(IMAGE_MODE_ORIGINAL)
99
+ imagePath = i.imagePath(IMAGE_MODE_ORIGINAL)
100
100
  }
101
- isAsp, conf := IsValidAspectRatio(imagePath, i.Size)
101
+ isAsp, conf := isValidAspectRatio(imagePath, i.Size)
102
102
 
103
103
  width := i.Size.Width
104
104
  height := i.Size.Height
@@ -109,11 +109,11 @@ func ResizeToCrop(i *Ires, inputImg image.Image) image.Image {
109
109
  var resizeImg image.Image
110
110
 
111
111
  // Resize
112
- mode := ResizeMode(conf, i.Size)
112
+ mode := resizeMode(conf, i.Size)
113
113
  switch mode {
114
- case 1, 3:
114
+ case 3, 4:
115
115
  resizeImg = resize.Resize(uint(width), 0, inputImg, resize.Lanczos3)
116
- case 2, 4:
116
+ case 1, 2:
117
117
  resizeImg = resize.Resize(0, uint(height), inputImg, resize.Lanczos3)
118
118
  default:
119
119
  resizeImg = inputImg
@@ -133,9 +133,9 @@ func ResizeToCrop(i *Ires, inputImg image.Image) image.Image {
133
133
 
134
134
 
135
135
  // Check expiration date
136
- func (i *Ires) DeleteExpireImage(mode int) {
136
+ func (i *Ires) deleteExpireImage(mode int) {
137
137
  today := time.Now().Format("20060102")
138
- dir := i.ReadImageDir(mode)
138
+ dir := i.readImageDir(mode)
139
139
  files, err := ioutil.ReadDir(dir)
140
140
  if err != nil {
141
141
  return
@@ -145,14 +145,14 @@ func (i *Ires) DeleteExpireImage(mode int) {
145
145
  findName := file.Name()
146
146
  matched,_ := path.Match(today + "_*", findName)
147
147
  if matched {
148
- DeleteImage(path.Join(dir, findName))
148
+ deleteImage(path.Join(dir, findName))
149
149
  }
150
150
  }
151
151
  }
152
152
 
153
153
 
154
154
  // Delete image
155
- func DeleteImage(path string) {
155
+ func deleteImage(path string) {
156
156
  _, err := os.Stat(path)
157
157
  if err == nil {
158
158
  if err := os.Remove(path); err != nil {
@@ -163,8 +163,8 @@ func DeleteImage(path string) {
163
163
 
164
164
 
165
165
  // Verify aspect ratio
166
- func IsValidAspectRatio(path string, s Size) (bool, image.Config) {
167
- conf := ImageConfig(path)
166
+ func isValidAspectRatio(path string, s Size) (bool, image.Config) {
167
+ conf := imageConfig(path)
168
168
  aspH := (conf.Height * s.Width) / conf.Width
169
169
  if aspH == s.Height {
170
170
  return true, conf
@@ -175,7 +175,7 @@ func IsValidAspectRatio(path string, s Size) (bool, image.Config) {
175
175
 
176
176
 
177
177
  // Image config
178
- func ImageConfig(path string) image.Config {
178
+ func imageConfig(path string) image.Config {
179
179
  file, err := os.Open(path)
180
180
  if err != nil {
181
181
  panic(err)
@@ -191,7 +191,7 @@ func ImageConfig(path string) image.Config {
191
191
 
192
192
 
193
193
  // Select image resize mode
194
- func ResizeMode(conf image.Config, s Size) int {
194
+ func resizeMode(conf image.Config, s Size) int {
195
195
  srcWidth := s.Width
196
196
  srcHeight := s.Height
197
197
 
@@ -210,7 +210,7 @@ func ResizeMode(conf image.Config, s Size) int {
210
210
 
211
211
  // Search image format
212
212
  // if defined, return "jpeg"
213
- func FormatSearch(r io.Reader) string{
213
+ func formatSearch(r io.Reader) string{
214
214
  _, format, err := image.DecodeConfig(r)
215
215
  if err != nil {
216
216
  return "jpeg"
@@ -220,7 +220,7 @@ func FormatSearch(r io.Reader) string{
220
220
 
221
221
 
222
222
  // Copy Reader
223
- func CopyReader(body io.Reader) (io.Reader, io.Reader) {
223
+ func copyReader(body io.Reader) (io.Reader, io.Reader) {
224
224
  header := bytes.NewBuffer(nil)
225
225
  r := io.TeeReader(body, header)
226
226
  return header, r
data/ext/ires/ires.go CHANGED
@@ -27,41 +27,41 @@ type Ires struct {
27
27
 
28
28
  func (i *Ires) Resize() string {
29
29
  // Check image type
30
- i.IsLocalFile()
30
+ i.isLocalFile()
31
31
 
32
32
  // Delete the expiration date image
33
- i.DeleteExpireImage(IMAGE_MODE_RESIZE)
33
+ i.deleteExpireImage(IMAGE_MODE_RESIZE)
34
34
 
35
- distPath := i.ImagePath(IMAGE_MODE_RESIZE)
35
+ distPath := i.imagePath(IMAGE_MODE_RESIZE)
36
36
  // When the image exists, return the image path
37
- if IsExistsImage(distPath) {
38
- return i.TargetImagePath(distPath)
37
+ if isExistsImage(distPath) {
38
+ return i.targetImagePath(distPath)
39
39
  }
40
40
 
41
- inputImg, format, isImageExist := InputImage(i)
41
+ inputImg, format, isImageExist := inputImage(i)
42
42
  if !isImageExist {
43
43
  return i.Uri
44
44
  }
45
45
 
46
- outputImg := resize.Resize(uint(i.Width), uint(i.Height), inputImg, resize.Lanczos3)
47
- CreateImage(outputImg, distPath, format)
48
- return i.TargetImagePath(distPath)
46
+ outputImg := resize.Resize(uint(i.Width), uint(i.Height), inputImg, resize.Lanczos3)
47
+ createImage(outputImg, distPath, format)
48
+ return i.targetImagePath(distPath)
49
49
  }
50
50
 
51
51
  func (i *Ires) Crop() string {
52
52
  // Check image type
53
- i.IsLocalFile()
53
+ i.isLocalFile()
54
54
 
55
55
  // Delete the expiration date image
56
- i.DeleteExpireImage(IMAGE_MODE_CROP)
56
+ i.deleteExpireImage(IMAGE_MODE_CROP)
57
57
 
58
- distPath := i.ImagePath(IMAGE_MODE_CROP)
58
+ distPath := i.imagePath(IMAGE_MODE_CROP)
59
59
  // When the image exists, return the image path
60
- if IsExistsImage(distPath) {
61
- return i.TargetImagePath(distPath)
60
+ if isExistsImage(distPath) {
61
+ return i.targetImagePath(distPath)
62
62
  }
63
63
 
64
- inputImg, format, isImageExist := InputImage(i)
64
+ inputImg, format, isImageExist := inputImage(i)
65
65
  if !isImageExist {
66
66
  return i.Uri
67
67
  }
@@ -72,31 +72,31 @@ func (i *Ires) Crop() string {
72
72
  Mode: cutter.Centered,
73
73
  Options: cutter.Copy,
74
74
  })
75
- CreateImage(outputImg, distPath, format)
75
+ createImage(outputImg, distPath, format)
76
76
 
77
- return i.TargetImagePath(distPath)
77
+ return i.targetImagePath(distPath)
78
78
  }
79
79
 
80
80
  func (i *Ires) ResizeToCrop() string {
81
81
  // Check image type
82
- i.IsLocalFile()
82
+ i.isLocalFile()
83
83
 
84
84
  // Delete the expiration date image
85
- i.DeleteExpireImage(IMAGE_MODE_RESIZE_TO_CROP)
85
+ i.deleteExpireImage(IMAGE_MODE_RESIZE_TO_CROP)
86
86
 
87
- distPath := i.ImagePath(IMAGE_MODE_RESIZE_TO_CROP)
87
+ distPath := i.imagePath(IMAGE_MODE_RESIZE_TO_CROP)
88
88
  // When the image exists, return the image path
89
- if IsExistsImage(distPath) {
90
- return i.TargetImagePath(distPath)
89
+ if isExistsImage(distPath) {
90
+ return i.targetImagePath(distPath)
91
91
  }
92
92
 
93
- inputImg, format, isImageExist := InputImage(i)
93
+ inputImg, format, isImageExist := inputImage(i)
94
94
  if !isImageExist {
95
95
  return i.Uri
96
96
  }
97
97
 
98
- outputImg := ResizeToCrop(i ,inputImg)
99
- CreateImage(outputImg, distPath, format)
98
+ outputImg := resizeToCrop(i ,inputImg)
99
+ createImage(outputImg, distPath, format)
100
100
 
101
- return i.TargetImagePath(distPath)
101
+ return i.targetImagePath(distPath)
102
102
  }
data/ext/ires/uri.go CHANGED
@@ -11,7 +11,7 @@ import (
11
11
 
12
12
 
13
13
  // Input image type is Local or HTTP
14
- func (i *Ires) IsLocalFile() {
14
+ func (i *Ires) isLocalFile() {
15
15
  if strings.Index(i.Uri, "http") == -1 {
16
16
  i.IsLocal = true
17
17
  } else {
@@ -21,7 +21,7 @@ func (i *Ires) IsLocalFile() {
21
21
 
22
22
 
23
23
  // Generate image name
24
- func ImageName(i *Ires, mode int) string {
24
+ func imageName(i *Ires, mode int) string {
25
25
  splitPath := strings.Split(i.Uri, "/")
26
26
 
27
27
  // ex. sample.jpg
@@ -39,9 +39,9 @@ func ImageName(i *Ires, mode int) string {
39
39
 
40
40
  var prefix string
41
41
  switch mode {
42
- case 0: prefix = PrefixSize(i.Size) + "_resize"
43
- case 1: prefix = PrefixSize(i.Size) + "_crop"
44
- case 2: prefix = PrefixSize(i.Size) + "_resize_to_crop"
42
+ case 0: prefix = prefixSize(i.Size) + "_resize"
43
+ case 1: prefix = prefixSize(i.Size) + "_crop"
44
+ case 2: prefix = prefixSize(i.Size) + "_resize_to_crop"
45
45
  case 3: prefix = "original"
46
46
  }
47
47
 
@@ -50,7 +50,7 @@ func ImageName(i *Ires, mode int) string {
50
50
 
51
51
 
52
52
  // Generate image path
53
- func (i *Ires) ImagePath(mode int) string {
53
+ func (i *Ires) imagePath(mode int) string {
54
54
  paths := []rune(i.Dir)
55
55
  pathsLastIndex := len(paths) - 1
56
56
  lastChar := string(paths[pathsLastIndex])
@@ -61,9 +61,9 @@ func (i *Ires) ImagePath(mode int) string {
61
61
 
62
62
  var oDir string
63
63
  if i.IsLocal {
64
- oDir = LocalPath(mode)
64
+ oDir = localPath(mode)
65
65
  } else {
66
- oDir = RemotePath(i)
66
+ oDir = remotePath(i)
67
67
  }
68
68
 
69
69
  // Create directory
@@ -74,20 +74,20 @@ func (i *Ires) ImagePath(mode int) string {
74
74
  }
75
75
  }
76
76
 
77
- name := ImageName(i, mode)
77
+ name := imageName(i, mode)
78
78
  return filepath.Join(oPath, name)
79
79
  }
80
80
 
81
81
 
82
82
  // Create prefix by size
83
83
  // ex. 640x480
84
- func PrefixSize(s Size) string {
84
+ func prefixSize(s Size) string {
85
85
  return strconv.Itoa(s.Width) + "x" + strconv.Itoa(s.Height)
86
86
  }
87
87
 
88
88
 
89
89
  // リサイズ済みのファイルがあれば、処理せず返す
90
- func IsExistsImage(path string) bool {
90
+ func isExistsImage(path string) bool {
91
91
  _, err := os.Stat(path)
92
92
  if err == nil {
93
93
  return true
@@ -98,19 +98,19 @@ func IsExistsImage(path string) bool {
98
98
 
99
99
 
100
100
  // Read directory
101
- func (i *Ires) ReadImageDir(mode int) string {
101
+ func (i *Ires) readImageDir(mode int) string {
102
102
  var dir string
103
103
  if i.IsLocal {
104
- dir = LocalPath(mode)
104
+ dir = localPath(mode)
105
105
  } else {
106
- dir = RemotePath(i)
106
+ dir = remotePath(i)
107
107
  }
108
108
  return filepath.Join(i.Dir, dir)
109
109
  }
110
110
 
111
111
 
112
112
  // if local image, create ires directory
113
- func LocalPath(mode int) string {
113
+ func localPath(mode int) string {
114
114
  var dir string
115
115
  switch mode {
116
116
  case 0: dir = "ires/resize"
@@ -122,7 +122,7 @@ func LocalPath(mode int) string {
122
122
 
123
123
 
124
124
  // if http image, parse URL & make directory
125
- func RemotePath(i *Ires) string {
125
+ func remotePath(i *Ires) string {
126
126
  u, err := urlx.Parse(i.Uri)
127
127
  dir := []string{"ires"}
128
128
  if err != nil {
@@ -138,6 +138,6 @@ func RemotePath(i *Ires) string {
138
138
 
139
139
 
140
140
  // Optimize image path
141
- func (i *Ires) TargetImagePath(path string) string {
141
+ func (i *Ires) targetImagePath(path string) string {
142
142
  return strings.Replace(path, i.Dir, "", -1)
143
143
  }
data/ext/main.go CHANGED
@@ -9,9 +9,10 @@ import (
9
9
  func init() {}
10
10
  func main() {}
11
11
 
12
- //export resizeImage
13
- func resizeImage(Uri *C.char, width, height int, Dir, Expire *C.char) *C.char {
12
+ //export iresImagePath
13
+ func iresImagePath(Uri *C.char, width, height int, Mode, Dir, Expire *C.char) *C.char {
14
14
  uri := C.GoString(Uri)
15
+ mode := C.GoString(Mode)
15
16
  dir := C.GoString(Dir)
16
17
  expire := C.GoString(Expire)
17
18
 
@@ -26,45 +27,15 @@ func resizeImage(Uri *C.char, width, height int, Dir, Expire *C.char) *C.char {
26
27
  IsLocal: false,
27
28
  }
28
29
 
29
- return C.CString(r.Resize())
30
- }
31
-
32
- //export cropImage
33
- func cropImage(Uri *C.char, width, height int, Dir, Expire *C.char) *C.char {
34
- uri := C.GoString(Uri)
35
- dir := C.GoString(Dir)
36
- expire := C.GoString(Expire)
37
-
38
- r := &ires.Ires{
39
- Uri: uri,
40
- Size: ires.Size{
41
- Width: width,
42
- Height: height,
43
- },
44
- Dir: dir,
45
- Expire: expire,
46
- IsLocal: false,
47
- }
48
-
49
- return C.CString(r.Crop())
50
- }
51
-
52
- //export resizeToCropImage
53
- func resizeToCropImage(Uri *C.char, width, height int, Dir, Expire *C.char) *C.char {
54
- uri := C.GoString(Uri)
55
- dir := C.GoString(Dir)
56
- expire := C.GoString(Expire)
57
-
58
- r := &ires.Ires{
59
- Uri: uri,
60
- Size: ires.Size{
61
- Width: width,
62
- Height: height,
63
- },
64
- Dir: dir,
65
- Expire: expire,
66
- IsLocal: false,
30
+ var imagePath string
31
+ switch mode {
32
+ case "resize":
33
+ imagePath = r.Resize()
34
+ case "crop":
35
+ imagePath = r.Crop()
36
+ case "resize_to_crop":
37
+ imagePath = r.ResizeToCrop()
67
38
  }
68
39
 
69
- return C.CString(r.ResizeToCrop())
70
- }
40
+ return C.CString(imagePath)
41
+ }
data/lib/ires.rb CHANGED
@@ -1,6 +1,5 @@
1
- require 'ires/service'
2
1
  require 'ires/view_helper'
3
- require 'ires/util'
2
+
4
3
  ActiveSupport.on_load(:action_view) do
5
4
  include Ires::ViewHelper
6
5
  end
data/lib/ires/core.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'ffi'
2
+ require 'ires/os'
3
+
4
+ module Ires
5
+ module Core
6
+ extend FFI::Library
7
+
8
+ # NOTE: ires.so is golang object
9
+ ffi_lib File.expand_path("../../shared/#{Ires::Os.current}/ires.so", File.dirname(__FILE__))
10
+
11
+ # resize func
12
+ attach_function :iresImagePath, [:string, :int, :int, :string, :string, :string], :string
13
+ end
14
+ end
@@ -1,11 +1,10 @@
1
- require 'rbconfig'
2
-
3
1
  module Ires
4
- module Util
2
+ module Os
5
3
  class << self
4
+
6
5
  # Reszie image directory
7
- # return [none(ffi)]
8
- def current_os
6
+ # @return [none(ffi)]
7
+ def current
9
8
  if ['darwin', 'linux'].include?(os)
10
9
  os
11
10
  else
@@ -13,13 +12,15 @@ module Ires
13
12
  nil
14
13
  end
15
14
  end
16
-
15
+
16
+ private
17
+
17
18
  # Search OS
18
- # return [String]
19
+ # @return [String]
19
20
  def os
20
21
  @os ||= (
21
- host_os = RbConfig::CONFIG['host_os']
22
- case host_os
22
+ host_os = RbConfig::CONFIG['host_os']
23
+ case host_os
23
24
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
24
25
  'windows'
25
26
  when /darwin|mac os/
@@ -30,10 +31,10 @@ module Ires
30
31
  'unix'
31
32
  else
32
33
  'unknown'
33
- end
34
+ end
34
35
  )
35
36
  end
36
37
 
37
38
  end
38
39
  end
39
- end
40
+ end
data/lib/ires/service.rb CHANGED
@@ -1,19 +1,80 @@
1
- require 'ffi'
2
- require 'ires/util'
1
+ require 'ires/core'
2
+ require 'ires/os'
3
3
 
4
4
  module Ires
5
- module Service
6
- extend FFI::Library
7
-
8
- os = Ires::Util.current_os
9
- return if os.nil?
10
-
11
- # NOTE: ires.so is golang object
12
- ffi_lib File.expand_path("../../shared/#{os}/ires.so", File.dirname(__FILE__))
13
-
14
- # resize func
15
- attach_function :resizeImage, [:string, :int, :int, :string, :string], :string
16
- attach_function :cropImage, [:string, :int, :int, :string, :string], :string
17
- attach_function :resizeToCropImage, [:string, :int, :int, :string, :string], :string
5
+ class Service
6
+ class << self
7
+
8
+ # Resize image path
9
+ # @return [String]
10
+ def path(path:, width:, height:, mode: 'resize', expire: 30.days)
11
+
12
+ os = Ires::Os.current
13
+ return nil if os.nil?
14
+
15
+ full_path = image_full_path(path.to_s)
16
+
17
+ # if no image or could not find file path then perform the same action as 'image_tag'
18
+ return nil if invalid_path?(full_path)
19
+
20
+ expiration_date = expiration_date(expire)
21
+ dir = image_dir
22
+
23
+ ires_element = {
24
+ path: full_path,
25
+ width: width,
26
+ height: height,
27
+ mode: mode,
28
+ dir: dir,
29
+ expire: expiration_date
30
+ }
31
+ ires_image_path(ires_element)
32
+ end
33
+
34
+ private
35
+
36
+ # Image full path or HTTP URL
37
+ # @return [String]
38
+ def image_full_path(path)
39
+ root = Rails.root.to_s
40
+ if path.include?(root) || path.include?('http')
41
+ path
42
+ else
43
+ File.join(image_dir, path)
44
+ end
45
+ end
46
+
47
+ # Reszie image directory
48
+ # @return [String]
49
+ def image_dir
50
+ @image_dir ||= Pathname.new(Rails.root).join('public').to_s
51
+ end
52
+
53
+ # Check file or URI
54
+ # @return [Bool]
55
+ def invalid_path?(path)
56
+ !File.exist?(path) && !path.include?("http")
57
+ end
58
+
59
+ # Expiration date (default: 7.days)
60
+ # ex. "20170101"
61
+ # @return [String]
62
+ def expiration_date(expire)
63
+ (Date.today + expire).strftime('%Y%m%d')
64
+ end
65
+
66
+ # Image path
67
+ # @return [String]
68
+ def ires_image_path(ires_element)
69
+ Ires::Core.iresImagePath(
70
+ ires_element[:path],
71
+ ires_element[:width],
72
+ ires_element[:height],
73
+ ires_element[:mode],
74
+ ires_element[:dir],
75
+ ires_element[:expire])
76
+ end
77
+
78
+ end
18
79
  end
19
80
  end
data/lib/ires/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ires
2
- VERSION = '0.1.4'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,66 +1,18 @@
1
- require 'net/http'
1
+ require 'ires/core'
2
+ require 'ires/service'
2
3
  require 'action_view/helpers'
3
4
 
4
5
  module Ires
5
6
  module ViewHelper
6
7
 
7
8
  # Image resize
8
- # return [image_tag]
9
+ # @return [image_tag]
9
10
  def ires_tag(path:, width:, height:, mode: 'resize', expire: 30.days, **option)
10
- full_path = image_full_path(path.to_s)
11
-
12
- # if no image or could not find file path then perform the same action as 'image_tag'
13
- return image_tag(path, option) if !File.exist?(full_path) && !full_path.include?("http")
14
-
15
- # Expiration date (default: 7.days)
16
- # ex. "20170101"
17
- expiration_date = (Date.today + expire).strftime('%Y%m%d')
18
-
19
- # Reszie image
20
- case mode
21
- when 'resize'
22
- @image = Ires::Service.resizeImage(
23
- full_path,
24
- width,
25
- height,
26
- image_dir,
27
- expiration_date)
28
- when 'crop'
29
- @image = Ires::Service.cropImage(
30
- full_path,
31
- width,
32
- height,
33
- image_dir,
34
- expiration_date)
35
- when 'resize_to_crop'
36
- @image = Ires::Service.resizeToCropImage(
37
- full_path,
38
- width,
39
- height,
40
- image_dir,
41
- expiration_date)
42
- end
43
-
44
- return nil if @image.nil?
11
+ image = Ires::Service.path(path: path, width: width, height: height, mode: mode, expire: expire)
12
+ return nil if image.nil?
45
13
 
46
14
  # Set image_tag
47
- image_tag(@image, option)
48
- end
49
-
50
- private
51
- # Reszie image directory
52
- # return [String]
53
- def image_dir
54
- @image_dir ||= Pathname.new(Rails.root).join('public').to_s
55
- end
56
-
57
- def image_full_path(path)
58
- root = Rails.root.to_s
59
- if path.include?(root) || path.include?('http')
60
- path
61
- else
62
- File.join(image_dir, path)
63
- end
15
+ image_tag(image, option)
64
16
  end
65
17
 
66
18
  end
data/lib/tasks/ires.rake CHANGED
@@ -1,10 +1,9 @@
1
- require 'rbconfig'
2
- require 'ires/util'
1
+ require 'ires/os'
3
2
 
4
3
  desc "Build shared object"
5
4
  namespace :ires do
6
5
  task :build do
7
- os = Ires::Util.current_os
6
+ os = Ires::Os.current
8
7
  return if os.nil?
9
8
  exec "CGO_ENABLED=1 GOOS=\"#{os}\" go build -v -buildmode=c-shared -o shared/\"#{os}\"/ires.so ext/main.go"
10
9
  end
data/shared/darwin/ires.h CHANGED
@@ -53,11 +53,7 @@ extern "C" {
53
53
  #endif
54
54
 
55
55
 
56
- extern char* resizeImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
57
-
58
- extern char* cropImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
59
-
60
- extern char* resizeToCropImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
56
+ extern char* iresImagePath(char* p0, GoInt p1, GoInt p2, char* p3, char* p4, char* p5);
61
57
 
62
58
  #ifdef __cplusplus
63
59
  }
Binary file
data/shared/linux/ires.h CHANGED
@@ -53,11 +53,7 @@ extern "C" {
53
53
  #endif
54
54
 
55
55
 
56
- extern char* resizeImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
57
-
58
- extern char* cropImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
59
-
60
- extern char* resizeToCropImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
56
+ extern char* iresImagePath(char* p0, GoInt p1, GoInt p2, char* p3, char* p4, char* p5);
61
57
 
62
58
  #ifdef __cplusplus
63
59
  }
data/shared/linux/ires.so CHANGED
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ires
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - enta0701
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-18 00:00:00.000000000 Z
11
+ date: 2017-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -1111,8 +1111,9 @@ files:
1111
1111
  - ext/vendor/golang.org/x/text/width/trieval.go
1112
1112
  - ext/vendor/golang.org/x/text/width/width.go
1113
1113
  - lib/ires.rb
1114
+ - lib/ires/core.rb
1115
+ - lib/ires/os.rb
1114
1116
  - lib/ires/service.rb
1115
- - lib/ires/util.rb
1116
1117
  - lib/ires/version.rb
1117
1118
  - lib/ires/view_helper.rb
1118
1119
  - lib/tasks/ires.rake