Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#already_done?(file) ⇒ Boolean



20
21
22
23
24
# File 'lib/smushkid.rb', line 20

def already_done?(file)
    simg = Magick::Image::read(file).first
    puts "checking for EXIF value"
    simg.properties.has_value?("smushkid")
end

#process_file(file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smushkid.rb', line 26

def process_file(file)
    simg = Magick::Image::read(file).first
    @source_image = {
             'format' => simg.format,
             'filesize' => simg.filesize,
             'geometry_cols' => simg.columns,
             'geometry_rows' => simg.rows,
             'resolution_x' =>  simg.x_resolution.to_i,
             'resolution_y' => simg.y_resolution.to_i,
             'depth' =>  simg.depth,
             'colors' => simg.number_colors,
             'ppi' => simg.units
    }
    simg.strip!
    simg.quantize 32
    simg.write(@target_file) do
        self.quality = @quality.to_i
    end
    timg = Magick::Image::read(@target_file).first
    @target_image = {
             'format' => timg.format,
             'filesize' => timg.filesize,
             'geometry_cols' => timg.columns,
             'geometry_rows' => timg.rows,
             'resolution_x' =>  timg.x_resolution.to_i,
             'resolution_y' => timg.y_resolution.to_i,
             'depth' =>  timg.depth,
             'colors' => timg.number_colors,
             'ppi' => timg.units
    }
    @savings =  @source_image['filesize'] - @target_image['filesize']
    @image_results = { "filename" => file, "savings" => @savings, "before" => @source_image, "after" => @target_image }
    puts "savings: " + @savings.to_s unless @quiet
    if @savings < 0
      @savings = 0
      puts "no space savings acheived deleting #{@target_file}" unless @quiet
      puts "tagging original file" unless @quiet
      @quoted_filename = "\"#{file}\""
      %x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
      File.delete(@target_file)
    else
        if @make_backup
          puts "space savings! replacing with: #{simg} and making backup or original: #{@backup_file}" unless @quiet
          puts "tagging target file" unless @quiet
          %x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
          puts "move #{@target_file} to #{file}" unless @quiet
          FileUtils.cp file, @backup_file
          File.rename @target_file, file
          File.open("images_processed.json", "a+") { |f| f << @image_results.to_json + ','}
        else
          puts "space savings! replacing with #{simg}" unless @quiet
          puts "tagging target file" unless @quiet
          %x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
          puts "move #{@target_file} to #{file}" unless @quiet
          File.rename @target_file, file
          File.open("images_processed.json", "a+") { |f| f << @image_results.to_json + ','}
        end
    end
end