Browse Source

素材匹配广告位

Signed-off-by: jiapeng.dong <jiapeng.dong@cloudcross.com>
jiapeng.dong 9 years ago
parent
commit
d355f881a8

+ 125 - 134
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/BannerTemplateController.java

@@ -223,6 +223,11 @@ public class BannerTemplateController extends SimpleController {
 		//缩略图路径
 		String pathSmall;
 		String responseStr="null";
+		int width;
+		int height;
+		//广告位宽高
+		int _width;
+		int _height;
 		Account loginUser = getLoginUser();
 		Long agentId = loginUser.getAgentId();
 	 
@@ -266,25 +271,44 @@ public class BannerTemplateController extends SimpleController {
 					//上传目录不存在,则新建目录
 					if (!localFile.getParentFile().exists()){
 						localFile.getParentFile().mkdirs();
-					}	
-				file.transferTo(localFile);
-				//取上传文件父级目录(此处可能将缩略图移到save中生成)
+					}
+					//生成上传文件
+					file.transferTo(localFile);
+					//取上传图片的宽高
+					 BufferedImage image = ImageIO.read(new FileInputStream(path));
+					 width = image.getWidth();
+					 height = image.getHeight();
+					 
+					 //匹配广告位
+					 bannerTemplate=suitSize(width,height, bannerTemplate);
+					//图片匹配
+						if(bannerTemplate==null){
+							return "图片尺寸错啦!!";
+						}
+					 _width =Integer.parseInt(bannerTemplate.getWidth().toString()) ;
+					 _height = Integer.parseInt(bannerTemplate.getHeight().toString());	
+					
+				//取上传文件父级目录
 				String absolutePath = localFile.getParentFile().getAbsolutePath();
 				//生成缩略图路径名
 				pathSmall = absolutePath.concat(File.separator).concat("small_").concat(uploadName);
 				//生成缩略图
-				smallPicture(localFile,pathSmall,uploadName);
-
+				compressPicture(localFile,pathSmall,150,60);
+				//判断是否需要缩放
+				if((width*_height)==(_width*height)){
+					//压缩文件上传路径
+					 String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(uploadName);
+					 //生成压缩图
+					 compressPicture(localFile,pathSmall, _width,_height);
+					//设置文件上传路径
+					 bannerTemplate.setPath(replacePath(compressPath));
+				 }else{
+					 //设置文件上传路径
+					 bannerTemplate.setPath(replacePath(path));
+				 }
 				responseStr="上传成功";
-				//设置上传文件路径
-				bannerTemplate.setPath(replacePath(path));
 				//设置缩略图文件路径
 				bannerTemplate.setPathSmall(replacePath(pathSmall));
-				//创建素材
-				if(suitSize(path, bannerTemplate)==null){
-					return "图片有点大了啊!";
-				}
-				bannerTemplate= suitSize(path, bannerTemplate);
 				bannerTemplate.setName(uploadName);
 				bannerTemplate.setType(judgeType(fileType));
 				bannerTemplate.setStatus(0);
@@ -317,6 +341,12 @@ public class BannerTemplateController extends SimpleController {
 		//缩略图路径
 		String pathSmall;
 		String responseStr="null";
+		//图片宽高
+		int width;
+		int height;
+		//广告位宽高
+		int _width;
+		int _height;
 		BannerTemplate bannerTemplate =bannerTemplateService.findById(bannerTemplateId);
 	if(request.getMethod()=="POST"){ 
 	
@@ -342,10 +372,6 @@ public class BannerTemplateController extends SimpleController {
 				 
 				//如果名称不为“”,说明该文件存在,否则说明该文件不存在  
 				if(uploadName.trim() !=""){  
-
-//					//命名上传后的缩略图文件名  
-//					String fileName = "small_".concat(file.getOriginalFilename()); 
-					
 					
 					//处理上传文件路径
 					path = getLocalFilePath(uploadName);
@@ -355,25 +381,40 @@ public class BannerTemplateController extends SimpleController {
 					if (!localFile.getParentFile().exists()){
 						localFile.getParentFile().mkdirs();
 					}	
-				file.transferTo(localFile);
-				//取上传文件父级目录(此处可能将缩略图移到save中生成)
-				String absolutePath = localFile.getParentFile().getAbsolutePath();
-				//生成缩略图路径名
-				pathSmall = absolutePath.concat(File.separator).concat("small_").concat(uploadName);
-				//生成缩略图
-				smallPicture(localFile,pathSmall,uploadName);
-
-				//设置上传文件路径
-				bannerTemplate.setPath(replacePath(path));
-				//设置缩略图文件路径
-				bannerTemplate.setPathSmall(replacePath(pathSmall));
-				//设置素材宽高
-				if(suitSize(path, bannerTemplate)==null){
-					return "图片有点大了啊!";
-				}
-				bannerTemplate= suitSize(path, bannerTemplate);
-				bannerTemplate.setName(uploadName);
-				bannerTemplate.setType(judgeType(fileType));
+					//生成上传文件
+					file.transferTo(localFile);
+					//取上传图片的宽高
+					 BufferedImage image = ImageIO.read(new FileInputStream(path));
+					 width = image.getWidth();
+					 height = image.getHeight();
+					 _width =Integer.parseInt(bannerTemplate.getWidth().toString()) ;
+					 _height = Integer.parseInt(bannerTemplate.getHeight().toString());
+					//取上传文件父级目录
+					String absolutePath = localFile.getParentFile().getAbsolutePath();
+					//生成缩略图路径名
+					pathSmall = absolutePath.concat(File.separator).concat("small_").concat(uploadName);
+					//生成缩略图
+					compressPicture(localFile,pathSmall,150,60);
+					
+					if((width*_height)==(_width*height)){
+						//压缩文件上传路径
+						 String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(uploadName);
+						 //生成压缩图
+						 compressPicture(localFile,pathSmall, _width,_height);
+						//设置文件上传路径
+						 bannerTemplate.setPath(replacePath(compressPath));
+					 }else{
+						 if(width>_width||height>_height){
+							 return "图片尺寸错啦!!";
+						 }
+						 //设置文件上传路径
+						 bannerTemplate.setPath(replacePath(path));
+					 }
+				
+					//设置缩略图文件路径
+					bannerTemplate.setPathSmall(replacePath(pathSmall));
+					bannerTemplate.setName(uploadName);
+					bannerTemplate.setType(judgeType(fileType));
 				//更新素材
 				bannerTemplateService.edit(bannerTemplate);
 				responseStr="上传成功";
@@ -388,57 +429,7 @@ public class BannerTemplateController extends SimpleController {
 		}	
 		  return responseStr; 
 	}
-	 //取素材的大小
-	public BannerTemplate setWidthAndHeight(BannerTemplate bannerTemplate,String urls,String fileType){
-	    //本地上传的文件
-					 if("jpeg".equalsIgnoreCase(fileType)||"jpg".equalsIgnoreCase(fileType) ||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType)){
-						 //把文件读取到本地	
-						 File file = new File(urls);
-						 	FileInputStream istream = null;
-							try {
-								istream = new FileInputStream(file);
-							} catch (FileNotFoundException e1) {
-								// TODO Auto-generated catch block
-								e1.printStackTrace();
-							}
-							BufferedImage image=null;
-							try {
-								image = javax.imageio.ImageIO.read(istream);
-							} catch (IOException e) {
-								e.printStackTrace();
-							}
-							if(image!=null){
-								bannerTemplate.setWidth((long)image.getWidth());
-								bannerTemplate.setHeight((long)image.getHeight());
-							}else{
-								System.out.println("aaaa");
-							}
-							try {
-								istream.close();
-							} catch (IOException e) {
-								
-								e.printStackTrace();
-							}
-					 }else if("swf".equalsIgnoreCase(fileType)){      //swf文件类型的设置
-						 File file=new File(urls);
-							SWFHeader swfh = new SWFHeader();
-							try {
-								swfh = SWFHeader.load(file);
-								bannerTemplate.setWidth((long)swfh.getWidth());
-								bannerTemplate.setHeight((long)swfh.getHeight());
-							} catch (IOException e) {
-								// TODO Auto-generated catch block
-								
-								e.printStackTrace();
-							} catch (IllegalAccessException e) {
-								// TODO Auto-generated catch block
-								
-								e.printStackTrace();
-							}
-					 }
-
-		return bannerTemplate;
-	}
+	
 	
 
 	
@@ -479,13 +470,8 @@ public String getLocalFilePath(String fileName){
 //处理路径为web路径
 	public String replacePath(String path){
 		
-		
-//		String url = config.getResourceUploadDomain()
-//							   .concat(":")
-//							   .concat(config.getResourceUploadPort())
-//							   .concat("/");
 		path = path.substring(path.indexOf("upload")-1);
-//		path = path.replace(top, url);
+
 	return path;
 	}			
 					
@@ -502,38 +488,33 @@ public int judgeType(String fileType){
 					return type;
 				}
 
-//生成缩略图
-public void smallPicture(File localFile,String pathSmall,String uploadName) throws IOException{
-	//新建缩略图文件
-	File smallFile = new File(pathSmall);
-	if (!smallFile.getParentFile().exists()){
-		smallFile.getParentFile().mkdirs();
-	}	
-	//构造image图片
-	Image src = javax.imageio.ImageIO.read(localFile);
-	/**得到原图的宽高
-	int width = src.getWidth(null);
-	int height =src.getHeight(null);*/
-	//表示该图像具有打包成整数的像素的8 位RGB颜色分量
-	BufferedImage tag = new BufferedImage(150, 60, BufferedImage.TYPE_INT_BGR);
-	//h绘制缩小后的图片
-	tag.getGraphics().drawImage(src, 0, 0, 150, 60, null);
 	//生成缩略图
-	ImageIO.write(tag, "png", smallFile);
-}
+	public void compressPicture(File localFile,String pathNew,int width,int height) throws IOException{
+		//新建缩略图文件
+		File newFile = new File(pathNew);
+		if (!newFile.getParentFile().exists()){
+			newFile.getParentFile().mkdirs();
+		}	
+		//构造image图片
+		Image src = javax.imageio.ImageIO.read(localFile);
+		//表示该图像具有打包成整数的像素的8 位RGB颜色分量
+		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
+		//h绘制缩小后的图片
+		tag.getGraphics().drawImage(src, 0, 0, width, height, null);
+		//生成缩略图
+		ImageIO.write(tag, "png", newFile);
+	}
 
 //匹配广告为宽高,用取平方差最小
-public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws IOException{
-	File file =  new File(path);
+public BannerTemplate suitSize(int width,int height,BannerTemplate bannerTemplate) throws IOException{
+
 	//构造image图片
-		Image src = javax.imageio.ImageIO.read(file);
-		int width = src.getWidth(null);
-		int height =src.getHeight(null);
+
 		long max_width = 0;
 		long max_height = 0;
 		Size size = new Size();		
 		int i;
-		int n =0;
+		int n =0; 
 		//查找所有广告位
 		List<Size> sizeList = sizeService.findAllSize();
 		//取平方差
@@ -544,28 +525,38 @@ public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws
 			size = sizeList.get(i); 
 			_width[i] = size.getWidth(); 
 			_height[i] = size.getHeight();
-			if(_width[i]>max_width){
-				max_width = _width[i];
+			if(width<_width[i]&&height<_height[i]){
+				suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
+			}else{
+				if((width*_height[i])==(_width[i]*height)){
+					suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
+				}else{
+				suit[i]=0;
+				}
 			}
-			if(_height[i]>max_height){
-				max_height = _height[i];
+			
 			}
-			suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
-		}
-		if(width>max_width||height>max_height){
-			bannerTemplate = null;
-	}else{
-		double min =suit[0];
-		for (i=0;i<suit.length;i++){
-			if (suit[i]<min){
-				min = suit[i];
-				n=i;
-			}	
-		}
 		
+			//取最小值
+			double min=10000;
+			for (i=0;i<suit.length;i++){
+				if(suit[i]!=0){			
+					if (suit[i]<min){						
+						min = suit[i];
+						n=i;
+					}	
+				}	
+			}
+		if(min==10000){
+				bannerTemplate=null;
+			}else{
+	
 		bannerTemplate.setWidth(_width[n]);
-		bannerTemplate.setHeight(_height[n]);	
-	}
+		bannerTemplate.setHeight(_height[n]);
+		}	
 		return bannerTemplate;
 	}
+
+
+
 }

+ 113 - 149
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/target/AdController.java

@@ -6,6 +6,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 import java.text.SimpleDateFormat;
@@ -429,7 +430,7 @@ public class AdController extends SimpleController {
 	 *文件上传
 	 * */
 	@RequestMapping("/upload")
-	public @ResponseBody String upload(HttpServletRequest request,HttpServletResponse response,Model model,Long id) throws IOException{
+	public @ResponseBody String upload(HttpServletRequest request,HttpServletResponse response,Model model,Long bannerId) throws IOException{
 	
 	HttpSession session = request.getSession();	
 	String uploadName = null;
@@ -437,6 +438,7 @@ public class AdController extends SimpleController {
 	String pathSmall = null;
 	String fileType = null;
 	String result = "null";	
+	BannerTemplate bannerTemplate = bannerService.findById(bannerId).getBannerTemplate();
 	// 创建一个通用的多部分解析器
 	CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
 	// 判断 request 是否有文件上传,即多部分请求
@@ -460,6 +462,8 @@ public class AdController extends SimpleController {
 							result = "~不支持此文件类型";
 							return result;
 						}
+					  
+					  
 					//判断文件名书否存在
 					if(uploadName.trim() !=""){  
 	
@@ -469,27 +473,21 @@ public class AdController extends SimpleController {
 						//获取上传路径
 						path = getLocalFilePath(fileName);
 						
-						try{
+				try{
 						//新建上传路径下文件
 						File localFile = new File(path);  
 						if (!localFile.getParentFile().exists()){
 							localFile.getParentFile().mkdirs();
 						}
-						//if(checkPic(path)){
+												
 						//复制文件到上传文件
 						file.transferTo(localFile);
-						//取上传文件父级目录
-						String absolutePath = localFile.getParentFile().getAbsolutePath();
-						//生成缩略图路径名
-						pathSmall = absolutePath.concat(File.separator).concat("small_").concat(uploadName);
-						//新建缩略图文件
-						smallPicture(localFile,pathSmall, uploadName);
-						
-						result="文件上传成功";
-//							}else{
-//								result="图片太大了哦!";
-//							}
-						}catch(IOException e){
+						if(checkPic(localFile,bannerTemplate)){
+							result = "~图片尺寸错啦!!";
+							return result;
+						}
+												
+					}catch(IOException e){
 							e.printStackTrace();
 							result="~文件上传失败 ";
 							return result;
@@ -514,19 +512,26 @@ public class AdController extends SimpleController {
 	@RequestMapping("/save")
 	public String save(HttpServletRequest request, Model model, @ModelAttribute("form") Banner banner,
 			@RequestParam Map<String, Object> paramMap) throws IOException {
-			
-				
+			//图片宽高
+			int width;
+			int height;
+			//广告位宽高
+			int _width;
+			int _height;
 			//查找创意
 			Banner formerBanner = bannerService.findById(banner.getId());
 			//取模板素材
 			BannerTemplate bannerTemplate = formerBanner.getBannerTemplate();
 			
+			
 			if(paramMap.containsKey("uploadPath")&&paramMap.get("uploadPath")!=""
 					&&paramMap.get("uploadPath")!=null){
-				String uploadName = bannerTemplate.getName();
+				String uploadName = banner.getBannerTemplate().getName();
 				String fileType = uploadName.substring(uploadName.lastIndexOf(".")+1);
 				String filePath = (String)paramMap.get("uploadPath");
-				/**生成文件缩略图*/
+				
+				
+				//取上传文件位置
 				File uploadFile = new File(filePath);
 				if(!uploadFile.getParentFile().exists()){
 					uploadFile.getParentFile().mkdirs();
@@ -535,14 +540,28 @@ public class AdController extends SimpleController {
 				String absolutePath = uploadFile.getParentFile().getAbsolutePath();
 				//生成缩略图路径名
 				String pathSmall = absolutePath.concat(File.separator).concat("small_").concat(uploadName);
-				smallPicture(uploadFile,pathSmall, uploadName);
-				
-				//匹配广告位宽高
-				
-				bannerTemplate = suitSize(filePath, bannerTemplate);
-				//设置文件上传路径
-				bannerTemplate.setPath(replacePath(filePath));
+				//生成缩略图
+				compressPicture(uploadFile,pathSmall, 150,60);
 				
+				//取上传图片的宽高
+				 BufferedImage image = ImageIO.read(new FileInputStream(filePath));
+				 width = image.getWidth();
+				 height = image.getHeight();
+				 _width =Integer.parseInt(bannerTemplate.getWidth().toString()) ;
+				 _height = Integer.parseInt(bannerTemplate.getHeight().toString());
+				 
+				 if(width>_width||height>_height){
+					//设置压缩文件上传路径
+					 String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(uploadName);
+					 //生成压缩图
+					 compressPicture(uploadFile,pathSmall, _width,_height);
+					//设置文件上传路径
+					 bannerTemplate.setPath(replacePath(compressPath));
+				 }else{
+				 
+					 //设置文件上传路径
+					 bannerTemplate.setPath(replacePath(filePath));
+				 }
 				//设置缩略图上传路径
 				bannerTemplate.setName(banner.getBannerTemplate().getName());
 				bannerTemplate.setPathSmall(replacePath(pathSmall));
@@ -553,6 +572,7 @@ public class AdController extends SimpleController {
 				banner.setBannerTemplate(bannerTemplate);
 				banner.setChecked(0);
 				bannerService.updata(banner);
+				
 			}else{
 				bannerService.edit(banner);
 			}
@@ -877,65 +897,12 @@ public class AdController extends SimpleController {
 	
 	
 
-	 //取素材的大小
-	public BannerTemplate setWidthAndHeight(BannerTemplate bannerTemplate,String urls,String fileType){
-	
-					 if("jpeg".equalsIgnoreCase(fileType)||"jpg".equalsIgnoreCase(fileType) ||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType)){
-						 //把文件读取到本地	
-						 File file = new File(urls);
-						 	FileInputStream istream = null;
-							try {
-								istream = new FileInputStream(file);
-							} catch (FileNotFoundException e1) {
-								// TODO Auto-generated catch block
-								e1.printStackTrace();
-							}
-							BufferedImage image=null;
-							try {
-								image = javax.imageio.ImageIO.read(istream);
-							} catch (IOException e) {
-								e.printStackTrace();
-							}
-							if(image!=null){
-								bannerTemplate.setWidth((long)image.getWidth());
-								bannerTemplate.setHeight((long)image.getHeight());
-							}else{
-								System.out.println("aaaa");
-							}
-							try {
-								istream.close();
-							} catch (IOException e) {
-							
-								e.printStackTrace();
-							}
-					 }else if("swf".equalsIgnoreCase(fileType)){      //swf文件类型的设置
-						 File file=new File(urls);
-							SWFHeader swfh = new SWFHeader();
-							try {
-								swfh = SWFHeader.load(file);
-								bannerTemplate.setWidth((long)swfh.getWidth());
-								bannerTemplate.setHeight((long)swfh.getHeight());
-							} catch (IOException e) {
-								// TODO Auto-generated catch block
-								
-								e.printStackTrace();
-							} catch (IllegalAccessException e) {
-								// TODO Auto-generated catch block
-								
-								e.printStackTrace();
-							}
-					 }
-			
-		return bannerTemplate;
-	}
-	
-
 	
 	//处理上传文件的绝对路径
 	public String getLocalFilePath(String fileName){
-						Account loginUser = getLoginUser();
-						// 计算相对路径名,即不包括默认路径名
-						String path = new StringBuilder()
+					Account loginUser = getLoginUser();
+					// 计算相对路径名,即不包括默认路径名
+					String path = new StringBuilder()
 								.append(File.separator)
 								.append(loginUser == null ? "anon" : loginUser.getId())							
 								.append(File.separator)
@@ -947,106 +914,103 @@ public class AdController extends SimpleController {
 								.toString();
 						
 					String localFilePath =config.getResourceUploadDir().concat(File.separator).concat("banner").concat(path);
-						//String localFilePath = "/upload/banner".concat(path);
 						
-						String accessUrl = localFilePath;
-				ServletContext servletContext = getServletContext();
+					String accessUrl = localFilePath;
+					ServletContext servletContext = getServletContext();
 					if (servletContext == null) {
 						LOG.debug("单体测试情况,servletContext不存在。");
-					} else {
+						} else {
 					
 						accessUrl = servletContext.getRealPath("/");
 						File upFile = new File(accessUrl);
 						accessUrl = upFile.getParent();
 						LOG.info("\naaa----"+localFilePath+" -----"+accessUrl);
-				//		获取绝对路径
+						//获取绝对路径
 						localFilePath = accessUrl.concat(File.separator).concat(localFilePath);
 						}
-						return localFilePath;
-					}
+		return localFilePath;
+	}
 						
 	//处理路径为web路径
 	public String replacePath(String path){
 		
-		
-//		String url = config.getResourceUploadDomain()
-//							   .concat(":")
-//							   .concat(config.getResourceUploadPort())
-//							   .concat("/");
 		path = path.substring(path.indexOf("upload")-1);
-//		path = path.replace(top, url);
+
 	return path;
-	}			
+	}
+	
+	//处理文件类型
 	private int judgeType(String fileType){
-					int type = 2;
-					if("jpg".equalsIgnoreCase(fileType)||"jpeg".equalsIgnoreCase(fileType)||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType) ){
-						type = 2;
-					}else if("flv".equalsIgnoreCase(fileType)||"mp4".equalsIgnoreCase(fileType)){
-						type = 3;
-					}else{
-						type = 1;
-					}
-					return type;
-				}
+			int type = 2;
+			if("jpg".equalsIgnoreCase(fileType)||"jpeg".equalsIgnoreCase(fileType)||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType) ){
+				type = 2;
+			}else if("flv".equalsIgnoreCase(fileType)||"mp4".equalsIgnoreCase(fileType)){
+				type = 3;
+			}else{
+				type = 1;
+			}
+			return type;
+	}
 	
 	//生成缩略图
-	public void smallPicture(File localFile,String pathSmall,String uploadName) throws IOException{
+	public void compressPicture(File localFile,String pathNew,int width,int height) throws IOException{
 		//新建缩略图文件
-		File smallFile = new File(pathSmall);
-		if (!smallFile.getParentFile().exists()){
-			smallFile.getParentFile().mkdirs();
+		File newFile = new File(pathNew);
+		if (!newFile.getParentFile().exists()){
+			newFile.getParentFile().mkdirs();
 		}	
 		//构造image图片
 		Image src = javax.imageio.ImageIO.read(localFile);
-		/**得到原图的宽高
-		int width = src.getWidth(null);
-		int height =src.getHeight(null);*/
 		//表示该图像具有打包成整数的像素的8 位RGB颜色分量
-		BufferedImage tag = new BufferedImage(150, 60, BufferedImage.TYPE_INT_BGR);
+		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
 		//h绘制缩小后的图片
-		tag.getGraphics().drawImage(src, 0, 0, 150, 60, null);
+		tag.getGraphics().drawImage(src, 0, 0, width, height, null);
 		//生成缩略图
-		ImageIO.write(tag, "png", smallFile);
+		ImageIO.write(tag, "png", newFile);
 	}
 	
-	//匹配广告为宽高,用取平方差最小
-	public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws IOException{
-		File file =  new File(path);
+
+	
+	//编辑创意判断上传图片是否符合上传条件
+	public boolean checkPic(File localFile,BannerTemplate bannerTemplate) throws IOException{
 		//构造image图片
-			Image src = javax.imageio.ImageIO.read(file);
-			int width = src.getWidth(null);
-			int height =src.getHeight(null);
-			Size size = new Size();	
-			int i;
-			int n =0;
-			//查找所有广告位
-			List<Size> sizeList = sizeService.findAllSize();
-			//取平方差
-			double suit[] = new double[sizeList.size()];
-			long _width[] = new long[sizeList.size()];
-			long _height[] = new long[sizeList.size()];
-			for ( i=0 ;i<sizeList.size();i++){
-				size = sizeList.get(i); 
-				_width[i] = size.getWidth(); 
-				_height[i] = size.getHeight();
-				suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
-			}
-			
-				double min =suit[0];
-				for (i=0;i<suit.length;i++){
-					if (suit[i]<min){
-						min = suit[i];
-						n=i;
-					}	
+		Image src = javax.imageio.ImageIO.read(localFile);	
+		long width = src.getWidth(null);
+		long height = src.getHeight(null);
+		long _width = bannerTemplate.getWidth();
+		long _height = bannerTemplate.getHeight();
+		if(width>0&&height>0){
+			if(width<=_width&&height<=_height){
+				return false;
+			}else{
+				if((width*_height)==(_width*height)){
+					return false;
 				}
+					return true;
+			}	
 		
-				bannerTemplate.setWidth(_width[n]);
-				bannerTemplate.setHeight(_height[n]);				
-				return bannerTemplate;
+		}
+			return true;
 	}
 	
-	
-	
+//	//如果是大于广告位且等比例的需要等比例缩小
+//	public void compressPic(File localFile,String compressPath,) throws IOException{
+//		File compressFile = new File(compressPath);
+//		if (!compressFile.getParentFile().exists()){
+//			compressFile.getParentFile().mkdirs();
+//		}	
+//		//构造image图片
+//		Image src = javax.imageio.ImageIO.read(localFile);
+//		
+//		//表示该图像具有打包成整数的像素的8 位RGB颜色分量
+//		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
+//		//h绘制缩小后的图片
+//		tag.getGraphics().drawImage(src, 0, 0, width, height, null);
+//		//生成压缩图
+//		ImageIO.write(tag, "png", compressFile);
+//		
+//	}
+		
 }
 
 

+ 82 - 64
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/target/CreateBannerController.java

@@ -271,6 +271,11 @@ public class CreateBannerController extends SimpleController{
 		//缩略图路径
 		String pathSmall;
 		String responseStr="null";
+		int width;
+		int height;
+		//广告位宽高
+		int _width;
+		int _height;
 		Long adGroupId = 1l;
 	 
 	if(request.getMethod()=="POST"){ 
@@ -300,13 +305,9 @@ public class CreateBannerController extends SimpleController{
 				 
 				//如果名称不为“”,说明该文件存在,否则说明该文件不存在  
 				if(uploadName.trim() !=""){  
-
-//					//命名上传后的缩略图文件名  
-//					String fileName = "small_".concat(file.getOriginalFilename()); 
 					
 					//处理上传文件路径
 					path = getLocalFilePath(uploadName);
-					//System.out.println("qqqqqqq+==="+path);
 					
 					File localFile = new File(path); 
 					//上传目录不存在,则新建目录
@@ -314,17 +315,40 @@ public class CreateBannerController extends SimpleController{
 						localFile.getParentFile().mkdirs();
 					}	
 				file.transferTo(localFile);
+				
+				//取上传图片的宽高
+				 BufferedImage image = ImageIO.read(new FileInputStream(path));
+				 width = image.getWidth();
+				 height = image.getHeight();
+				//匹配广告位
+				 bannerTemplate=suitSize(width,height, bannerTemplate);
+				//图片匹配
+					if(bannerTemplate==null){
+						return "图片尺寸错啦!!";
+					}
+				 _width =Integer.parseInt(bannerTemplate.getWidth().toString()) ;
+				 _height = Integer.parseInt(bannerTemplate.getHeight().toString());	
+				 
 				//取上传文件父级目录
 				String absolutePath = localFile.getParentFile().getAbsolutePath();
-				//String absolutePath = localFile.getParent();
 				//生成缩略图路径名
 				pathSmall = absolutePath.concat(File.separator).concat("small_").concat(uploadName);
 				//生成缩略图
-				smallPicture(localFile,pathSmall,uploadName);
+				compressPicture(localFile,pathSmall,150,60);
+				//判断是否需要缩放
+				if((width*_height)==(_width*height)){
+					//压缩文件上传路径
+					 String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(uploadName);
+					 //生成压缩图
+					 compressPicture(localFile,pathSmall, _width,_height);
+					//设置文件上传路径
+					 bannerTemplate.setPath(replacePath(compressPath));
+				 }else{
+					 //设置文件上传路径
+					 bannerTemplate.setPath(replacePath(path));
+				 }
 
 				responseStr="文件上传成功";
-				//保存上传文件路径 
-				bannerTemplate.setPath(replacePath(path));
 				//保存缩略图路径
 				bannerTemplate.setPathSmall(replacePath(pathSmall));	
 				adGroupId =Long.parseLong(ids[0]);
@@ -340,11 +364,6 @@ public class CreateBannerController extends SimpleController{
 				model.addAttribute("advertiserId",advertiserId);
 				model.addAttribute("agentId", agentId);
 				
-				//创建素材
-				if(suitSize(path, bannerTemplate)==null){
-					return "图片有点大了!";
-				}
-				bannerTemplate= suitSize(path, bannerTemplate);
 				bannerTemplate.setName(uploadName);
 				bannerTemplate.setType(judgeType(fileType));
 				bannerTemplate.setStatus(0);
@@ -495,14 +514,10 @@ public String getLocalFilePath(String fileName){
 //处理路径为web路径
 public String replacePath(String path){
 	
+
 	
-//	String url = config.getResourceUploadDomain()
-//						   .concat(":")
-//						   .concat(config.getResourceUploadPort())
-//						   .concat("/");
-	System.out.println(path.indexOf("/upload")+1);
 	path = path.substring(path.indexOf("upload")-1);
-//	path = path.replace(top, url);
+
 return path;
 }
 
@@ -519,38 +534,35 @@ public int judgeType(String fileType){
 					return type;
 				}
 
+
+
 //生成缩略图
-public void smallPicture(File localFile,String pathSmall,String uploadName) throws IOException{
-	//新建缩略图文件
-	File smallFile = new File(pathSmall);
-	if (!smallFile.getParentFile().exists()){
-		smallFile.getParentFile().mkdirs();
-	}	
-	//构造image图片
-	Image src = javax.imageio.ImageIO.read(localFile);
-	/**得到原图的宽高
-	int width = src.getWidth(null);
-	int height =src.getHeight(null);*/
-	//表示该图像具有打包成整数的像素的8 位RGB颜色分量
-	BufferedImage tag = new BufferedImage(150, 60, BufferedImage.TYPE_INT_BGR);
-	//h绘制缩小后的图片
-	tag.getGraphics().drawImage(src, 0, 0, 150, 60, null);
-	//生成缩略图
-	ImageIO.write(tag, "png", smallFile);
-}
+	public void compressPicture(File localFile,String pathNew,int width,int height) throws IOException{
+		//新建缩略图文件
+		File newFile = new File(pathNew);
+		if (!newFile.getParentFile().exists()){
+			newFile.getParentFile().mkdirs();
+		}	
+		//构造image图片
+		Image src = javax.imageio.ImageIO.read(localFile);
+		//表示该图像具有打包成整数的像素的8 位RGB颜色分量
+		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
+		//h绘制缩小后的图片
+		tag.getGraphics().drawImage(src, 0, 0, width, height, null);
+		//生成缩略图
+		ImageIO.write(tag, "png", newFile);
+	}
 
 //匹配广告为宽高,用取平方差最小
-public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws IOException{
-	File file =  new File(path);
+public BannerTemplate suitSize(int width,int height,BannerTemplate bannerTemplate) throws IOException{
+
 	//构造image图片
-		Image src = javax.imageio.ImageIO.read(file);
-		int width = src.getWidth(null);
-		int height =src.getHeight(null);
-		Size size = new Size();	
+
 		long max_width = 0;
 		long max_height = 0;
+		Size size = new Size();		
 		int i;
-		int n =0;
+		int n =0; 
 		//查找所有广告位
 		List<Size> sizeList = sizeService.findAllSize();
 		//取平方差
@@ -561,31 +573,37 @@ public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws
 			size = sizeList.get(i); 
 			_width[i] = size.getWidth(); 
 			_height[i] = size.getHeight();
-			if(_width[i]>max_width){
-				max_width = _width[i];
+			if(width<_width[i]&&height<_height[i]){
+				suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
+			}else{
+				if((width*_height[i])==(_width[i]*height)){
+					suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
+				}else{
+				suit[i]=0;
+				}
 			}
-			if(_height[i]>max_height){
-				max_height = _height[i];
+			
 			}
-			suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
-		}
-		if(width>max_width||height>max_height){
-				bannerTemplate = null;
-		}else{
-				double min =suit[0];
-				for (i=0;i<suit.length;i++){
-					if (suit[i]<min){
+		
+			//取最小值
+			double min=10000;
+			for (i=0;i<suit.length;i++){
+				if(suit[i]!=0){			
+					if (suit[i]<min){						
 						min = suit[i];
 						n=i;
 					}	
-				}
-		
-				bannerTemplate.setWidth(_width[n]);
-				bannerTemplate.setHeight(_height[n]);
-				}
-				return bannerTemplate;
-				
-		}
+				}	
+			}
+		if(min==10000){
+				bannerTemplate=null;
+			}else{
+	
+		bannerTemplate.setWidth(_width[n]);
+		bannerTemplate.setHeight(_height[n]);
+		}	
+		return bannerTemplate;
+	}
 }