From c626861546bc3e33b068d929174a7d478c4f2bab Mon Sep 17 00:00:00 2001 From: luxagraf Date: Sun, 27 Mar 2016 20:38:22 -0400 Subject: fixed photo resizer to work with gallery uploads --- app/photos/forms.py | 29 ++++++++++------------------- app/photos/models.py | 19 ++++++++++++++++--- 2 files changed, 26 insertions(+), 22 deletions(-) (limited to 'app/photos') diff --git a/app/photos/forms.py b/app/photos/forms.py index 03c6f09..16ba890 100644 --- a/app/photos/forms.py +++ b/app/photos/forms.py @@ -148,28 +148,19 @@ class UploadZipForm(forms.Form): ) contentfile = ContentFile(data) image.image.save(filename, contentfile) - image.save() - gallery.images.add(image) - readexif(image) - count += 1 img = Image.open(image.image.path) - base_path = "%s/galleries/" % settings.IMAGES_ROOT if img.size[0] > img.size[1]: - resize_image(img, 2280, None, 65, base_path+'large/', image.get_image_name()) - image.sizes.add(LuxImageSize.objects.get(size=2280)) - resize_image(img, 1140, None, 72, base_path+'medium/', image.get_image_name()) - image.sizes.add(LuxImageSize.objects.get(size=1140)) - resize_image(img, 720, None, 68, base_path+'small/', image.get_image_name()) - image.sizes.add(LuxImageSize.objects.get(size=720)) + image.sizes.add(LuxImageSize.objects.get(width=2280)) + image.sizes.add(LuxImageSize.objects.get(width=1140)) + image.sizes.add(LuxImageSize.objects.get(width=720)) if img.size[1] > img.size[0]: - resize_image(img, None, 1600, 65, base_path+'large/', image.get_image_name()) - image.sizes.add(LuxImageSize.objects.get(size=1600)) - resize_image(img, None, 800, 72, base_path+'medium/', image.get_image_name()) - image.sizes.add(LuxImageSize.objects.get(size=800)) - resize_image(img, None, 460, 60, base_path+'small/', image.get_image_name()) - image.sizes.add(LuxImageSize.objects.get(size=460)) - - resize_image(img, 160, None, 68, base_path+'thumb/', image.get_image_name()) + image.sizes.add(LuxImageSize.objects.get(width=1600)) + image.sizes.add(LuxImageSize.objects.get(width=800)) + image.sizes.add(LuxImageSize.objects.get(width=460)) + image.save() + gallery.images.add(image) + base_path = "%s/galleries/" % settings.IMAGES_ROOT + zipper.close() if request: diff --git a/app/photos/models.py b/app/photos/models.py index 6907a25..3ae58e4 100644 --- a/app/photos/models.py +++ b/app/photos/models.py @@ -10,6 +10,8 @@ from django.conf import settings from taggit.managers import TaggableManager from locations.models import Location, Region +from resizeimage.imageexceptions import ImageSizeError + from .utils import resize_image from .readexif import readexif from django.db.models.signals import post_save @@ -121,13 +123,24 @@ def post_save_events(sender, update_fields, created, instance, **kwargs): @receiver(m2m_changed, sender=LuxImage.sizes.through) def update_photo_sizes(sender, instance, **kwargs): - print("hellow world") base_path = "%s/%s/" % (settings.IMAGES_ROOT, instance.pub_date.strftime("%Y")) img = Image.open(instance.image.path) resize_image(img, 160, None, 78, base_path, "%s_tn.%s" % (instance.get_image_name(), instance.get_image_ext())) for size in instance.sizes.all(): - print(size.width) - resize_image(img, size.width, None, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), size.width, instance.get_image_ext())) + if img.size[0] > img.size[1]: + try: + resize_image(img, size.width, None, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), size.width, instance.get_image_ext())) + except ImageSizeError: + m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through) + instance.sizes.remove(size) + m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through) + if img.size[1] > img.size[0]: + try: + resize_image(img, None, size.width, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), size.width, instance.get_image_ext())) + except ImageSizeError: + m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through) + instance.sizes.remove(size) + m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through) class LuxGallery(models.Model): -- cgit v1.2.3-70-g09d2