質問編集履歴

1

内容変更

2018/05/31 11:12

投稿

you88
you88

スコア147

test CHANGED
File without changes
test CHANGED
@@ -7,175 +7,3 @@
7
7
 
8
8
 
9
9
  でテキストを持ってきています。
10
-
11
-
12
-
13
- ```
14
-
15
- # ⑨メソッド追加(画像生成)
16
-
17
- def make_picture(id)
18
-
19
- # contentにフォームで入力した内容をいれ、それをさらにsentenseに入れ描画用に加工
20
-
21
- sentense = ""
22
-
23
- # ⑨-1 改行を消去
24
-
25
- content = @post.title.gsub(/\r\n|\r|\n/," ")
26
-
27
- # ⑨-2 contentの文字数に応じて条件分岐
28
-
29
- if content.length <= 28 then
30
-
31
- # ⑨-3 28文字以下の場合は7文字毎に改行
32
-
33
- n = (content.length / 7).floor + 1
34
-
35
- n.times do |i|
36
-
37
- s_num = i * 7
38
-
39
- f_num = s_num + 6
40
-
41
- range = Range.new(s_num,f_num)
42
-
43
- sentense += content.slice(range)
44
-
45
- sentense += "\n" if n != i+1
46
-
47
- end
48
-
49
- # ⑨-4 文字サイズの指定
50
-
51
- pointsize = 90
52
-
53
- elsif content.length <= 50 then
54
-
55
- n = (content.length / 10).floor + 1
56
-
57
- n.times do |i|
58
-
59
- s_num = i * 10
60
-
61
- f_num = s_num + 9
62
-
63
- range = Range.new(s_num,f_num)
64
-
65
- sentense += content.slice(range)
66
-
67
- sentense += "\n" if n != i+1
68
-
69
- end
70
-
71
- pointsize = 60
72
-
73
- else
74
-
75
- n = (content.length / 15).floor + 1
76
-
77
- n.times do |i|
78
-
79
- s_num = i * 15
80
-
81
- f_num = s_num + 14
82
-
83
- range = Range.new(s_num,f_num)
84
-
85
- sentense += content.slice(range)
86
-
87
- sentense += "\n" if n != i+1
88
-
89
- end
90
-
91
- pointsize = 45
92
-
93
- end
94
-
95
- # ⑨-5 文字色の指定
96
-
97
- color = "white"
98
-
99
- # ⑨-6 文字を入れる場所の調整(0,0を変えると文字の位置が変わります)
100
-
101
- draw = "text 0,0 '#{sentense}'"
102
-
103
- # ⑨-7 フォントの指定
104
-
105
- font = ".fonts/GenEiGothicN-U-KL.otf"
106
-
107
- # ⑨-8 ↑これらの項目も文字サイズのように背景画像や文字数によって変えることができます
108
-
109
- # ⑨-9 選択された背景画像の設定
110
-
111
- base = "app/assets/images/thumbnail.png"
112
-
113
- # ⑨-11 minimagickを使って選択した画像を開き、作成した文字を指定した条件通りに挿入している
114
-
115
- image = MiniMagick::Image.open(base)
116
-
117
- image.combine_options do |i|
118
-
119
- i.font font
120
-
121
- i.fill color
122
-
123
- i.gravity 'center'
124
-
125
- i.pointsize pointsize
126
-
127
- i.draw draw
128
-
129
- end
130
-
131
- # ⑨-12 保存先のストレージの指定。Amazon S3を指定する。
132
-
133
- storage = Fog::Storage.new(
134
-
135
- provider: 'AWS',
136
-
137
- aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
138
-
139
- aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
140
-
141
- region: 'ap-northeast-1'
142
-
143
- )
144
-
145
- # ⑨-13 開発環境or本番環境でS3のバケット(フォルダのようなもの)を分ける
146
-
147
- case Rails.env
148
-
149
- when 'production'
150
-
151
- # ⑨-14 バケットの指定・URLの設定
152
-
153
- bucket = storage.directories.get('scalendar-test')
154
-
155
- # ⑨-15 保存するディレクトリ、ファイル名の指定(ファイル名は投稿id.pngとしています)
156
-
157
- png_path = 'images/' + id.to_s + '.png'
158
-
159
- image_uri = image.path
160
-
161
- file = bucket.files.create(key: png_path, public: true, body: open(image_uri))
162
-
163
- @post.img_id = '' + "/" + png_path
164
-
165
- when 'development'
166
-
167
- bucket = storage.directories.get('scalendar-test')
168
-
169
- png_path = 'images/' + id.to_s + '.png'
170
-
171
- image_uri = image.path
172
-
173
- file = bucket.files.create(key: png_path, public: true, body: open(image_uri))
174
-
175
- @post.img_id = '' + "/" + png_path
176
-
177
- end
178
-
179
- end
180
-
181
- ```