rails crop attachment_fu

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 :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值