Class: PicPacPut

Inherits:
Object
  • Object
show all
Defined in:
lib/picpacput.rb,
lib/picpacput/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PicPacPut

Returns a new instance of PicPacPut.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/picpacput.rb', line 13

def initialize( opts = {} )
  options = {
    :folder => ('.'+File::SEPARATOR),
    :recursive => true,
    :extension => "*.JPG",
    :scale_by => 0.05,
    :zipFile => 'output.zip',
    :outfolder => ('resized'+File::SEPARATOR),
    :ext_types => ["png","PNG","jpg","JPG","jpeg","JPEG","gif","GIF","bmp","BMP"]
  }.merge(opts)
  @folder = options[:folder]
  @recursive = options[:recursive]
  @extension = options[:extension]
  @scale_by = options[:scale_by]
  @outfolder = options[:outfolder]
  @zipFile = options[:zipFile]
  @ext_types = options[:ext_types]
  Dir.mkdir(@outfolder) unless File.exists?(@outfolder)
end

Instance Attribute Details

#ext_typesObject

Returns the value of attribute ext_types.



11
12
13
# File 'lib/picpacput.rb', line 11

def ext_types
  @ext_types
end

#extensionObject

Returns the value of attribute extension.



11
12
13
# File 'lib/picpacput.rb', line 11

def extension
  @extension
end

#folderObject

Returns the value of attribute folder.



11
12
13
# File 'lib/picpacput.rb', line 11

def folder
  @folder
end

#outfolderObject

Returns the value of attribute outfolder.



11
12
13
# File 'lib/picpacput.rb', line 11

def outfolder
  @outfolder
end

#recursiveObject

Returns the value of attribute recursive.



11
12
13
# File 'lib/picpacput.rb', line 11

def recursive
  @recursive
end

#scale_byObject

Returns the value of attribute scale_by.



11
12
13
# File 'lib/picpacput.rb', line 11

def scale_by
  @scale_by
end

Instance Method Details

#pacObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/picpacput.rb', line 79

def pac
  Zip::ZipFile.open( @outfolder + File::SEPARATOR + @zipFile, Zip::ZipFile::CREATE) do |zipfile|
    Dir.glob( @outfolder + File::SEPARATOR + '*' ).each do |filename|
      filename.gsub!(File::SEPARATOR*2, File::SEPARATOR)
      if File.directory?(filename) or !!filename[@zipFile]
        next
      end
      zipfile.add(filename, filename)
    end
  end
end

#picObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/picpacput.rb', line 59

def pic
  if not @recursive
    return thumbit
  end
  dirList = []
  Find.find(@folder) do |f|
    if !!f[@outfolder]
      next
    end
    @ext_types.each do |type|
      ext = f.match(".#{type}")
      if not ext.nil?
        dirList << f
 thumbOne(f)# maybe CWD + directory
      end
    end
  end
  dirList
end

#thumbitObject



49
50
51
52
53
54
55
56
57
# File 'lib/picpacput.rb', line 49

def thumbit    
  if !!@extension["*"] # blob operator
    Dir.glob("#{@folder + @extension}") do |f|
      thumbOne(f)
    end
  else
    thumbOne      
  end
end

#thumbOne(image_in = @folder + @extension) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/picpacput.rb', line 33

def thumbOne(image_in = @folder + @extension )
  puts "STATUS #{image_in}"
  if File.exists?(image_in)
    image = Magick::Image.read(image_in).first
    new_image = image.scale(@scale_by)
    if not File.exists?(@outfolder + image.filename.split(File::SEPARATOR)[-1])
      new_image.write(@outfolder + image.filename.split(File::SEPARATOR)[-1])
    else
      puts "File #{ @outfolder + image.filename } already exists!  Please change your output."
    end
    [image,new_image].each { |img| img.destroy! } # IMPORTANT!!!
  else
    raise "File #{image_in} does not exist!"
  end
end