をwrited非常に困難な変化であり、されていますあなたが大きなアプリを持っていても、他のコードを曲げようともっと多くの時間を費やすことになります(もしあなたが作物を保存したいと思っている場合)。状況。
(このコードは非常に粗いです - 私は本当に手順をレイアウトしています)あなたはのImageFieldでモデルを持っている場合、あなたはトリミングされた画像を保持するための第二の画像フィールドを追加することができます
を:
class MyModel(models.Model):
image = models.ImageField(...)
image_crop = models.ImageField(...)
フォームとクライアント側のフォームに入力されるjcrop座標を保持する余分なフィールドを持つフォーム(フィールドは非表示になります)。どのような形式で座標をフィールドに保存するかはあなた次第ですが、json辞書(クライアント側のjson.jsとサーバー側のsimplejson)を使用するといいでしょう。
{ 'x1' : '145', 'y1' : '200' ... }
形式:
class MyModelForm(form.ModelForm):
""" Hide a field to hold the coordinates chosen by the user """
crop_coords = forms.CharField(attrs={'style':'display:none'})
class Meta:
model = MyModel
ビュー全てこの処理:PIL使用してトリミングされた画像を作成する
def some_view(request):
form = request.POST
if form.is_valid():
crop_coords = form.cleaned_data['crop_coords']
# decode the coords using simpleson (or however you passed them)
...
# create a cropped image
original_image = form.cleaned_data['image']
cropped_image = cropper(original_image.path, crop_coords)
...
# save it back to the db - http://stackoverflow.com/questions/1308386/programmatically-saving-image-to-django-imagefield
...
と機能:
# Look here: http://djangosnippets.org/snippets/224/
def cropper(original_image_path, crop_coords):
""" Open original, create and return a new cropped image
...
django-image-croppingについて:ImageCropField(https://github.com/jonasundderwolf/django-image-cropping/blob/master/image_cropping/fields.py)を見ると、基本的に標準的な画像フィールドを取り、管理者用のウィジェットを追加するだけです。 そして、ImageRatioFieldは座標をカンマで区切った文字列で保存するので、pastylegsの提案とあまり違いはありません。 ImageCropFieldとして、既存のアプリケーションの標準イメージフィールドを簡単に再宣言できるはずです。またはあなたの心配は何ですか(「非常に結合しました」)? – arie