rabin 7 years ago
parent
commit
5c3f10cdca
2 changed files with 36 additions and 3 deletions
  1. 29 1
      admin/page/upload.py
  2. 7 2
      front/api/upload.py

+ 29 - 1
admin/page/upload.py

@@ -9,6 +9,7 @@ from __load__ import *
 from datetime import *
 import os
 import uuid
+from PIL import Image
 
 class upload_path(Load):
  	@Web.auth
@@ -16,6 +17,7 @@ class upload_path(Load):
 	def post(self, *args, **kwargs):
 		url = self.request.protocol + "://" + self.request.host
 		file_metas = self.request.files["file"]
+		crop = self.input('crop', 1)
 
 		# print(file_metas)
 		day = str(date.today())
@@ -27,4 +29,30 @@ class upload_path(Load):
 			file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
 			with open(file_path, 'wb') as up:
 				up.write(meta['body'])
-		self.out('yes', {'src':url + file_path.replace(Demeter.path + 'runtime', '')})
+		if crop:
+			#裁图类型,暂时居中裁图
+			file_path = cropImage(file_path)
+		self.out('yes', {'src':url + file_path.replace(Demeter.path + 'runtime', '')})
+
+def cropImage(file):
+	img = Image.open(file)
+	width = img.size[0]
+	height = img.size[1]
+	size = 0
+	x = 0
+	y = 0
+	if width == height:
+		return file
+	elif width < height:
+		size = width
+		x = 0
+		y = (height-width)/2
+	else:
+		size = height
+		x = (width-height)/2
+		y = 0
+	#img.thumbnail((500, 500))
+	crop = img.crop((x, y, size+x, size+y))
+	file = file + '_crop_center.jpg'
+	crop.save(file)
+	return file

+ 7 - 2
front/api/upload.py

@@ -58,15 +58,20 @@ def cropImage(file):
 	width = img.size[0]
 	height = img.size[1]
 	size = 0
+	x = 0
+	y = 0
 	if width == height:
 		return file
 	elif width < height:
 		size = width
+		x = 0
+		y = (height-width)/2
 	else:
 		size = height
+		x = (width-height)/2
+		y = 0
 	#img.thumbnail((500, 500))
-	left = width/4
-	crop = img.crop((left, 0, size+left, size))
+	crop = img.crop((x, y, size+x, size+y))
 	file = file + '_crop_center.jpg'
 	crop.save(file)
 	return file