下面贴出事例代码
#----图片上传 def otherimgupload(request): timepath=time.strftime('%Y/%m/%d/',time.localtime(time.time())) nowtime=int(time.time()) hostname = socket.gethostname() if request.FILES: file = request.FILES.getlist('file')#拿到上传文件 if not file: return None listall=[] for f in file: filetype=f.name.split(".")[-1]#获取文件类型 #视频上传 if (filetype.lower() in ["mp4","mov","avi","3gp","3gp2","wav","rm","mpg","asf","mid"]): mediatype="video" tmp = random.randint(100, 999)#产生随机数 newpath="/mnt/data/resources/other/"+timepath imgpath=newpath+str(nowtime)+str(tmp)+"."+filetype databasepath="other/"+timepath+str(nowtime)+str(tmp)+"."+filetype if not os.path.isdir(newpath): os.makedirs(newpath) des_origin_f = open(imgpath,"w")#拿到打开文件对象 for chunk in f.chunks(): des_origin_f.write(chunk)#将文件写入磁盘 des_origin_f.close()#关闭
picurl="http://"+hostname+"img.zz91.com/other/"+timepath+str(nowtime)+str(tmp)+"."+filetype else: mediatype="pic" suportFormat = ['BMP', 'GIF', 'JPG','JPGE', 'PNG','JPEG','jpg','mp4'] tempim = StringIO.StringIO() mstream = StringIO.StringIO(f.read()) im = Image.open(mstream) rheight=800 rwidth=800 pwidth=im.size[0] pheight=im.size[1] rate = int(pwidth/pheight) if rate==0: rate=1 nwidth=800 nheight=800 if (pwidth>rwidth): nwidth=rwidth nheight=nwidth /rate else: nwidth=pwidth nheight=pheight if (pheight>rheight): nheight=rheight nwidth=rheight*rate else: nwidth=pwidth nheight=pheight kzname=im.format if kzname not in ["mp4","mov","avi","3gp","3gp2","wav","rm","mpg","asf","mid"]: im.thumbnail((nwidth,nheight),Image.ANTIALIAS) #logo水印 if nwidth/2>130: wm = Image.open(nowpath+"/media/logo.png") layer = Image.new('RGBA', im.size, (0,0,0,0)) layer.paste(wm, (im.size[0] - 120, im.size[1] - 50)) im = Image.composite(layer, im, layer) #加字体水印 fontPath=nowpath+'/media/SIMHEI.TTF' fontSize=18 font = ImageFont.truetype(fontPath,fontSize) draw = ImageDraw.Draw(im) if nwidth/2>130: draw.text((nwidth/2-60,nheight/2-25),u'www.zz91.com',(0,0,0),font=font) draw.text((nwidth/2-70,nheight/2),unicode('ZZ91再生网专用','utf-8'),(0,0,0),font=font) del draw tmp = random.randint(100, 999) newpath="/mnt/data/resources/other/"+timepath imgpath=newpath+str(nowtime)+str(tmp)+"."+kzname databasepath="other/"+timepath+str(nowtime)+str(tmp)+"."+kzname if not os.path.isdir(newpath): os.makedirs(newpath) im.save(imgpath,kzname,quality = 80) mstream.closed tempim.closed #picurl="http://img4.zz91.com/300x15000/other/"+timepath+str(nowtime)+str(tmp)+"."+kzname picurl="http://"+hostname+"img.zz91.com/other/"+timepath+str(nowtime)+str(tmp)+"."+kzname #picurl="http://images.zz91.com/images.php?width=300&height=15000&picurl=http://img1.zz91.com/other/"+timepath+str(nowtime)+str(tmp)+"."+kzname picid=0 if source=="products": sql="insert into products_pic(product_id,pic_address,gmt_created) values(0,%s,%s)" result=dbc.updatetodb(sql,[databasepath,gmt_created]) if result: picid=result[0] # 发布与修改产品时,存入产品图片到产品相册 if company_id: sql = "insert into photo_abum(company_id,album_id,is_mark,pic_address,gmt_created," "gmt_modified) values(%s,%s,%s,%s,%s,%s)" dbc.updatetodb(sql, [company_id, 5, 1, databasepath, gmt_created, gmt_created]) else: sql="insert into other_piclist(path,source,gmt_created) values(%s,%s,%s)" result=dbc.updatetodb(sql,[databasepath,source,gmt_created]) if result: picid=result[0] # 手机端发布话题与修改头像,存入互助图片到产品相册 if company_id: sql = "insert into photo_abum(company_id,album_id,is_mark,pic_address,gmt_created," "gmt_modified) values(%s,%s,%s,%s,%s,%s)" dbc.updatetodb(sql, [company_id, 6, 1, databasepath, gmt_created, gmt_created]) list={'path':picurl,'id':picid,'databasepath':databasepath,'mediatype':mediatype} listall.append(list) return listall return None