Class: Itools::ImgFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/itools/find_unuse_img.rb

Overview


Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImgFinder

Returns a new instance of ImgFinder.



17
18
19
20
21
# File 'lib/itools/find_unuse_img.rb', line 17

def initialize
   @image_count = 0
   @images = []
   @search_files = []
end

Instance Attribute Details

#find_pathObject

Returns the value of attribute find_path.



15
16
17
# File 'lib/itools/find_unuse_img.rb', line 15

def find_path
  @find_path
end

#image_countObject

Returns the value of attribute image_count.



15
16
17
# File 'lib/itools/find_unuse_img.rb', line 15

def image_count
  @image_count
end

#imagesObject

Returns the value of attribute images.



15
16
17
# File 'lib/itools/find_unuse_img.rb', line 15

def images
  @images
end

#search_filesObject

Returns the value of attribute search_files.



16
17
18
# File 'lib/itools/find_unuse_img.rb', line 16

def search_files
  @search_files
end

#unuse_imagesObject

Returns the value of attribute unuse_images.



15
16
17
# File 'lib/itools/find_unuse_img.rb', line 15

def unuse_images
  @unuse_images
end

Class Method Details

.find(temp_find_dir) ⇒ Object

查找



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
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/itools/find_unuse_img.rb', line 39

def self.find(temp_find_dir)
   imgFinder = ImgFinder.new
   imgFinder.find_path = temp_find_dir
   # 第一步:找到该文件夹下所有的图片文件
   Find.find(temp_find_dir) do |filename|
      if File.file?(filename)   #如果是文件,则从文件中查找,忽略文件夹
         if Image.is_image_format(File.extname(filename))
            # p File.basename(filename)
            # exit
            imgFinder.image_count = imgFinder.image_count + 1
            imageResult = FindResult.new(Image.get_image_name(File.basename(filename,".*")),filename)
            imgFinder.images << imageResult
         elsif File.extname(filename).eql?(".m")
            imgFinder.search_files << filename
         end
      end
   end
   if imgFinder.images.size == 0
      puts "\033[32m查找成功,未发现图片\033[0m"
      return
   else
      puts "\033[32m查找成功,共发现图片#{imgFinder.images.size}张\033[0m"
   end
   # 第二步:找到图片是否使用
   imags = imgFinder.get_img_name_strs.uniq   #要查找的图片名称数组

  puts "\033[32m需要查找的图片有#{imags.size}张\033[0m"
  #  imgFinder.search_files   #要查找的文件
   imgFinder.search_files.each {|file|
      File.read(file).each_line do |line|
          haveStr = StringHandle.containsStr(line,imags)
          if haveStr != -1
              puts "#{imags[haveStr]}在使用...,剩余查找项#{imags.size-1}"           
              imags.delete_at(haveStr)
         end
      end
   }
   puts "\033[32m无用图片#{imags.size}张,图片名称如下:\033[0m"
   unuse_total_size = 0
 
   Spreadsheet.client_encoding = 'utf-8'
   book = Spreadsheet::Workbook.new
   sheet1 = book.create_worksheet
   sheet1.row(0)[0] = "文件名"
   sheet1.row(0)[1] = "文件路径"
   sheet1.row(0)[2] = "文件大小(B)"
   imags.each_with_index {|item,idx|
      sheet1.row(idx+1)[0] = item
      path = imgFinder.get_image_path(item)
      sheet1.row(idx+1)[1] = path
      unuse_total_size = unuse_total_size + File.size(path)
      sheet1.row(idx+1)[2] = File.size(path)
      puts item
  }
   book.write "#{imgFinder.find_path}/search_result.xls"
   puts "\033[32m文件已经保存到#{imgFinder.find_path}/search_result.xls,无用图片大小:#{unuse_total_size}B\033[0m"
   puts "\033[32m内容仅供参考,具体还要自己通过结果查看一下\033[0m"
end

Instance Method Details

#get_image_path(image) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/itools/find_unuse_img.rb', line 31

def get_image_path(image)
   @images.each {|item|
      if item.name.eql?(image)
         return item.path
      end
   }
end

#get_img_name_strsObject

得到所有图片名称字符



23
24
25
26
27
28
29
30
# File 'lib/itools/find_unuse_img.rb', line 23

def get_img_name_strs
   result_arr = []
   @images.each {|item|
      item_name = Image.get_image_name(File.basename(item.name, ".*"))
      result_arr << item_name
   }
   return result_arr
end