CJ 1 anno fa
parent
commit
f91c7e5448
3 ha cambiato i file con 8 aggiunte e 4 eliminazioni
  1. 6 3
      blog/controller/files.py
  2. 1 0
      templates/blog.html
  3. 1 1
      templates/index.html

+ 6 - 3
blog/controller/files.py

@@ -12,6 +12,7 @@ from django.views.decorators.clickjacking import xframe_options_sameorigin
 from blog import models
 from system import settings
 from system.error.ServerException import ServerException
+from PIL import Image
 
 
 @xframe_options_sameorigin
@@ -23,10 +24,13 @@ def upload_markdown_img(request):
     except Exception as e:
         return JsonResponse({"errno": 1, "message": str(e)})
     from PIL import Image
-
     img = Image.open(file_record.file_path + file_record.file_name)
     width, height = img.size
-    print('Image size: {}x{}'.format(width, height))
+
+    if int(os.path.getsize(file_record.file_path + file_record.file_name) / 1024) > 1024:
+        img.save(file_record.file_path + file_record.file_name, quality=75)  # The saved downsized image size is 24.8kb
+        img.close()
+        print('Image size: {}x{}'.format(width, height))
 
     return JsonResponse({"errno": 0, "data": {
         'url': file_record.file_net_path,
@@ -53,7 +57,6 @@ def save_file(file_obj):
         filepath = os.path.join(settings.UPLOAD_ROOT + '/' + file_type + '/')
         if not os.path.exists(filepath):
             os.mkdir(filepath)
-
         file_net_path = settings.DOMAIN + settings.UPLOAD_URL + '/' + file_type + '/' + filename
 
         f = open(filepath + filename, 'wb')

+ 1 - 0
templates/blog.html

@@ -233,6 +233,7 @@
     </div>
 </div>
 
+
 <footer style="margin-top: 50px" class="ui secondary segment">
     <div class="ui two column stackable grid">
         <div class="ten wide column">

+ 1 - 1
templates/index.html

@@ -120,7 +120,7 @@
                 elemToHtml: function imageToHtml(elemNode) {
                     const {src, alt, href = '', style = {}, width, height} = elemNode || {}
 
-                    return `<a data-pswp-width="${alt.split(',')[1]}" data-pswp-height="${alt.split(',')[1]}" href="${src}"><img data-magnify="gallery" data-src="${src}"  src="${src}"  data-href="${href}" "/></a>`
+                    return `<a data-pswp-width="${alt.split(',')[0]}" data-pswp-height="${alt.split(',')[1]}" href="${src}"><img data-magnify="gallery" data-src="${src}"  src="${src}"  data-href="${href}" "/></a>`
                 }
             };