How to set the maximum image size to upload image in django-ckeditor?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping
--
Chapters
00:00 How To Set The Maximum Image Size To Upload Image In Django-Ckeditor?
01:08 Answer 1 Score 0
02:02 Accepted Answer Score 0
03:02 Thank you
--
Full question
https://stackoverflow.com/questions/6313...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #djangomodels #djangoviews #djangockeditor
#avk47
ANSWER 1
Score 0
I think there should be a way to resize the picture on uploading. But to be honest I didn't find nor in documentation neither by debugging.
However, I can suggest you a work-around to post-process all the images inside the content upon saving of Post model.
models.py
from django.conf import settings
from PIL import Image
def resize_image(filename):
"""
here goes resize logic.
For example, I've put 50% of current size if width>=900.
Might be something more sophisticated
"""
im = Image.open(filename)
width, height = im.size
if width >= 500:
im = im.resize((int(width * 0.5), int(height * 0.5)))
im.save(filename)
class Post(models.Model):
...
body = RichTextUploadingField(blank=True, null=True)
...
def save(self, *args, **kwargs):
for token in self.body.split():
# find all <img src="/media/.../img.jpg">
if token.startswith('src='):
filepath = token.split('=')[1].strip('"') # get path of src
filepath = filepath.replace('/media', settings.MEDIA_ROOT) # resolve path to MEDIA_ROOT
resize_image(filepath) #do resize in-place
super().save(*args, **kwargs)
The solution is not super nice because it resizes picture only after it was uploaded.
Probably the library itself should have some kind of exit-point/callback for image pre-processing when uploaded.
ACCEPTED ANSWER
Score 0
You can set CKEDITOR_THUMBNAIL_SIZE in setting.py
CKEDITOR_THUMBNAIL_SIZE = (500, 500)
With the pillow backend, you can change the thumbnail size with the CKEDITOR_THUMBNAIL_SIZE setting (formerly THUMBNAIL_SIZE). Default value: (75, 75)
With the pillow backend, you can convert and compress the uploaded images to jpeg, to save disk space. Set the CKEDITOR_FORCE_JPEG_COMPRESSION setting to True (default False) You can change the CKEDITOR_IMAGE_QUALITY setting (formerly IMAGE_QUALITY), which is passed to Pillow:
The image quality, on a scale from 1 (worst) to 95 (best). The default is 75. Values above 95 should be avoided; 100 disables portions of the JPEG compression algorithm and results in large files with hardly any gain in image quality.
This feature is disabled for animated images.
check official doc. https://github.com/django-ckeditor/django-ckeditor/blob/master/README.rst