質問編集履歴

2

追記

2018/06/03 06:03

投稿

naoya.n
naoya.n

スコア26

test CHANGED
File without changes
test CHANGED
@@ -245,3 +245,223 @@
245
245
  end
246
246
 
247
247
  ```
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+ 追記
256
+
257
+
258
+
259
+ **__### lib / rmagick.rb__**
260
+
261
+ ```ruby
262
+
263
+ require 'fileutils'
264
+
265
+ require 'RMagick'
266
+
267
+
268
+
269
+
270
+
271
+ class Arg
272
+
273
+ # 引数取得
274
+
275
+ def get_arg
276
+
277
+ begin
278
+
279
+ if ARGV[0]
280
+
281
+ # ファイルが存在しなければ終了
282
+
283
+ unless File.exist?(ARGV[0])
284
+
285
+ puts MSG_NOT_EXIST + " #{ARGV[0]}"
286
+
287
+ exit
288
+
289
+ end
290
+
291
+ else
292
+
293
+ # 引数無ければ終了
294
+
295
+ puts MSG_USAGE
296
+
297
+ exit
298
+
299
+ end
300
+
301
+
302
+
303
+ # ファイル名返却
304
+
305
+ return ARGV[0]
306
+
307
+ rescue => e
308
+
309
+ STDERR.puts "[ERROR][#{self.class.name}.get_arg] #{e}"
310
+
311
+ exit 1
312
+
313
+ end
314
+
315
+ end
316
+
317
+ end
318
+
319
+
320
+
321
+ class Rmagick
322
+
323
+ attr_reader :translation, :lyric, :title, :artist
324
+
325
+
326
+
327
+ def initialize(filename, translation_img:, lyric_img:, title_img:, artist_img:)
328
+
329
+ @filename = filename
330
+
331
+ @translation = translation_img
332
+
333
+ @lyric = lyric_img
334
+
335
+ @title = title_img
336
+
337
+ @artist = artist_img
338
+
339
+ end
340
+
341
+
342
+
343
+ # テキスト描画
344
+
345
+ def rmagick(columns = 5)
346
+
347
+
348
+
349
+ FileUtils.cp(@filename, "2"+@filename, {:preserve => true}) # 元画像退避
350
+
351
+ img = Magick::ImageList.new(@filename) # 画像オブジェクト
352
+
353
+ draw = Magick::Draw.new # 描画オブジェクト
354
+
355
+ begin
356
+
357
+ draw.annotate(img, 0, 0, 70, 320, @lyric.scan(/.{1,#{30}}/).join("\n")) do
358
+
359
+
360
+
361
+ self.fill = 'white'
362
+
363
+ self.stroke = 'transparent'
364
+
365
+ self.pointsize = 24
366
+
367
+ self.gravity = Magick::NorthWestGravity
368
+
369
+ end
370
+
371
+
372
+
373
+ draw.annotate(img, 0, 0, 70, 165, @tranalation.scan(/.{0,44}[a-z.!?,;](?:\b|$)/mi).join("\n") ) do
374
+
375
+
376
+
377
+ self.fill = 'white'
378
+
379
+ self.stroke = 'transparent'
380
+
381
+ self.pointsize = 44
382
+
383
+ self.gravity = Magick::NorthWestGravity
384
+
385
+ end
386
+
387
+
388
+
389
+ draw.annotate(img, 0, 0, 68, 55, @title) do
390
+
391
+
392
+
393
+ self.fill = 'white'
394
+
395
+ self.stroke = 'transparent'
396
+
397
+ self.pointsize = 24
398
+
399
+ self.gravity = Magick::NorthWestGravity
400
+
401
+ end
402
+
403
+
404
+
405
+ # 画像生成
406
+
407
+ img.write("2"+@filename)
408
+
409
+ rescue => e
410
+
411
+ STDERR.puts "[ERROR][#{self.class.name}.rmagick] #{e}"
412
+
413
+ exit 1
414
+
415
+ end
416
+
417
+ end
418
+
419
+ end
420
+
421
+
422
+
423
+ ```
424
+
425
+ **__### SongsController.rb__**
426
+
427
+ ```ruby
428
+
429
+ def share
430
+
431
+
432
+
433
+ @translation= Translation.find_by(id: params[:id])
434
+
435
+ @lyric = Lyric.find_by(number: @translation.number , song_id:@translation.song_id)
436
+
437
+ @song = Song.find_by(id: @translation.song_id)
438
+
439
+
440
+
441
+ if @current_user
442
+
443
+ @user = User.find_by(id: @current_user.id)
444
+
445
+ end
446
+
447
+
448
+
449
+ images = {
450
+
451
+ translation_img: @translation.content,
452
+
453
+ lyric_img: @lyric.content,
454
+
455
+ artist_img: @song.artist,
456
+
457
+ title_img: @song.title
458
+
459
+ }
460
+
461
+ filename = Rails.root.join('public', 'test2.png')
462
+
463
+ ::Rmagick.new(filename.to_s, **images).rmagick if filename.exist?
464
+
465
+ end
466
+
467
+ ```

1

タイトル

2018/06/03 06:03

投稿

naoya.n
naoya.n

スコア26

test CHANGED
@@ -1 +1 @@
1
- モデルから取得した変数を、コントローラでpublic配下のrbファイルに渡したい
1
+ モデルから取得した変数を、public配下のrbファイルに渡したい
test CHANGED
File without changes