:art: make_thumbnail2

huangqimin001 6 months ago
parent
commit
ba682c98ba
2 changed files with 27 additions and 1 deletions
  1. 3 1
      api/lensman/contract_mp_views.py
  2. 24 0
      utils/thumbnail_utils.py

+ 3 - 1
api/lensman/contract_mp_views.py

@@ -17,6 +17,7 @@ from utils.error.errno_utils import ContractStatusCode
17 17
 from utils.redis.rimage import get_images_data
18 18
 from utils.tencentcloud.ess import (callback_decode, create_document, create_flow, create_scheme_url, start_flow,
19 19
                                     test_upload_document_files, upload_document_files)
20
+from utils.thumbnail_utils import make_thumbnail2
20 21
 
21 22
 
22 23
 @logit(res=True)
@@ -99,6 +100,7 @@ def get_contribtion_contract_sign_mppath_api(request):
99 100
 def generate_file_from_qiniu(file_url):
100 101
     try:
101 102
         data = requests.get(file_url).content
103
+        data = make_thumbnail2(data)
102 104
         data = base64.b64encode(data).decode('utf-8')
103 105
     except Exception:
104 106
         data = None
@@ -165,7 +167,7 @@ def create_contribution_contract_document(lensman, contribution_id, file_ids, Fl
165 167
     # 创建电子签文档 https://qian.tencent.com/developers/companyApis/startFlows/CreateDocument
166 168
     IMAGE_COMPONENTIDS = ['ComponentId_37', 'ComponentId_38','ComponentId_39','ComponentId_40','ComponentId_41','ComponentId_42',
167 169
                            'ComponentId_43','ComponentId_44','ComponentId_45','ComponentId_46','ComponentId_47','ComponentId_48','ComponentId_49','ComponentId_50','ComponentId_51','ComponentId_52','ComponentId_53','ComponentId_54','ComponentId_55', 'ComponentId_55', 'ComponentId_56', 'ComponentId_57', 'ComponentId_58', 'ComponentId_59', 'ComponentId_60', 'ComponentId_9', 'ComponentId_10', 'ComponentId_11', 'ComponentId_12', 'ComponentId_13', 'ComponentId_14', 'ComponentId_15', 'ComponentId_16', 'ComponentId_17', 'ComponentId_18', 'ComponentId_19', 'ComponentId_20', 'ComponentId_21', 'ComponentId_22', 'ComponentId_23', 'ComponentId_24', 'ComponentId_25', 'ComponentId_26', 'ComponentId_27', 'ComponentId_28','ComponentId_29', 'ComponentId_30', 'ComponentId_31', 'ComponentId_32']
168
-     
170
+
169 171
     try:
170 172
         amount = LensmanContributionActivityIncomeExpensesInfo.objects.get(contribution_id=contribution_id, lensman_id=lensman.lensman_id).amount
171 173
     except LensmanContributionActivityIncomeExpensesInfo.DoesNotExist:

+ 24 - 0
utils/thumbnail_utils.py

@@ -4,6 +4,15 @@ from __future__ import division
4 4
 
5 5
 
6 6
 try:
7
+    from cStringIO import StringIO
8
+    from cStringIO import StringIO as BytesIO
9
+except ImportError:
10
+    try:
11
+        from StringIO import StringIO
12
+        from StringIO import StringIO as BytesIO
13
+    except ImportError:
14
+        from io import BytesIO, StringIO
15
+try:
7 16
     from PIL import Image
8 17
 except ImportError:
9 18
     import Image
@@ -17,3 +26,18 @@ def make_thumbnail(im_path, im_thumbnail_path=None, max_width=360):
17 26
     im.thumbnail((thumb_width, thumb_height), Image.ANTIALIAS)
18 27
     im.save(im_thumbnail_path or im_path, im.format or 'JPEG', quality=90)
19 28
     return width, height, thumb_width, thumb_height
29
+
30
+
31
+def make_thumbnail2(data, w=360, h=240):
32
+    im = Image.open(BytesIO(data))
33
+    fmt = im.format.lower()
34
+    width, height = im.size
35
+    if width > height:
36
+        thumb_width, thumb_height = w, height * w / width
37
+    else:
38
+        thumb_width, thumb_height = width * h / height, h
39
+    im.thumbnail((thumb_width, thumb_height), Image.ANTIALIAS)
40
+    out = BytesIO()
41
+    im.save(out, format=fmt)
42
+    data = out.getvalue()
43
+    return data