Module: Crimagify::ImageFunctions

Defined in:
lib/crimagify/image_functions.rb

Instance Method Summary collapse

Instance Method Details

#create_new_images(object, params = nil, nested = false) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/crimagify/image_functions.rb', line 199

def create_new_images(object, params = nil, nested = false)
	
	if !params.nil?
		if nested
			params.each do |key, value|
				if value[:_destroy] == 'false'
					destroy_attribute = false
				else
					destroy_attribute = true
				end
				if !destroy_attribute
					parent = value[:parent]
					parent_id = value[:parent_id]
					save_parent_values = value.clone
					id_array = []

					value.each do |key|
						name = key[0].split("_")						
						if name.length.to_i == 3 && "#{name[0]}_#{name[1]}" == "image_temporal"
							id_array << name[2].to_s
						end
						id_array.each do |image_name|
							if key[0].match(image_name)
								save_parent_values.delete(key[0].to_sym)
							end
						end
					end
					save_parent_values.delete(:parent)
					save_parent_values.delete(:parent_id)
					save_parent_values.delete(:id_images)
					save_parent_values.delete(:id)
					save_parent_values.delete(:_destroy)


					save_parent_values["#{object.class.name.underscore}_id"] = object.id
					parent_class = parent.constantize
					save_parent = parent_class.new(save_parent_values)

					if save_parent.save
						id_array.map { |image_name| 
							path = value["image_temporal_#{image_name}"]

							if path.to_s != "" && File.exist?(path.to_s)
								img = save_new_image(path.to_s,
													 					 value["#{image_name}_crop_x"],
													 					 value["#{image_name}_crop_y"],
													 					 value["#{image_name}_crop_w"],
													 					 value["#{image_name}_crop_h"],
													 					 save_parent.class.name,
													 					 save_parent.id,
													 					 image_name,
													 					 false)
								img.save!
								img.crop_avatar_real
							end
						}
					end
				end											
			end				
		else
			id_array = params[:id_images].split(",")
			id_array.map { |image_name|
				path = params["image_temporal_#{image_name}"]
				if !(path.to_s == "") && File.exist?(path.to_s)
					img = save_new_image(path.to_s,
										 					 params["#{image_name}_crop_x"],
										 					 params["#{image_name}_crop_y"],
										 					 params["#{image_name}_crop_w"],
										 					 params["#{image_name}_crop_h"],
										 					 object.class.name,
										 					 object.id,
										 					 image_name,
										 					 false)
					img.save!
					img.crop_avatar_real
				end
			}
		end	
	end					
end

#get_data_parent(parent, parent_id) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/crimagify/image_functions.rb', line 30

def get_data_parent(parent, parent_id)
	if !(parent_id.to_i == 0)
		parent_model = parent.constantize
		data = parent_model.find(parent_id.to_i)
    else
      data = 0
    end
    return data
end

#save_new_image(path, x, y, w, h, parent_type, parent_id, image_name, temporal) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/crimagify/image_functions.rb', line 40

def save_new_image(path, x, y, w, h, parent_type, parent_id, image_name, temporal)
	data = {:crop_x => x,
			:crop_y => y,
			:crop_w => w,
			:crop_h => h,
			:parent_type => parent_type,
			:parent_id => parent_id,
			:image_name => image_name
	}
	data[(temporal ? :image_temporal : :image)] = File.open(path)

	return Image.new(data)
end

#update_images(object, params = {}, nested = false) ⇒ Object



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/crimagify/image_functions.rb', line 54

def update_images(object, params = {}, nested = false)
	if nested
		params.each do |key, value|

			if value[:_destroy] == 'false'
				destroy_attribute = false
			else
				destroy_attribute = true
			end

			parent = value[:parent]
			parent_id = value[:parent_id]
			save_parent_values = value.clone
			id_array = []

			value.each do |key|
				name = key[0].split("_")
				if name.length.to_i == 3 && "#{name[0]}_#{name[1]}" == "image_temporal"
					id_array << name[2].to_s
				end
				id_array.each do |image_name|
					if key[0].match(image_name)
						save_parent_values.delete(key[0].to_sym)
					end
				end					
			end

			save_parent_values.delete(:parent)
			save_parent_values.delete(:parent_id)
			save_parent_values.delete(:id_images)
			save_parent_values.delete(:id)
			save_parent_values.delete(:_destroy)

			save_parent_values["#{object.class.name.underscore}_id"] = object.id
			parent_class = parent.constantize
			if parent_id == '' && destroy_attribute == false
				save_parent = parent_class.new(save_parent_values)
				if save_parent.save
					if id_array != []
						id_array.map { |image_name|  
							path = value["image_temporal_#{image_name}"]
							if path.to_s != "" && File.exist?(path.to_s)
								image = save_parent.crimagify_images.where("image_name=?", image_name)
								if image != []
									image.map { |img|  
										img.update_attributes(:image => File.open(path.to_s),
																					:crop_x => value["#{image_name}_crop_x"],
																					:crop_y => value["#{image_name}_crop_y"],
																					:crop_w => value["#{image_name}_crop_w"],
																					:crop_h => value["#{image_name}_crop_h"])
										img.crop_avatar_real
									}
								else
									img = save_new_image(path,
																			 value["#{image_name}_crop_x"],
																			 value["#{image_name}_crop_y"],
																			 value["#{image_name}_crop_w"],
																			 value["#{image_name}_crop_h"],
																			 save_parent.class.name,
																			 save_parent.id,
																			 image_name,
																			 false)
									img.save!
									img.crop_avatar_real
								end
							end
						}
					end						
				end
			else
				object_parent_image = parent_class.find(parent_id)
				if destroy_attribute
					object_parent_image.destroy
				else							
					if object_parent_image.update_attributes(save_parent_values)
						if id_array != []
							id_array.map { |image_name|  
								path = value["image_temporal_#{image_name}"]
								if path.to_s != "" && File.exist?(path.to_s)
									image = object_parent_image.crimagify_images.where("image_name=?", image_name)
									if image != []
										image.map { |img|  
											img.update_attributes(:image => File.open(path.to_s),
																						:crop_x => value["#{image_name}_crop_x"],
																						:crop_y => value["#{image_name}_crop_y"],
																						:crop_w => value["#{image_name}_crop_w"],
																						:crop_h => value["#{image_name}_crop_h"])
											img.crop_avatar_real
										}
									else
										img = save_new_image(path,
																				 value["#{image_name}_crop_x"],
																				 value["#{image_name}_crop_y"],
																				 value["#{image_name}_crop_w"],
																				 value["#{image_name}_crop_h"],
																				 object_parent_image.class.name,
																				 object_parent_image.id,
																				 image_name,
																				 false)
										img.save!
										img.crop_avatar_real
									end
								end
							}
						end						
					end
				end						
			end					
		end
	else
		if params[:id_images] != ""
			id_array = params[:id_images].split(",")
			id_array.map { |image_name|
				path = params["image_temporal_#{image_name}"]
				if !(path.to_s == "") && File.exist?(path.to_s)
					image = object.crimagify_images.where("image_name=?", image_name)
					if !(image == [])
						image.map { |img|  
							img.update_attributes(:image => File.open(path.to_s),
												  :crop_x => params["#{image_name}_crop_x"],
												  :crop_y => params["#{image_name}_crop_y"],
												  :crop_w => params["#{image_name}_crop_w"],
												  :crop_h => params["#{image_name}_crop_h"])
							img.crop_avatar_real
						}
					else
						img = save_new_image(path,

											 params["#{image_name}_crop_x"],
											 params["#{image_name}_crop_y"],
											 params["#{image_name}_crop_w"],
											 params["#{image_name}_crop_h"],
											 object.class.name,
											 object.id,
											 image_name,
											 false)
						img.save!
						img.crop_avatar_real
					end
				end
			}				
		end	
	end					
end

#write_tmp_image(image, parent_id, parent_type, image_name, parent_fieldset = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/crimagify/image_functions.rb', line 4

def write_tmp_image(image, parent_id, parent_type, image_name, parent_fieldset = nil)
    require 'base64'
    image_encoded = image.split(",")      
    extension = image_encoded[0].split("/")[1].split(";")[0]

    today = Time.now
    generate_name = "#{today.strftime("%Y-%m-%d")}-#{parent_type}-#{image_name}-#{parent_id}"

    path = nil
    if parent_fieldset.nil?
    	path = "#{Rails.root}/app/assets/images/tmps_cropper/#{parent_type}/image_temporal/#{parent_id}/#{image_name}"
    else
    	path = "#{Rails.root}/app/assets/images/tmps_cropper/#{parent_type}/image_temporal/#{parent_id}/#{parent_fieldset}/#{image_name}"
    end

    if !File.exist?(path)
      FileUtils.mkdir_p(path)
    end

    File.open("#{path}/#{generate_name}.#{extension}", "wb") do |item|
      item << (Base64.decode64(image_encoded[1]))
    end

    return "#{path}/#{generate_name}.#{extension}"
end