def update
@face = Face.find_by_user_id(session[:user_id],:conditions => "parent_id is null" )
respond_to do |format|
if @face.update_attributes(params[:face])
# flash[:notice] = 'Face was successfully updated.'
# format.html { redirect_to(edit_user_path(@face.user,:web_name => "face")) }
format.html { redirect_to(:controller => "houses", :action => "show", :id => session[:house_id] ) }
format.xml { head
k }
else
format.html { render :action => "edit" }
format.xml { render
ml => @face.errors, :status => :unprocessable_entity }
end
end
end
def update_attributes(att)
# path = "/home/sunchi/work/demo/swfupload-rails-authentication/public/assets/0000/0050/1521572725770.jpg"
path = File.expand_path(RAILS_ROOT)+"/public"+self.public_filename(:big)
bash_path = File.expand_path(RAILS_ROOT)+"/public"+self.public_filename
# scaled_img = Magick::ImageList.new(self.path)
# orig_img = Magick::ImageList.new(self.path(:original))
scaled_img = Magick::ImageList.new(path) # 缩略图
orig_img = Magick::ImageList.new(bash_path) # 原图
scale = orig_img.columns.to_f / scaled_img.columns # 按照缩略图于原图缩放比例 进行裁剪
args = [ att[:x1], att[:y1], att[:width], att[:height] ]
args = args.collect { |a| a.to_i * scale }
orig_img.crop!(*args)
# orig_img.write(self.path(:original))
orig_img.write(bash_path)
# self.reprocess!
self.save!
super(att)
end
<div class="fam_h_m">
<div class="head_bg">
<div class="font003 head_line">上传头像</div>
<div style="margin-top: 10px;">
<% form_for @face, :method => :put do |f| -%>
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tbody><tr>
<td width="280">
<input type="hidden" name="face[x1]" id="x1" />
<input type="hidden" name="face[y1]" id="y1" />
<input type="hidden" name="face[width]" id="width" />
<input type="hidden" name="face[height]" id="height" />
<p>
<div style="width:280px;height:325px;"><%= image_tag(crop_face.public_filename(:big), :style => 'max-height: 325px; max-width: 280px;', :id => "photo")%></div>
</p>
<div style="float: left; height: 20px; line-height: 20px;"></div>
</td>
<td valign="top">
<div class="head_c">
<p>
</p><div style="height: 40px; line-height: 40px;"><div style="float: left; margin-top: 10px;"></div> </div>
<br>
<div style="width: 100%;"></div>
<p><br>
<br>
<br>
<br>
<span>预览</span>
</p><p> <div id="preview" style="margin-left:40px;overflow:hidden;position:relative; "><span><%= image_tag('yulan.png', :style => "z-index:10001;width:100px;height:100px;")%></span></div></p>
<br />
<div style="margin-top: 30px; margin-left: 40px;">
<!-- <a class="button white" type="submit" id="face_submit" >保存</a> -->
<%= f.submit("保存", :class=> "button white" , :style => "background:-moz-linear-gradient(center top , #FFFFFF, #EDEDED) repeat scroll 0 0 transparent;") %>
</div>
</div>
</td>
</tr>
</tbody></table>
<% end %>
</div>
</div>
</div>
<script type="text/javascript">
function onEndCrop( coords, dimensions ) {
$( 'x1' ).value = coords.x1;
$( 'y1' ).value = coords.y1;
$( 'width' ).value = dimensions.width;
$( 'height' ).value = dimensions.height;
}
Event.observe( window, 'load',function() {
new Cropper.ImgWithPreview('photo',{
minWidth: 118,
minHeight: 115,
ratioDim: { x:100, y: 100 },
displayOnInit: true,
onEndCrop: onEndCrop,
previewWrap: 'preview'
});
});
</script>