I have form with image field and my upload function:
def handle_uploaded_file(f, gallery):
directory = settings.GALLERIES_ROOT+'gal_'+str(gallery.id)+'/'
# check if directory exist
try:
if not os.path.exists(directory):
os.makedirs(directory)
except Exception, e:
print 'DEBUG (directory): ', e
# find next pic number, to improve
i = 1
for r,d,f in os.walk(directory):
for file in f:
if fl.startswith("pic"):
i += 1
extension = os.path.splitext(f.name.replace(" ", "_"))[1]
filename = "pic%03d%s" % (i, extension)
# saving
try:
with open(directory + pf + filename, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
return True
except Exception, e:
print 'DEBUG (file writing problem): ', e
return False
and now I want to resize uploaded image and save them to file ico[number].[extension] Image may be png or jpg, portrait or landscape. How to do this with aspect ratio (eventually cropped longer part after reduction)?