cafe_buy_duo 0.1.51 → 0.1.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cafe_buy_duo.rb +185 -110
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ab9b13b2f6755f84dfcad6193902351caa52ba3cfbed979a8e717920ca328e4
4
- data.tar.gz: b43408a87613df0002c399f652e355e904732d129a92abd7ee2173db383c4f84
3
+ metadata.gz: bb6262162997d15877bbefc16791064e678caa15cba1fe4502669f9e1a4b58c2
4
+ data.tar.gz: ae6f20b8fa94e6704f2e596749019afa47297e4757105ee55f07586c4f441609
5
5
  SHA512:
6
- metadata.gz: d9dddfb0a0a315ff6b24eeaa9a4685d1817551f9eab78a356681784192888c492befc9c5f3cb1b8f78672a62607c06fc9c1a30ae1fac64ef2a819d372efa6b65
7
- data.tar.gz: 222c76cac0fc0d4a83256afe91d98651bf5dbd4aaa76b0b1b781f4e5ddfd1def96f4722d36042fc6dfa4d03d3d742a0597593c2128ab050869fffe6582bdbcee
6
+ metadata.gz: 4a07c7159f00755c9721322c8a4a0315717e4b4dce78d1ef5bf2960a5055c940fd33322b9a1bf04acc6e46f7e29500c8bfd485aa2439982ca8647db2bb95c4c7
7
+ data.tar.gz: e73ef5468ac924cdeae86cc0263c1e2b99b15925dddcbeab5ca60ebaf3f684c003bb50fa67a575385c777ee1d11658b4f123e561aa405a79a235adcd89ab5b6c
data/lib/cafe_buy_duo.rb CHANGED
@@ -2434,84 +2434,112 @@ class Wordpress
2434
2434
 
2435
2435
 
2436
2436
 
2437
+ def crop_image_height_under_width(path, min_crop_ratio = 0.625)
2438
+ img = Magick::Image.read(path).first
2439
+ width = img.columns
2440
+ height = img.rows
2441
+
2442
+
2443
+
2444
+ if height > width
2445
+ min_height = (width * min_crop_ratio).to_i
2446
+ new_height = rand(min_height..width)
2447
+ crop_top = ((height - new_height) / 2.0).round
2448
+
2449
+ cropped = img.crop(0, crop_top, width, new_height, true)
2450
+ cropped.write(path)
2451
+
2452
+
2453
+ else
2454
+
2455
+ end
2456
+ end
2457
+
2437
2458
  def auto_image(keyword = nil)
2438
- keyword ||= @keyword
2439
- puts "키워드: #{keyword}"
2440
-
2441
- client = HTTPClient.new
2442
- client.default_header = {
2443
- 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '\
2444
- '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
2445
- 'Accept' => 'application/json, text/javascript, */*; q=0.01',
2446
- 'Accept-Language' => 'en-US,en;q=0.9',
2447
- 'Referer' => "https://unsplash.com/s/photos/#{URI.encode_www_form_component(keyword)}",
2448
- 'X-Requested-With' => 'XMLHttpRequest'
2449
- }
2459
+ # auto_image 내부에서만 crop 호출
2460
+ keyword ||= @keyword
2461
+ puts "키워드: #{keyword}"
2462
+
2463
+ client = HTTPClient.new
2464
+ client.default_header = {
2465
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '\
2466
+ '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
2467
+ 'Accept' => 'application/json, text/javascript, */*; q=0.01',
2468
+ 'Accept-Language' => 'en-US,en;q=0.9',
2469
+ 'Referer' => "https://unsplash.com/s/photos/#{URI.encode_www_form_component(keyword)}",
2470
+ 'X-Requested-With' => 'XMLHttpRequest'
2471
+ }
2472
+
2473
+ retry_count = 0
2474
+ max_retries = 10
2475
+ results = []
2450
2476
 
2451
- retry_count = 0
2452
- max_retries = 10
2453
- results = []
2477
+ begin
2478
+ page = rand(1..15)
2479
+ url = "https://unsplash.com/napi/search/photos?query=#{URI.encode_www_form_component(keyword)}&page=#{page}&per_page=20"
2480
+ puts "Request URL: #{url}"
2481
+ res = client.get(url)
2482
+
2483
+ unless res.status == 200
2484
+ puts "HTTP Error: #{res.status}"
2485
+ raise "HTTP Error"
2486
+ end
2454
2487
 
2455
- begin
2456
- page = rand(1..15)
2457
- url = "https://unsplash.com/napi/search/photos?query=#{URI.encode_www_form_component(keyword)}&page=#{page}&per_page=20"
2458
- puts "Request URL: #{url}"
2459
- res = client.get(url)
2460
-
2461
- unless res.status == 200
2462
- puts "HTTP Error: #{res.status}"
2463
- raise "HTTP Error"
2464
- end
2488
+ json = JSON.parse(res.body)
2489
+ results = json['results']
2490
+ mm = []
2465
2491
 
2466
- json = JSON.parse(res.body)
2467
- results = json['results']
2468
- mm = []
2492
+ results.each do |photo|
2493
+ full_url = photo.dig('urls', 'full').to_s
2494
+ regular_url = photo.dig('urls', 'regular').to_s
2469
2495
 
2470
- results.each do |photo|
2471
- full_url = photo.dig('urls', 'full').to_s
2472
- regular_url = photo.dig('urls', 'regular').to_s
2496
+ if full_url.start_with?("https://images.unsplash.com/photo-") &&
2497
+ regular_url.include?("1080")
2498
+ mm << full_url
2499
+ end
2500
+ end
2473
2501
 
2474
- if full_url.start_with?("https://images.unsplash.com/photo-") &&
2475
- regular_url.include?("1080")
2476
- mm << full_url
2477
- end
2478
- end
2502
+ if mm.empty?
2503
+ raise "No matching image"
2504
+ end
2479
2505
 
2480
- if mm.empty?
2481
- raise "No matching image"
2482
- end
2506
+ selected_url = mm.sample
2507
+ destination_path = "./image/memory.png"
2508
+ Down.download(selected_url, destination: destination_path)
2509
+ puts "이미지 다운로드 완료: #{selected_url}"
2483
2510
 
2484
- selected_url = mm.sample
2485
- Down.download(selected_url, destination: "./image/memory.png")
2486
- puts "이미지 다운로드 완료: #{selected_url}"
2511
+ # 오직 auto_image에서만 자르기 호출
2512
+ crop_image_height_under_width(destination_path)
2487
2513
 
2488
- rescue => e
2489
- retry_count += 1
2490
- puts "auto_image 에러: #{e.message} (재시도 #{retry_count}/#{max_retries})"
2491
- sleep(3)
2492
- if retry_count < max_retries
2493
- retry
2494
- else
2495
- puts "최대 재시도 초과. 조건 무시하고 랜덤 이미지 다운로드 시도..."
2496
-
2497
- if results && !results.empty?
2498
- random_photo = results.sample
2499
- fallback_url = random_photo.dig('urls', 'full')
2500
- if fallback_url
2501
- Down.download(fallback_url, destination: "./image/memory.png")
2502
- puts "랜덤 이미지 다운로드 완료: #{fallback_url}"
2503
- else
2504
- puts "랜덤 이미지 URL을 찾을 수 없습니다. 단색 배경 이미지 생성합니다."
2505
- color_image
2506
- end
2514
+ rescue => e
2515
+ retry_count += 1
2516
+ puts "auto_image 에러: #{e.message} (재시도 #{retry_count}/#{max_retries})"
2517
+ sleep(3)
2518
+ if retry_count < max_retries
2519
+ retry
2520
+ else
2521
+ puts "최대 재시도 초과. 조건 무시하고 랜덤 이미지 다운로드 시도..."
2522
+
2523
+ if results && !results.empty?
2524
+ random_photo = results.sample
2525
+ fallback_url = random_photo.dig('urls', 'full')
2526
+ if fallback_url
2527
+ Down.download(fallback_url, destination: "./image/memory.png")
2528
+ puts "랜덤 이미지 다운로드 완료: #{fallback_url}"
2529
+ crop_image_height_under_width("./image/memory.png")
2507
2530
  else
2508
- puts "이미지 결과가 없어 다운로드할 수 없습니다. 단색 배경 이미지 생성합니다."
2509
- color_image
2510
- end
2531
+ puts "랜덤 이미지 URL을 찾을 수 없습니다. 단색 배경 이미지 생성합니다."
2532
+ color_image
2511
2533
  end
2534
+ else
2535
+ puts "이미지 결과가 없어 다운로드할 수 없습니다. 단색 배경 이미지 생성합니다."
2536
+ color_image
2512
2537
  end
2538
+ end
2539
+ end
2513
2540
  end
2514
2541
 
2542
+
2515
2543
  def color_image
2516
2544
  color = File.open('./color.ini', 'r', :encoding => 'utf-8').read().split("\n")
2517
2545
  image = Magick::Image.new(740, 740) { |k| k.background_color = color.sample }
@@ -2604,57 +2632,104 @@ class Wordpress
2604
2632
  end
2605
2633
 
2606
2634
 
2607
- def image_text(text1, text2)
2635
+ def image_text(text1, text2)
2636
+ begin
2637
+ color = File.open('./color.ini', 'r', encoding: 'utf-8').read.split("\n").map(&:strip).reject(&:empty?)
2638
+ font_files = Dir.entries('./fonts').select { |f| f.downcase.end_with?('.ttf') }
2639
+ font2 = './fonts/' + font_files.sample
2640
+
2641
+ # 랜덤 글자색 선택
2642
+ color2 = color.sample
2643
+
2644
+ # 헬퍼 함수: 색상 문자열 '#RRGGBB' -> [R,G,B] 배열로 변환
2645
+ def hex_to_rgb(hex)
2646
+ hex = hex.delete('#')
2647
+ [hex[0..1], hex[2..3], hex[4..5]].map { |c| c.to_i(16) }
2648
+ end
2649
+
2650
+ # 헬퍼 함수: 두 RGB 색상의 차이 계산 (간단한 유클리드 거리)
2651
+ def color_distance(c1, c2)
2652
+ Math.sqrt(
2653
+ (c1[0] - c2[0])**2 +
2654
+ (c1[1] - c2[1])**2 +
2655
+ (c1[2] - c2[2])**2
2656
+ )
2657
+ end
2658
+
2659
+ # 대비가 충분히 되는 테두리 색상 선택
2660
+ max_attempts = 10
2661
+ stroke_color = nil
2662
+ base_rgb = hex_to_rgb(color2)
2663
+
2664
+ max_attempts.times do
2665
+ candidate = color.sample
2666
+ candidate_rgb = hex_to_rgb(candidate)
2667
+ dist = color_distance(base_rgb, candidate_rgb)
2668
+
2669
+ # 거리(차이) 임계값 100 (0~441 범위) — 필요시 조절 가능
2670
+ if dist > 100
2671
+ stroke_color = candidate
2672
+ break
2673
+ end
2674
+ end
2675
+ stroke_color ||= '#000000' # 만약 충분히 다른 색 없으면 검정색 기본값
2676
+
2677
+ img = Magick::Image.read('./image/memory.png').first
2678
+ draw = Magick::Draw.new
2679
+
2680
+ raw_message = "#{text1}\n#{text2}".strip
2681
+ max_width = img.columns * 0.85
2682
+ max_height = img.rows * 0.6
2683
+
2608
2684
  begin
2609
- color = File.open('./color.ini', 'r', :encoding => 'utf-8').read().split("\n")
2610
- font_files = Dir.entries('./fonts').select { |f| f.downcase.end_with?('.ttf') }
2611
- font2 = './fonts/' + font_files.sample
2612
- color2 = color.sample
2613
-
2614
- img = Magick::Image.read('./image/memory.png').first
2615
- draw = Magick::Draw.new
2616
-
2617
- raw_message = "#{text1}\n#{text2}".strip
2618
- max_width = img.columns * 0.85
2619
- max_height = img.rows * 0.6
2620
-
2621
- begin
2622
- size = rand(@data['이미지설정']['이미지글자1크기1'].text.to_i..@data['이미지설정']['이미지글자1크기2'].text.to_i)
2623
- rescue
2624
- size = 30
2625
- end
2626
-
2627
- wrapped_message, adjusted_size = wrap_text_to_fit(draw, raw_message, max_width, max_height, font2, size)
2628
-
2629
- if @data['이미지설정']['글자그림자'].checked?
2630
- img.annotate(draw, 0, 0, 2, 2, wrapped_message) do
2631
- draw.gravity = Magick::CenterGravity
2632
- draw.pointsize = adjusted_size
2633
- draw.fill = '#000000'
2634
- draw.font = font2
2635
- end
2636
- end
2637
-
2638
- draw2 = Magick::Draw.new
2639
- img.annotate(draw2, 0, 0, 0, 0, wrapped_message) do
2640
- draw2.gravity = Magick::CenterGravity
2641
- draw2.pointsize = adjusted_size
2642
- draw2.fill = color2
2643
- draw2.font = font2
2644
- if @data['이미지설정']['글자테두리'].checked?
2645
- draw2.stroke_width = 2
2646
- draw2.stroke = '#000000'
2647
- end
2648
- end
2649
-
2650
- img.write('./image/memory.png')
2685
+ size = rand(@data['이미지설정']['이미지글자1크기1'].text.to_i..@data['이미지설정']['이미지글자1크기2'].text.to_i)
2651
2686
  rescue
2652
- puts '이미지 폰트 불러오기 오류 재시도...'
2653
- sleep(3)
2654
- retry
2687
+ size = 30
2688
+ end
2689
+
2690
+ wrapped_message, adjusted_size = wrap_text_to_fit(draw, raw_message, max_width, max_height, font2, size)
2691
+
2692
+ if @data['이미지설정']['글자그림자'].checked?
2693
+ img.annotate(draw, 0, 0, 2, 2, wrapped_message) do
2694
+ draw.gravity = Magick::CenterGravity
2695
+ draw.pointsize = adjusted_size
2696
+ draw.fill = '#000000'
2697
+ draw.font = font2
2698
+ end
2699
+ end
2700
+
2701
+ if @data['이미지설정']['글자테두리'].checked?
2702
+ draw_stroke = Magick::Draw.new
2703
+ img.annotate(draw_stroke, 0, 0, 0, 0, wrapped_message) do
2704
+ draw_stroke.gravity = Magick::CenterGravity
2705
+ draw_stroke.pointsize = adjusted_size
2706
+ draw_stroke.fill = 'none'
2707
+ draw_stroke.stroke = stroke_color
2708
+ draw_stroke.stroke_width = rand(5..10)
2709
+ draw_stroke.font = font2
2655
2710
  end
2711
+ end
2712
+
2713
+ draw2 = Magick::Draw.new
2714
+ img.annotate(draw2, 0, 0, 0, 0, wrapped_message) do
2715
+ draw2.gravity = Magick::CenterGravity
2716
+ draw2.pointsize = adjusted_size
2717
+ draw2.fill = color2
2718
+ draw2.stroke = 'none'
2719
+ draw2.font = font2
2720
+ end
2721
+
2722
+ img.write('./image/memory.png')
2723
+
2724
+ rescue => e
2725
+ puts "이미지 폰트 불러오기 오류 재시도... (#{e.message})"
2726
+ sleep(3)
2727
+ retry
2728
+ end
2656
2729
  end
2657
2730
 
2731
+
2732
+
2658
2733
  def border()
2659
2734
  color = File.open('./color.ini', 'r',:encoding => 'utf-8').read().split("\n")
2660
2735
  img = Magick::Image.read('./image/memory.png').first
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cafe_buy_duo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.51
4
+ version: 0.1.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - zon
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-06-25 00:00:00.000000000 Z
10
+ date: 2025-06-26 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: File to Clipboard gem
13
13
  email: mymin26@naver.com