ires 0.1.0 → 0.1.1

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: 66797c484af9e7c031cc342f26b4665b7f76313f
4
- data.tar.gz: fc97bea1182999376c00f08fdfb087995dcd84cd
3
+ metadata.gz: 023001af021142239fda30dfb1e8175965e7d88c
4
+ data.tar.gz: b7680a40d6a9a75cdc543db2e74e5a3a3f87d483
5
5
  SHA512:
6
- metadata.gz: a1188b7281b891486501c101d71843a7d89ac1be0e006413cdf98459693fd2a3735ac388b8d529e6459d43d298a906d18ea27c6844a8a68df27a181b0f0c17ee
7
- data.tar.gz: 83e1fa789edf1182ccb60976889a510148c8889f51aed4561b953a7b340e583c2af67d249e10e4649a0cf822f56f3136aabae4010b0e8d2aa8e9e412b741e468
6
+ metadata.gz: 7a87de597b8b366c7351030ed3cd902f1b1b8f9fe9f0f62bbd6bf8a7868dbd543f31451b6b0727c1a7abf836688faa8788df5314bb700d3c251ae599ce0b180b
7
+ data.tar.gz: 5b09fde65eb7f157cdc29a0f92722c279c04684995fa9c5e4c18de609582ba029ffd9a45dd669e2cf328828ba00d8ddec2f5869b1fda27be9a86b8d7e2ad4d3c
data/README.md CHANGED
@@ -1,4 +1,8 @@
1
1
  # Ires
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ires.svg)](https://badge.fury.io/rb/ires)
4
+
5
+
2
6
  `Ires` is image resizer gem.
3
7
 
4
8
  ## Usage
@@ -19,32 +23,51 @@
19
23
  | Cropping | crop |
20
24
  | Rsize after Cropping | rsize_to_crop |
21
25
 
26
+
27
+ ### Specify cache expiration
28
+
29
+ Default: **30days**
30
+
31
+ ```erb
32
+ <%= ires_tag( path: "/image.jpg", width: 400, height: 300, expire: 7.days ) %>
33
+ ```
34
+
22
35
  ### Saved directory
23
36
 
24
37
  ```
25
38
  .
26
- └── public
27
-    └── ires
28
-    ├── crop
29
-    │   └── 300x220
30
-       └── image_300x220.jpg
31
-    ├── original
32
-    │   └── original
33
-       └── image.jpg
34
-    ├── resize
35
-    │   ├── 200x150
36
-    │   │   └── image_200x150.jpg
37
-       ├── 300x220
38
-    │   │   └── image_300x220.jpg
39
-     │   ├── 300x400
40
-    │   │   └── image_300x400.jpg
41
-    │   ├── 400x300
42
-    │   │   └── image_400x300.jpg
43
-    │   └── 90x120
44
-       └── image_90x120.jpg
45
-    └── resize_to_crop
46
-    └── 300x220
47
-    └── image_300x220.jpg
39
+ └── public
40
+ ├── image.jpg
41
+ └── ires
42
+ ├── crop
43
+    ├── 150x150
44
+ │   │   └── 20171012_image.jpg
45
+ │   ├── 200x170
46
+    │   └── 20171019_image.jpg
47
+ │   ├── 400x300
48
+ │   │   └── 20171028_image.jpg
49
+ │   └── 640x480
50
+    └── 20171005_image.jpg
51
+ ├── original
52
+ │   └── original
53
+ ├── resize
54
+ │   ├── 150x150
55
+ │   │   └── 20171012_image.jpg
56
+ │   ├── 200x170
57
+    │   └── 20171019_image.jpg
58
+ │   ├── 400x300
59
+ │   │   └── 20171028_image.jpg
60
+ │   └── 640x480
61
+ │   └── 20171005_image.jpg
62
+ └── resize_to_crop
63
+ ├── 150x150
64
+ │   └── 20171012_image.jpg
65
+ ├── 200x170
66
+ │   └── 20171019_image.jpg
67
+ ├── 400x300
68
+ │   └── 20171028_image.jpg
69
+ └── 640x480
70
+ └── 20171005_image.jpg
48
71
  ```
49
72
 
50
73
  `original` directory where downloaded images are saved.
@@ -14,47 +14,64 @@ const (
14
14
  IMAGE_MODE_RESIZE int = iota
15
15
  IMAGE_MODE_CROP
16
16
  IMAGE_MODE_RESIZE_TO_CROP
17
+ IMAGE_MODE_ORIGINAL
17
18
  )
18
19
 
19
20
  func init() {}
20
21
  func main() {}
21
22
 
22
23
  //export resizeImage
23
- func resizeImage(Uri *C.char, width, height int, Dir *C.char) *C.char {
24
- dir := C.GoString(Dir)
25
- uri := C.GoString(Uri)
24
+ func resizeImage(Uri *C.char, width, height int, Dir, Expire *C.char) *C.char {
25
+ uri := C.GoString(Uri)
26
+ dir := C.GoString(Dir)
27
+ expire := C.GoString(Expire)
26
28
 
27
29
  size := []int{width, height}
28
- path := util.NewImagePath(uri, dir, IMAGE_MODE_RESIZE, size...)
30
+ path := util.NewImagePath(uri, dir, expire, IMAGE_MODE_RESIZE, size...)
31
+ originalPath := util.NewImagePath(uri, dir, expire, IMAGE_MODE_ORIGINAL)
32
+
33
+ // Delete the expiration date image
34
+ util.DeleteExpireImage(uri, dir, IMAGE_MODE_RESIZE, size...)
29
35
 
30
36
  // When the image exists, return the image path
31
- if !util.IsEmptyImage(path) {
37
+ if util.IsExistsImage(path) {
32
38
  return C.CString(strings.Replace(path, dir, "", -1))
33
39
  }
34
40
 
35
- inputImg, _ := operate.InputImage(uri, dir)
41
+ inputImg, _, isImageExist := operate.InputImage(uri, originalPath)
42
+ if !isImageExist {
43
+ return C.CString(uri)
44
+ }
36
45
  outputImg := resize.Resize(uint(width), uint(height), inputImg, resize.Lanczos3)
37
46
 
38
- _, filePath := operate.CreateImage(outputImg, path)
47
+ _, filePath, _ := operate.CreateImage(outputImg, path)
39
48
 
40
49
  fileName := strings.Replace(filePath, dir, "", -1)
41
50
  return C.CString(fileName)
42
51
  }
43
52
 
44
53
  //export cropImage
45
- func cropImage(Uri *C.char, width, height int, Dir *C.char) *C.char {
46
- dir := C.GoString(Dir)
47
- uri := C.GoString(Uri)
54
+ func cropImage(Uri *C.char, width, height int, Dir, Expire *C.char) *C.char {
55
+ uri := C.GoString(Uri)
56
+ dir := C.GoString(Dir)
57
+ expire := C.GoString(Expire)
48
58
 
49
59
  size := []int{width, height}
50
- path := util.NewImagePath(uri, dir, IMAGE_MODE_CROP, size...)
60
+ path := util.NewImagePath(uri, dir, expire, IMAGE_MODE_CROP, size...)
61
+ originalPath := util.NewImagePath(uri, dir, expire, IMAGE_MODE_ORIGINAL)
62
+
63
+ // Delete the expiration date image
64
+ util.DeleteExpireImage(uri, dir, IMAGE_MODE_CROP, size...)
51
65
 
52
66
  // When the image exists, return the image path
53
- if !util.IsEmptyImage(path) {
67
+ if util.IsExistsImage(path) {
54
68
  return C.CString(strings.Replace(path, dir, "", -1))
55
69
  }
56
70
 
57
- inputImg, _ := operate.InputImage(uri, dir)
71
+ inputImg, _, isImageExist := operate.InputImage(uri, originalPath)
72
+ if !isImageExist {
73
+ return C.CString(uri)
74
+ }
58
75
  outputImg, _ := cutter.Crop(inputImg, cutter.Config{
59
76
  Width: width,
60
77
  Height: height,
@@ -62,28 +79,36 @@ func cropImage(Uri *C.char, width, height int, Dir *C.char) *C.char {
62
79
  Options: cutter.Copy,
63
80
  })
64
81
 
65
- _, filePath := operate.CreateImage(outputImg, path)
82
+ _, filePath, _ := operate.CreateImage(outputImg, path)
66
83
 
67
84
  fileName := strings.Replace(filePath, dir, "", -1)
68
85
  return C.CString(fileName)
69
86
  }
70
87
 
71
88
  //export resizeToCropImage
72
- func resizeToCropImage(Uri *C.char, width, height int, Dir *C.char) *C.char {
73
- dir := C.GoString(Dir)
74
- uri := C.GoString(Uri)
89
+ func resizeToCropImage(Uri *C.char, width, height int, Dir, Expire *C.char) *C.char {
90
+ uri := C.GoString(Uri)
91
+ dir := C.GoString(Dir)
92
+ expire := C.GoString(Expire)
75
93
 
76
94
  size := []int{width, height}
77
- path := util.NewImagePath(uri, dir, IMAGE_MODE_RESIZE_TO_CROP, size...)
95
+ path := util.NewImagePath(uri, dir, expire, IMAGE_MODE_RESIZE_TO_CROP, size...)
96
+ originalPath := util.NewImagePath(uri, dir, expire, IMAGE_MODE_ORIGINAL)
97
+
98
+ // Delete the expiration date image
99
+ util.DeleteExpireImage(uri, dir, IMAGE_MODE_RESIZE_TO_CROP, size...)
78
100
 
79
101
  // When the image exists, return the image path
80
- if !util.IsEmptyImage(path) {
102
+ if util.IsExistsImage(path) {
81
103
  return C.CString(strings.Replace(path, dir, "", -1))
82
104
  }
83
105
 
84
- inputImg, imgPath := operate.InputImage(uri, dir)
106
+ inputImg, imgPath, isImageExist := operate.InputImage(uri, originalPath)
107
+ if !isImageExist {
108
+ return C.CString(uri)
109
+ }
85
110
  outputImg := operate.ResizeToCrop(imgPath, size, inputImg)
86
- _, filePath := operate.CreateImage(outputImg, path)
111
+ _, filePath, _ := operate.CreateImage(outputImg, path)
87
112
 
88
113
  fileName := strings.Replace(filePath, dir, "", -1)
89
114
  return C.CString(fileName)
@@ -12,17 +12,17 @@ import (
12
12
  )
13
13
 
14
14
  // 入力画像
15
- func InputImage(uri, dir string) (image.Image, string) {
15
+ func InputImage(uri, path string) (image.Image, string, bool) {
16
16
  if util.IsLocalFile(uri) {
17
- return LocalImage(uri), uri
17
+ return LocalImage(uri), uri, true
18
18
  } else {
19
- img, path := DownloadImage(uri, util.NewImagePath(uri, dir, 3))
20
- return img, path
19
+ img, path, isImageExist := DownloadImage(uri, path)
20
+ return img, path, isImageExist
21
21
  }
22
22
  }
23
23
 
24
24
  // http経由での画像を保存
25
- func DownloadImage(uri, path string) (image.Image, string) {
25
+ func DownloadImage(uri, path string) (image.Image, string, bool) {
26
26
  res, err := http.Get(uri)
27
27
  if err != nil {
28
28
  panic(err)
@@ -31,13 +31,13 @@ func DownloadImage(uri, path string) (image.Image, string) {
31
31
 
32
32
  img, _, err := image.Decode(res.Body)
33
33
  if err != nil {
34
- panic(err)
34
+ return nil, path, false
35
35
  }
36
36
  return CreateImage(img, path)
37
37
  }
38
38
 
39
39
  // 画像を作成
40
- func CreateImage(img image.Image, path string) (image.Image, string) {
40
+ func CreateImage(img image.Image, path string) (image.Image, string, bool) {
41
41
  file, err := os.Create(path)
42
42
  if err != nil {
43
43
  panic(err)
@@ -46,7 +46,7 @@ func CreateImage(img image.Image, path string) (image.Image, string) {
46
46
 
47
47
  jpeg.Encode(file, img, nil)
48
48
 
49
- return LocalImage(path), path
49
+ return LocalImage(path), path, true
50
50
  }
51
51
 
52
52
  // ローカルの画像を取得
@@ -5,7 +5,7 @@ import (
5
5
  "path/filepath"
6
6
  "strconv"
7
7
  "strings"
8
- "io/ioutil"
8
+ "time"
9
9
  )
10
10
 
11
11
  // ローカルの画像かどうか
@@ -26,19 +26,16 @@ func getImageName(uri string) (string, string) {
26
26
  }
27
27
 
28
28
  // 画像のフルパスを生成
29
- func NewImagePath(uri, dir string, mode int, size ...int) string {
30
- name := NewImageName(uri, size...)
29
+ func NewImagePath(uri, dir, expire string, mode int, size ...int) string {
30
+ name := NewImageName(uri, expire)
31
31
  return FilePath(name, dir, mode, size...)
32
32
  }
33
33
 
34
34
  // ファイル名を生成
35
- func NewImageName(uri string, size ...int) string {
35
+ func NewImageName(uri, expire string) string {
36
36
  imageName, ext := getImageName(uri)
37
- if len(size) == 2 {
38
- imageName += "_" + PrefixSize(size...)
39
- }
40
- imageName += ext
41
- return imageName
37
+ fullImageName := expire + "_" + imageName + ext
38
+ return fullImageName
42
39
  }
43
40
 
44
41
  // 画像を保存するパスの設定
@@ -53,10 +50,10 @@ func FilePath(name string, d string, mode int, size ...int) string {
53
50
  // 画像格納先
54
51
  var oDir string
55
52
  switch mode {
56
- case 0: oDir = "ires/resize"
57
- case 1: oDir = "ires/crop"
58
- case 2: oDir = "ires/resize_to_crop"
59
- default: oDir = "ires/original"
53
+ case 0: oDir = "ires/resize"
54
+ case 1: oDir = "ires/crop"
55
+ case 2: oDir = "ires/resize_to_crop"
56
+ case 3: oDir = "ires/original"
60
57
  }
61
58
 
62
59
  var prefix string
@@ -86,11 +83,23 @@ func PrefixSize(size ...int) string {
86
83
  }
87
84
 
88
85
  // リサイズ済みのファイルがあれば、処理せず返す
89
- func IsEmptyImage(path string) bool {
90
- _, err := ioutil.ReadFile(path)
91
- if err != nil {
86
+ func IsExistsImage(path string) bool {
87
+ _, err := os.Stat(path)
88
+ if err == nil {
92
89
  return true
93
90
  } else {
94
91
  return false
95
92
  }
93
+ }
94
+
95
+ // 有効期限に達した画像を削除
96
+ func DeleteExpireImage(uri, dir string, mode int, size ...int) {
97
+ today := time.Now().Format("20060102")
98
+ path := NewImagePath(uri, dir, today, mode, size...)
99
+ _, err := os.Stat(path)
100
+ if err == nil {
101
+ if err := os.Remove(path); err != nil {
102
+ panic(err)
103
+ }
104
+ }
96
105
  }
@@ -1,4 +1,4 @@
1
- require "ffi"
1
+ require 'ffi'
2
2
  require 'ires/util'
3
3
 
4
4
  module Ires
@@ -12,8 +12,8 @@ module Ires
12
12
  ffi_lib File.expand_path("../../shared/#{os}/ires.so", File.dirname(__FILE__))
13
13
 
14
14
  # resize func
15
- attach_function :resizeImage, [:string, :int, :int, :string], :string
16
- attach_function :cropImage, [:string, :int, :int, :string], :string
17
- attach_function :resizeToCropImage, [:string, :int, :int, :string], :string
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
18
18
  end
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module Ires
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -1,19 +1,43 @@
1
+ require 'net/http'
2
+
1
3
  module Ires
2
4
  module ViewHelper
3
5
 
4
6
  # Image resize
5
7
  # return [image_tag]
6
- def ires_tag(path:, width:, height:, mode: "resize", **option)
8
+ def ires_tag(path:, width:, height:, mode: "resize", expire: 30.days, **option)
7
9
  full_path = image_full_path(path.to_s)
8
10
 
11
+ # if no image or could not find file path then perform the same action as 'image_tag'
12
+ return image_tag(path, option) if !File.exist?(full_path) && !full_path.include?("http")
13
+
14
+ # Expiration date (default: 7.days)
15
+ # ex. "20170101"
16
+ expiration_date = (Date.today + expire).strftime("%Y%m%d")
17
+
9
18
  # Reszie image
10
19
  case mode
11
20
  when "resize"
12
- @image = Ires::Service.resizeImage(full_path, width, height, image_dir)
21
+ @image = Ires::Service.resizeImage(
22
+ full_path,
23
+ width,
24
+ height,
25
+ image_dir,
26
+ expiration_date)
13
27
  when "crop"
14
- @image = Ires::Service.cropImage(full_path, width, height, image_dir)
28
+ @image = Ires::Service.cropImage(
29
+ full_path,
30
+ width,
31
+ height,
32
+ image_dir,
33
+ expiration_date)
15
34
  when "resize_to_crop"
16
- @image = Ires::Service.resizeToCropImage(full_path, width, height, image_dir)
35
+ @image = Ires::Service.resizeToCropImage(
36
+ full_path,
37
+ width,
38
+ height,
39
+ image_dir,
40
+ expiration_date)
17
41
  end
18
42
 
19
43
  return nil if @image.nil?
@@ -53,11 +53,11 @@ extern "C" {
53
53
  #endif
54
54
 
55
55
 
56
- extern char* resizeImage(char* p0, GoInt p1, GoInt p2, char* p3);
56
+ extern char* resizeImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
57
57
 
58
- extern char* cropImage(char* p0, GoInt p1, GoInt p2, char* p3);
58
+ extern char* cropImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
59
59
 
60
- extern char* resizeToCropImage(char* p0, GoInt p1, GoInt p2, char* p3);
60
+ extern char* resizeToCropImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
61
61
 
62
62
  #ifdef __cplusplus
63
63
  }
Binary file
@@ -53,11 +53,11 @@ extern "C" {
53
53
  #endif
54
54
 
55
55
 
56
- extern char* resizeImage(char* p0, GoInt p1, GoInt p2, char* p3);
56
+ extern char* resizeImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
57
57
 
58
- extern char* cropImage(char* p0, GoInt p1, GoInt p2, char* p3);
58
+ extern char* cropImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
59
59
 
60
- extern char* resizeToCropImage(char* p0, GoInt p1, GoInt p2, char* p3);
60
+ extern char* resizeToCropImage(char* p0, GoInt p1, GoInt p2, char* p3, char* p4);
61
61
 
62
62
  #ifdef __cplusplus
63
63
  }
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - enta0701
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-27 00:00:00.000000000 Z
11
+ date: 2017-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi