Last active
August 20, 2016 11:06
-
-
Save nakamasato/10e18cb3c626c51d27ac to your computer and use it in GitHub Desktop.
rails 4.2, paperclip, Jcropを使ってユーザが写真切り取りし保存する機能をつける ref: http://qiita.com/gymnstcs/items/69d319a6415a53a6576e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//= require jquery.Jcrop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*= require jquery.Jcrop.min | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:javascript | |
$(function(){ | |
$("#cropbox").Jcrop({//サイズ変更があったら、アップデートする | |
onChange: update_crop, | |
onSelect: update_crop, | |
setSelect: [0,0,300,300], | |
aspectRatio: 1 | |
}); | |
}); | |
function update_crop(coords){//アップデートする関数で、 | |
var rx = 100/coords.w; | |
var ry = 100/coords.h; | |
$("#preview").css({//プレビューを更新する。 | |
width: Math.round(rx * #{ @user.image_geometry(:medium).width }) + 'px', | |
height: Math.round(ry * #{ @user.image_geometry(:medium).height }) + 'px', | |
marginLeft: '-' + Math.round(rx * coords.x) + 'px', | |
marginTop: '-' + Math.round(ry * coords.y) + 'px' | |
}); | |
var ratio = #{@user.image_geometry(:original).width}/#{@user.image_geometry(:medium).width}; | |
$("#crop_x").val(coords.x * ratio);//クロップするための4つの変数をformの中で更新する。 | |
$("#crop_y").val(coords.y * ratio);// | |
$("#crop_w").val(coords.w * ratio);// | |
$("#crop_h").val(coords.h * ratio);// | |
} | |
= image_tag @user.image.url(:medium), id: "cropbox" | |
%h4 プレビュー | |
%div{style: "width:100px; height:100px; overflow:hidden"} | |
= image_tag @user.image.url(:medium), id: "preview" | |
= form_for @user do |f| | |
.form-group | |
- for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] | |
= f.hidden_field attribute, id: attribute | |
%p= f.submit '送信', class: 'btn btn-primary' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update | |
@user = User.find_by(account: params[:id]) | |
if @user.update_attributes(user_params) | |
if params[:user][:image].blank?#画像がないときは、今までどおり更新 | |
flash[:success] = "更新されました" | |
redirect_to @user | |
else#画像があるときは、Cropに飛ばす。 | |
render :crop | |
end | |
else | |
render :edit | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attr_accessor .... , :crop_x, :crop_y, :crop_w, :crop_h | |
after_update :reprocess_image, if: :cropping?#クロップするための位置情報が送られてきたら、updateの後に、reprocess_imageというPrivateメソッドを呼ぶ | |
... | |
has_attached_file :image, styles: {medium: "300x300>", thumb: "100x100#", icon: "40x40#"}, processors: [:cropper] | |
# mediumを使ってCropするので、”>”を使う。"#"を使うと直接真ん中部分を正方形に切り取って、保存されてしまうため、切り取り作業を行うときに、すでに正方形のものからしか切り取れなくなる! | |
# processors: [:cropper]は4に書く、paperclipをoverrideするClass名である。(この名前は任意) | |
def cropping?# クロップするための位置情報が送られてきたかチェック | |
crop_x && crop_y && crop_w && crop_h | |
end | |
def image_geometry(style=:original) | |
@geometry ||= {} | |
@geometry[style] ||= Paperclip::Geometry.from_file(image.path(style)) | |
end | |
... | |
private | |
def reprocess_image # ここが、RailsCastと違う!! | |
image.assign(image) | |
image.save | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Paperclip | |
class Cropper < Thumbnail | |
def transformation_command | |
if crop_command | |
crop_command + super.join(' ').sub(/ -crop \S+/, '').split(" ") | |
else | |
super | |
end | |
end | |
def crop_command | |
target = @attachment.instance | |
if target.cropping? | |
[" -crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"] | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
convert input.png -crop 100x100+0+10 output.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment