tblog_zon 0.0.52 → 0.0.55

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/tblog_zon.rb +191 -111
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59e32026de6913b1c3a5136ed8eb6c0a97b0ccd96f29e12a927ae9af4c6929cf
4
- data.tar.gz: bb764327f7e69a61971315a1f612fda67b569926ea5dcf6917aa63634e787a58
3
+ metadata.gz: 674cd1bee02e91efd1d217ace77de8cc5143aadf9e97a7d1e6a3a37287100602
4
+ data.tar.gz: 5865b2572fb16ab3826fa78ff6b6e58017f558a855e2216dae05b5b00aa90b26
5
5
  SHA512:
6
- metadata.gz: f63664a68e49b5a00bc0f388ac7678f8674efc953d098062778729beb9272d4fc6b1abb5d126e869f699e5784c8fa17b571c275bad55fcb1657fd862fad8c279
7
- data.tar.gz: 91db21efda1c057656f5e39784cee829586e615ad0efe432ad9cc75797857215ec98bc6240243cdfd63a8738c0b17c20f207bc5d9fb01139a3b62ae49a68ce68
6
+ metadata.gz: 24c84d17c6f9a35a7e4d07427c79a702fa0177d496574902c2f5036c61ca6be639484fb10166afb730c0e3800c52a22e56e0ad0e37aac88aa2e5960f9a02bc8c
7
+ data.tar.gz: c7c75c38c0c962a3d4eac1da29b5d036465a623c1e9d7a60a73dcb66ac1ee99d082f9dad08e2f7de284b8fe438dd468fbbc29bf737f19dc43604fcd9be622824
data/lib/tblog_zon.rb CHANGED
@@ -1686,84 +1686,116 @@ class Wordpress
1686
1686
 
1687
1687
 
1688
1688
 
1689
- def auto_image(keyword = nil)
1690
- keyword ||= @keyword
1691
- puts "키워드: #{keyword}"
1692
-
1693
- client = HTTPClient.new
1694
- client.default_header = {
1695
- 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '\
1696
- '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
1697
- 'Accept' => 'application/json, text/javascript, */*; q=0.01',
1698
- 'Accept-Language' => 'en-US,en;q=0.9',
1699
- 'Referer' => "https://unsplash.com/s/photos/#{URI.encode_www_form_component(keyword)}",
1700
- 'X-Requested-With' => 'XMLHttpRequest'
1701
- }
1689
+ def crop_image_height_under_width(path, min_crop_ratio = 0.625)
1690
+ img = Magick::Image.read(path).first
1691
+ width = img.columns
1692
+ height = img.rows
1702
1693
 
1703
- retry_count = 0
1704
- max_retries = 10
1705
- results = []
1694
+ if height > width
1695
+ min_height = (width * min_crop_ratio).to_i
1696
+ new_height = rand(min_height..width)
1697
+ crop_top = ((height - new_height) / 2.0).round
1706
1698
 
1707
- begin
1708
- page = rand(1..15)
1709
- url = "https://unsplash.com/napi/search/photos?query=#{URI.encode_www_form_component(keyword)}&page=#{page}&per_page=20"
1710
- puts "Request URL: #{url}"
1711
- res = client.get(url)
1712
-
1713
- unless res.status == 200
1714
- puts "HTTP Error: #{res.status}"
1715
- raise "HTTP Error"
1699
+ cropped = img.crop(0, crop_top, width, new_height, true)
1700
+
1701
+ retries = 0
1702
+ begin
1703
+ cropped.write(path)
1704
+ rescue => e
1705
+ retries += 1
1706
+ puts "이미지 저장 오류 (#{e.message}), 재시도 #{retries}/5"
1707
+ sleep(1)
1708
+ retry if retries < 5
1709
+ raise "이미지 저장 실패: #{e.message}"
1716
1710
  end
1711
+ end
1712
+ end
1717
1713
 
1718
- json = JSON.parse(res.body)
1719
- results = json['results']
1720
- mm = []
1714
+ def auto_image(keyword = nil)
1715
+ # auto_image 내부에서만 crop 호출
1716
+ keyword ||= @keyword
1717
+ puts "키워드: #{keyword}"
1718
+
1719
+ client = HTTPClient.new
1720
+ client.default_header = {
1721
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '\
1722
+ '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
1723
+ 'Accept' => 'application/json, text/javascript, */*; q=0.01',
1724
+ 'Accept-Language' => 'en-US,en;q=0.9',
1725
+ 'Referer' => "https://unsplash.com/s/photos/#{URI.encode_www_form_component(keyword)}",
1726
+ 'X-Requested-With' => 'XMLHttpRequest'
1727
+ }
1728
+
1729
+ retry_count = 0
1730
+ max_retries = 10
1731
+ results = []
1732
+
1733
+ begin
1734
+ page = rand(1..15)
1735
+ url = "https://unsplash.com/napi/search/photos?query=#{URI.encode_www_form_component(keyword)}&page=#{page}&per_page=20"
1736
+ puts "Request URL: #{url}"
1737
+ res = client.get(url)
1738
+
1739
+ unless res.status == 200
1740
+ puts "HTTP Error: #{res.status}"
1741
+ raise "HTTP Error"
1742
+ end
1721
1743
 
1722
- results.each do |photo|
1723
- full_url = photo.dig('urls', 'full').to_s
1724
- regular_url = photo.dig('urls', 'regular').to_s
1744
+ json = JSON.parse(res.body)
1745
+ results = json['results']
1746
+ mm = []
1725
1747
 
1726
- if full_url.start_with?("https://images.unsplash.com/photo-") &&
1727
- regular_url.include?("1080")
1728
- mm << full_url
1729
- end
1730
- end
1748
+ results.each do |photo|
1749
+ full_url = photo.dig('urls', 'full').to_s
1750
+ regular_url = photo.dig('urls', 'regular').to_s
1731
1751
 
1732
- if mm.empty?
1733
- raise "No matching image"
1734
- end
1752
+ if full_url.start_with?("https://images.unsplash.com/photo-") &&
1753
+ regular_url.include?("1080")
1754
+ mm << full_url
1755
+ end
1756
+ end
1735
1757
 
1736
- selected_url = mm.sample
1737
- Down.download(selected_url, destination: "./image/memory.png")
1738
- puts "이미지 다운로드 완료: #{selected_url}"
1758
+ if mm.empty?
1759
+ raise "No matching image"
1760
+ end
1739
1761
 
1740
- rescue => e
1741
- retry_count += 1
1742
- puts "auto_image 에러: #{e.message} (재시도 #{retry_count}/#{max_retries})"
1743
- sleep(3)
1744
- if retry_count < max_retries
1745
- retry
1746
- else
1747
- puts "최대 재시도 초과. 조건 무시하고 랜덤 이미지 다운로드 시도..."
1748
-
1749
- if results && !results.empty?
1750
- random_photo = results.sample
1751
- fallback_url = random_photo.dig('urls', 'full')
1752
- if fallback_url
1753
- Down.download(fallback_url, destination: "./image/memory.png")
1754
- puts "랜덤 이미지 다운로드 완료: #{fallback_url}"
1755
- else
1756
- puts "랜덤 이미지 URL을 찾을 수 없습니다. 단색 배경 이미지 생성합니다."
1757
- color_image
1758
- end
1762
+ selected_url = mm.sample
1763
+ destination_path = "./image/memory.png"
1764
+ Down.download(selected_url, destination: destination_path)
1765
+ puts "이미지 다운로드 완료: #{selected_url}"
1766
+
1767
+ # 오직 auto_image에서만 자르기 호출
1768
+ crop_image_height_under_width(destination_path)
1769
+
1770
+ rescue => e
1771
+ retry_count += 1
1772
+ puts "auto_image 에러: #{e.message} (재시도 #{retry_count}/#{max_retries})"
1773
+ sleep(3)
1774
+ if retry_count < max_retries
1775
+ retry
1776
+ else
1777
+ puts "최대 재시도 초과. 조건 무시하고 랜덤 이미지 다운로드 시도..."
1778
+
1779
+ if results && !results.empty?
1780
+ random_photo = results.sample
1781
+ fallback_url = random_photo.dig('urls', 'full')
1782
+ if fallback_url
1783
+ Down.download(fallback_url, destination: "./image/memory.png")
1784
+ puts "랜덤 이미지 다운로드 완료: #{fallback_url}"
1785
+ crop_image_height_under_width("./image/memory.png")
1759
1786
  else
1760
- puts "이미지 결과가 없어 다운로드할 수 없습니다. 단색 배경 이미지 생성합니다."
1761
- color_image
1762
- end
1787
+ puts "랜덤 이미지 URL을 찾을 수 없습니다. 단색 배경 이미지 생성합니다."
1788
+ color_image
1763
1789
  end
1790
+ else
1791
+ puts "이미지 결과가 없어 다운로드할 수 없습니다. 단색 배경 이미지 생성합니다."
1792
+ color_image
1764
1793
  end
1794
+ end
1795
+ end
1765
1796
  end
1766
1797
 
1798
+
1767
1799
  def color_image
1768
1800
  color = File.open('./color.ini', 'r', :encoding => 'utf-8').read().split("\n")
1769
1801
  image = Magick::Image.new(740, 740) { |k| k.background_color = color.sample }
@@ -1856,56 +1888,103 @@ class Wordpress
1856
1888
  end
1857
1889
 
1858
1890
 
1859
- def image_text(text1, text2)
1891
+ def image_text(text1, text2)
1892
+ begin
1893
+ color = File.open('./color.ini', 'r', encoding: 'utf-8').read.split("\n").map(&:strip).reject(&:empty?)
1894
+ font_files = Dir.entries('./fonts').select { |f| f.downcase.end_with?('.ttf') }
1895
+ font2 = './fonts/' + font_files.sample
1896
+
1897
+ # 랜덤 글자색 선택
1898
+ color2 = color.sample
1899
+
1900
+ # 헬퍼 함수: 색상 문자열 '#RRGGBB' -> [R,G,B] 배열로 변환
1901
+ def hex_to_rgb(hex)
1902
+ hex = hex.delete('#')
1903
+ [hex[0..1], hex[2..3], hex[4..5]].map { |c| c.to_i(16) }
1904
+ end
1905
+
1906
+ # 헬퍼 함수: 두 RGB 색상의 차이 계산 (간단한 유클리드 거리)
1907
+ def color_distance(c1, c2)
1908
+ Math.sqrt(
1909
+ (c1[0] - c2[0])**2 +
1910
+ (c1[1] - c2[1])**2 +
1911
+ (c1[2] - c2[2])**2
1912
+ )
1913
+ end
1914
+
1915
+ # 대비가 충분히 되는 테두리 색상 선택
1916
+ max_attempts = 10
1917
+ stroke_color = nil
1918
+ base_rgb = hex_to_rgb(color2)
1919
+
1920
+ max_attempts.times do
1921
+ candidate = color.sample
1922
+ candidate_rgb = hex_to_rgb(candidate)
1923
+ dist = color_distance(base_rgb, candidate_rgb)
1924
+
1925
+ # 거리(차이) 임계값 100 (0~441 범위) — 필요시 조절 가능
1926
+ if dist > 100
1927
+ stroke_color = candidate
1928
+ break
1929
+ end
1930
+ end
1931
+ stroke_color ||= '#000000' # 만약 충분히 다른 색 없으면 검정색 기본값
1932
+
1933
+ img = Magick::Image.read('./image/memory.png').first
1934
+ draw = Magick::Draw.new
1935
+
1936
+ raw_message = "#{text1}\n#{text2}".strip
1937
+ max_width = img.columns * 0.85
1938
+ max_height = img.rows * 0.6
1939
+
1860
1940
  begin
1861
- color = File.open('./color.ini', 'r', :encoding => 'utf-8').read().split("\n")
1862
- font_files = Dir.entries('./fonts').select { |f| f.downcase.end_with?('.ttf') }
1863
- font2 = './fonts/' + font_files.sample
1864
- color2 = color.sample
1865
-
1866
- img = Magick::Image.read('./image/memory.png').first
1867
- draw = Magick::Draw.new
1868
-
1869
- raw_message = "#{text1}\n#{text2}".strip
1870
- max_width = img.columns * 0.85
1871
- max_height = img.rows * 0.6
1872
-
1873
- begin
1874
- size = rand(@data['이미지설정']['이미지글자1크기1'].text.to_i..@data['이미지설정']['이미지글자1크기2'].text.to_i)
1875
- rescue
1876
- size = 30
1877
- end
1878
-
1879
- wrapped_message, adjusted_size = wrap_text_to_fit(draw, raw_message, max_width, max_height, font2, size)
1880
-
1881
- if @data['이미지설정']['글자그림자'].checked?
1882
- img.annotate(draw, 0, 0, 2, 2, wrapped_message) do
1883
- draw.gravity = Magick::CenterGravity
1884
- draw.pointsize = adjusted_size
1885
- draw.fill = '#000000'
1886
- draw.font = font2
1887
- end
1888
- end
1889
-
1890
- draw2 = Magick::Draw.new
1891
- img.annotate(draw2, 0, 0, 0, 0, wrapped_message) do
1892
- draw2.gravity = Magick::CenterGravity
1893
- draw2.pointsize = adjusted_size
1894
- draw2.fill = color2
1895
- draw2.font = font2
1896
- if @data['이미지설정']['글자테두리'].checked?
1897
- draw2.stroke_width = 2
1898
- draw2.stroke = '#000000'
1899
- end
1900
- end
1901
-
1902
- img.write('./image/memory.png')
1941
+ size = rand(@data['이미지설정']['이미지글자1크기1'].text.to_i..@data['이미지설정']['이미지글자1크기2'].text.to_i)
1903
1942
  rescue
1904
- puts '이미지 폰트 불러오기 오류 재시도...'
1905
- sleep(3)
1906
- retry
1943
+ size = 30
1944
+ end
1945
+
1946
+ wrapped_message, adjusted_size = wrap_text_to_fit(draw, raw_message, max_width, max_height, font2, size)
1947
+
1948
+ if @data['이미지설정']['글자그림자'].checked?
1949
+ img.annotate(draw, 0, 0, 2, 2, wrapped_message) do
1950
+ draw.gravity = Magick::CenterGravity
1951
+ draw.pointsize = adjusted_size
1952
+ draw.fill = '#000000'
1953
+ draw.font = font2
1954
+ end
1955
+ end
1956
+
1957
+ if @data['이미지설정']['글자테두리'].checked?
1958
+ draw_stroke = Magick::Draw.new
1959
+ img.annotate(draw_stroke, 0, 0, 0, 0, wrapped_message) do
1960
+ draw_stroke.gravity = Magick::CenterGravity
1961
+ draw_stroke.pointsize = adjusted_size
1962
+ draw_stroke.fill = 'none'
1963
+ draw_stroke.stroke = stroke_color
1964
+ draw_stroke.stroke_width = rand(5..10)
1965
+ draw_stroke.font = font2
1966
+ end
1967
+ end
1968
+
1969
+ draw2 = Magick::Draw.new
1970
+ img.annotate(draw2, 0, 0, 0, 0, wrapped_message) do
1971
+ draw2.gravity = Magick::CenterGravity
1972
+ draw2.pointsize = adjusted_size
1973
+ draw2.fill = color2
1974
+ draw2.stroke = 'none'
1975
+ draw2.font = font2
1907
1976
  end
1977
+
1978
+ img.write('./image/memory.png')
1979
+
1980
+ rescue => e
1981
+ puts "이미지 폰트 불러오기 오류 재시도... (#{e.message})"
1982
+ sleep(3)
1983
+ retry
1908
1984
  end
1985
+ end
1986
+
1987
+
1909
1988
 
1910
1989
  def border()
1911
1990
  color = File.open('./color.ini', 'r',:encoding => 'utf-8').read().split("\n")
@@ -1932,7 +2011,7 @@ class Wordpress
1932
2011
  else
1933
2012
  auto_image()
1934
2013
  end
1935
-
2014
+
1936
2015
  # '원본'을 포함한 이미지 크기 옵션 추가
1937
2016
  image_size = [480, 740, 650, 550, 480, 'original']
1938
2017
  size = 0
@@ -1956,6 +2035,7 @@ class Wordpress
1956
2035
 
1957
2036
  change_image_size(size) # 크기 변경 함수 호출
1958
2037
 
2038
+
1959
2039
  if @data['이미지설정']['필터사용'].checked?
1960
2040
  image_filter()
1961
2041
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tblog_zon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.52
4
+ version: 0.0.55
5
5
  platform: ruby
6
6
  authors:
7
7
  - zon
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-05-30 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