How can I create a processor that uses the value of another field in a model?

66 views
Skip to first unread message

Hilton Campbell

unread,
Dec 10, 2013, 12:45:09 PM12/10/13
to django-...@googlegroups.com
I'd like to create a processor for use in ImageSpecField that pulls a value from another field in the model. Specifically, I'd like the processor to crop the image, where the crop region is specified in a CharField ("left,top,right,bottom").

I tried but I have no idea how to do it. I then attempted to subclass ImageSpecField, with the idea that I could pass in the name of the crop field. I quickly got discouraged as I don't understand the internals of these classes.

Any idea how to go about this?

matthewwithanm

unread,
Dec 10, 2013, 1:21:37 PM12/10/13
to django-...@googlegroups.com
Hey Hilton! ImageSpec constructors get the source file (from which the model can be extracted), so you can accomplish this by writing a custom ImageSpec. There's a small example in the docstring of the get_field_info utility.

Hilton Campbell

unread,
Dec 10, 2013, 9:53:04 PM12/10/13
to django-...@googlegroups.com
Thanks for the great and fast response! I didn't realize there was more documentation than the Installation page... thanks to your suggestion, I found the Advanced Usage page and was able to accomplish what I wanted. Thanks for such a great library!


On Tue, Dec 10, 2013 at 11:21 AM, matthewwithanm <matthew...@gmail.com> wrote:
Hey Hilton! ImageSpec constructors get the source file (from which the model can be extracted), so you can accomplish this by writing a custom ImageSpec. There's a small example in the docstring of the get_field_info utility.

--
You received this message because you are subscribed to a topic in the Google Groups "Django ImageKit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-imagekit/SJxeG2xKuUM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-imagek...@googlegroups.com.
To post to this group, send email to django-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-imagekit/a6e43409-b8b3-4126-b665-1199631ab088%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

matthewwithanm

unread,
Dec 10, 2013, 9:55:53 PM12/10/13
to django-...@googlegroups.com
Glad I could help. Thanks for the kind words!

Nina Pavlich

unread,
May 12, 2015, 2:13:45 PM5/12/15
to django-...@googlegroups.com
Hi Matthew, Hi Hilton,

I have follow-up question to Hilton's. (I just posted a topic here: https://groups.google.com/forum/#!topic/django-imagekit/eDD4kBkLPNc and then realized that I'm essentially tying to do exactly what Hilton described.)

The only thing I'm not sure about is if the value on the model changed -- for example if the thumbnail_width or thumbnail_height changed -- would this trigger an update on the ImageSpecField? Is there a recommended way to handle that situation? 

Thanks!

matthewwithanm

unread,
May 12, 2015, 10:16:03 PM5/12/15
to django-...@googlegroups.com, ni...@cgpartnersllc.com
Hi Nina! Yeah, this will cause a new image to be generated. The processors are hashed to create the filename so any change you make to them will result in a new filename/file.

Hope that helps!
Reply all
Reply to author
Forward
0 new messages