Class: Converted::PNG
- Inherits:
-
Object
- Object
- Converted::PNG
- Defined in:
- lib/converted/png.rb
Constant Summary collapse
- TMP_DIR =
Define the tmp directory path
File.join(Dir.pwd, 'tmp')
Instance Method Summary collapse
- #convert_to_avif(output_file = nil) ⇒ Object
- #convert_to_bmp(output_file = nil, with_transparency: false) ⇒ Object
- #convert_to_gif(output_file = nil) ⇒ Object
- #convert_to_ico(output_file = nil) ⇒ Object
- #convert_to_jpg(output_file = nil) ⇒ Object
- #convert_to_webp(output_file = nil) ⇒ Object
-
#initialize(input_file) ⇒ PNG
constructor
A new instance of PNG.
Constructor Details
#initialize(input_file) ⇒ PNG
Returns a new instance of PNG.
8 9 10 11 12 13 |
# File 'lib/converted/png.rb', line 8 def initialize(input_file) @input_file = input_file unless File.exist?(@input_file) raise "Error: Input file #{@input_file} does not exist." end end |
Instance Method Details
#convert_to_avif(output_file = nil) ⇒ Object
55 56 57 58 |
# File 'lib/converted/png.rb', line 55 def convert_to_avif(output_file = nil) output_file ||= tmp_file(change_extension(@input_file, 'avif')) run_ffmpeg_command("-c:v libaom-av1 -crf 30", output_file, "AVIF", white_background: false) end |
#convert_to_bmp(output_file = nil, with_transparency: false) ⇒ Object
60 61 62 63 64 |
# File 'lib/converted/png.rb', line 60 def convert_to_bmp(output_file = nil, with_transparency: false) output_file ||= tmp_file(change_extension(@input_file, 'bmp')) = with_transparency ? "" : "-vf 'format=bgr24'" run_ffmpeg_command(, output_file, "BMP", white_background: !with_transparency) end |
#convert_to_gif(output_file = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/converted/png.rb', line 20 def convert_to_gif(output_file = nil) output_file ||= tmp_file(change_extension(@input_file, 'gif')) palette_file = tmp_file('palette.png') unless ffmpeg_installed? raise "Error: ffmpeg is not installed or not in the system PATH." end FileUtils.mkdir_p(TMP_DIR) # Ensure tmp directory exists # Generate a palette for better GIF quality palettegen_command = "ffmpeg -i \"#{@input_file}\" -vf \"palettegen\" \"#{palette_file}\" -y" puts "Generating palette for GIF conversion..." system(palettegen_command) if File.exist?(palette_file) # Use the palette for the actual GIF conversion gif_command = "ffmpeg -i \"#{@input_file}\" -i \"#{palette_file}\" -filter_complex \"paletteuse\" \"#{output_file}\" -y" puts "Converting to GIF: #{output_file}..." if system(gif_command) puts "GIF conversion complete: #{output_file}" else puts "Error: GIF conversion failed." end File.delete(palette_file) if File.exist?(palette_file) # Clean up temporary palette file else puts "Error: Failed to generate palette for GIF conversion." end end |
#convert_to_ico(output_file = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/converted/png.rb', line 66 def convert_to_ico(output_file = nil) output_file ||= tmp_file(change_extension(@input_file, 'ico')) unless ffmpeg_installed? raise "Error: ffmpeg is not installed or not in the system PATH." end FileUtils.mkdir_p(TMP_DIR) # Ensure tmp directory exists # Resize to a standard ICO size (e.g., 256x256 for high-quality icons) resize_command = "ffmpeg -i \"#{@input_file}\" -vf \"scale=256:256:force_original_aspect_ratio=decrease,pad=256:256:(ow-iw)/2:(oh-ih)/2:white\" \"#{output_file}\" -y" puts "Converting to ICO with resizing: #{output_file}..." if system(resize_command) puts "ICO conversion complete: #{output_file}" else puts "Error: ICO conversion failed." end end |
#convert_to_jpg(output_file = nil) ⇒ Object
15 16 17 18 |
# File 'lib/converted/png.rb', line 15 def convert_to_jpg(output_file = nil) output_file ||= tmp_file(change_extension(@input_file, 'jpg')) run_ffmpeg_command("-vf 'format=yuvj422p,scale=trunc(iw/2)*2:trunc(ih/2)*2'", output_file, "JPG", white_background: true) end |
#convert_to_webp(output_file = nil) ⇒ Object
50 51 52 53 |
# File 'lib/converted/png.rb', line 50 def convert_to_webp(output_file = nil) output_file ||= tmp_file(change_extension(@input_file, 'webp')) run_ffmpeg_command("-c:v libwebp -lossless 1", output_file, "WEBP", white_background: false) end |