Class: Breeze::Image

Inherits:
ActiveBase show all
Defined in:
app/models/breeze/image.rb

Instance Attribute Summary

Attributes inherited from ActiveYaml

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveBase

#add_save, #add_save!, #delete_save!, #edit_save, #edit_save!

Methods inherited from ActiveYaml

all, append, define_access, define_association, delete, fields, find, find_all, find_by, first, full_path, #id, #id=, #initialize, load_file, #persisted?, primary_key, reload, save_all, set_root_path, the_meta_class

Constructor Details

This class inherits a constructor from Breeze::ActiveYaml

Class Method Details

.asset_rootObject



99
100
101
# File 'app/models/breeze/image.rb', line 99

def self.asset_root
  "app/assets/images/" + Breeze.images_dir
end

.create_new(name, tags, io) ⇒ Object

save an io as new image. The filename is the id, type taken from io



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/breeze/image.rb', line 47

def self.create_new(name , tags,  io)
  original , end_ = io.original_filename.split("/").last.split(".")
  magick_image = MiniMagick::Image.read(io).auto_orient
  ending = magick_image.type.downcase
  ending = "jpg" if ending == "jpeg"
  name = original if( name.blank? )
  image = self.new_image_for( name: name , type: ending , tags: (tags || "") ,
            width: magick_image.width, height: magick_image.height ,
            size: (magick_image.size/1024).to_i )
  magick_image.write(image.full_filename)
  image
end

.new_image_for(image_data) ⇒ Object



60
61
62
63
# File 'app/models/breeze/image.rb', line 60

def self.new_image_for( image_data )
  new_id = Image.append(image_data)
  Image.new(image_data)
end

.transformObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/breeze/image.rb', line 78

def self.transform
  Image.all.each do |image|
    last = image.name.split.last
    image.tags = ""
    ["wide", "small" , "big" , "room"].each do |tag|
      if(last == tag)
        image.name = image.name.gsub(last,"").strip
        image.tags = last
      end
    end
    image.save
  end
end

Instance Method Details

#aspect_ratioObject



15
16
17
18
19
20
# File 'app/models/breeze/image.rb', line 15

def aspect_ratio
  ratio = self.ratio
  ratios = (1..9).collect{ |i|  ((ratio * i) - (ratio * i).round(0)).abs }
  min , min_index = ratios.each_with_index.min
  [(ratio *  (min_index + 1) ).round(0).to_i , (min_index + 1) ]
end

#asset_nameObject



69
70
71
# File 'app/models/breeze/image.rb', line 69

def asset_name
  Breeze.images_dir + "/" + self.id.to_s + "." + self.type
end

#change_nameObject



11
12
13
# File 'app/models/breeze/image.rb', line 11

def change_name
  name
end

#copyObject



27
28
29
30
31
32
33
# File 'app/models/breeze/image.rb', line 27

def copy()
  image = Image.new_image_for( name: "#{name}_copy" , type: type ,
                tags: (tags || "") , width: width ,
                height: height, size: size )
  FileUtils.cp full_filename , image.full_filename
  image
end

#crop(to_size) ⇒ Object



41
42
43
44
45
# File 'app/models/breeze/image.rb', line 41

def crop( to_size )
  mini = MiniMagick::Image.new(full_filename)
  mini.crop( to_size )
  init_file_size
end

#destroy(editor) ⇒ Object



64
65
66
67
# File 'app/models/breeze/image.rb', line 64

def destroy(editor)
  File.delete self.full_filename
  delete_save!(editor)
end

#full_filenameObject



73
74
75
76
# File 'app/models/breeze/image.rb', line 73

def full_filename
  full_filename = self.id.to_s + "." + self.type
  Rails.root.join(Image.asset_root, full_filename)
end

#init_file_sizeObject



92
93
94
95
96
97
# File 'app/models/breeze/image.rb', line 92

def init_file_size
  magick_image = MiniMagick::Image.open(full_filename)
  self.width = magick_image.width
  self.height = magick_image.height
  self.size = (magick_image.size/1024).to_i
end

#ratioObject



22
23
24
25
# File 'app/models/breeze/image.rb', line 22

def ratio
  return 0 unless self.height
  self.width.to_f / self.height
end

#scale(new_size) ⇒ Object



35
36
37
38
39
# File 'app/models/breeze/image.rb', line 35

def scale( new_size )
  mini = MiniMagick::Image.new( full_filename)
  mini.resize( new_size.to_s + "%")
  init_file_size
end