public ActionResult CreateImgByFolderPathAndDpi(string folderPath, string dpi) { int count = 0; var path = Server.MapPath(Request.ApplicationPath + folderPath); DirectoryInfo folder = new DirectoryInfo(path); string fileFullName; dpi = dpi.ToUpper(); int[] dpiWAndH = dpi.Split('X').Select(o => int.Parse(o)).Where(w => w > 0).ToArray(); foreach (FileInfo file in folder.GetFiles()) { fileFullName = file.FullName; if (fileFullName.IndexOf(dpi, StringComparison.Ordinal) == -1) { if (dpiWAndH != null && dpiWAndH.Length == 2) { if (dpi.StartsWith("-") == false) { dpi = string.Concat("-", dpi); } if (System.IO.File.Exists(fileFullName)) { string descPath = fileFullName.Insert(fileFullName.LastIndexOf('.'), dpi); if (System.IO.File.Exists(descPath) == false) { count++; ThumbnailHelper.GenerateImage2(fileFullName, descPath, dpiWAndH[0], dpiWAndH[1]); } } } } } return Content("生成缩略图总计:" + count); }
/// <summary> /// 文件上传到服务器之后执行的操作 /// </summary> /// <returns></returns> private void UploadAfterAction(UploadifyResult uploadifyResult) { //图片dpi if (_dpi.IsNotNullAndNotEmpty()) { List <string> paths = uploadifyResult.uploadPath.Split(_splitChar).ToList(); foreach (var path in paths) { string ext = Path.GetExtension(path).ToUpper(); if (ext.EndsWith("JPG") || ext.EndsWith("JPEG") || ext.EndsWith("PNG")) { int[] whInt = GetDpi(_dpi); //生成的缩略图的路径格式是:原图路径 + “-”+ dpi + 后缀 string desPath = path.Insert(path.LastIndexOf('.'), "-" + _dpi); ThumbnailHelper.GenerateImage2(WebHelper.GetMapPath("~" + path), WebHelper.GetMapPath("~" + desPath), whInt[0], whInt[1]); } } } //视频dpi if (_videoCoverDpi.IsNotNullAndNotWhiteSpace()) { string path = uploadifyResult.uploadPath.Split(_splitChar).First();//正常情况下,视频一次只能传一个 string ext = Path.GetExtension(path).ToUpper(); if (ext.EndsWith("MP4") || ext.EndsWith("MOV")) { GenerateVideoCoverImg(path, uploadifyResult); } } }
/// <summary> /// /// </summary> /// <param name="originalImagePath"></param> /// <param name="imagesDpiDetail"></param> /// <param name="fileType">image/video</param> public Tuple <int, int> MakeThumbnail(string originalImagePath, ImagesDpiDetail imagesDpiDetail, string fileType = "image") { Tuple <int, int> wh = Tuple.Create(0, 0); if (imagesDpiDetail != null) { imagesDpiDetail.SectionItems.ForEach(s => { //生成的缩略图的路径格式是:原图路径 + “-”+ dpi + 后缀 string desPath = originalImagePath.Insert(originalImagePath.LastIndexOf('.'), "-" + (s.dpi ?? imagesDpiDetail.def)); //ThumbnailHelper.GenerateImage(originalImagePath, desPath, s.width, s.height); if (!System.IO.File.Exists(desPath)) { wh = ThumbnailHelper.GenerateImage2(originalImagePath, desPath, s.width, s.height, fileType); } }); } return(wh); }
/// <summary> /// 生成视频封面 /// </summary> /// <param name="path">视频的绝对路径:/Upload/File/zzzzz.mp4</param> /// <param name="uploadifyResult"></param> public void GenerateVideoCoverImg(string path, UploadifyResult uploadifyResult) { #region 生成图片 string videoFullPath = WebHelper.GetMapPath(path); FFmpegHelper ff = new FFmpegHelper(videoFullPath); VideoInfo videoInfo = ff.GetVideoInfo(); string saveImgPath = string.Empty; if (videoInfo.Width < 1) { videoInfo.Width = 500; videoInfo.Height = 330; } if (videoInfo.Width > 0) { uploadifyResult.videoInfo = JsonConvert.SerializeObject(videoInfo); int[] whInt = GetDpi(_videoCoverDpi); string dir = Path.GetDirectoryName(videoFullPath); string videoFileName = Path.GetFileNameWithoutExtension(videoFullPath); int index = videoFileName.LastIndexOf(']'); videoFileName = videoFileName.Substring(index + 1);//去掉括号 int duration = 3; if (uploadifyResult.fileSize < 10485760) { duration = 2; } saveImgPath = string.Concat(dir, "\\", videoFileName, ".jpg"); ff.GetVideoFirstImage(saveImgPath, videoInfo.Width, videoInfo.Height, duration); //生成的缩略图的路径格式是:原图路径 + “-”+ dpi + 后缀 if (System.IO.File.Exists(saveImgPath)) { string desPath = string.Concat(dir, "\\", videoFileName, "-" + _videoCoverDpi, ".jpg");// videoFullPath.Insert(videoFullPath.LastIndexOf('.'), "-" + _videoCoverDpi); ThumbnailHelper.GenerateImage2(saveImgPath, desPath, whInt[0], whInt[1]); } } #endregion }