Пример #1
0
        /// <summary>
        /// 裁剪图片并保存
        /// </summary>
        public bool cropSaveAs(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
        {
            string fileExt = HotoUtils.GetFileExt(fileName); //文件扩展名,不含“.”

            if (!IsImage(fileExt))
            {
                return(false);
            }
            string newFileDir = HotoUtils.GetMapPath(newFileName.Substring(0, newFileName.LastIndexOf(@"/") + 1));

            //检查是否有该路径,没有则创建
            if (!Directory.Exists(newFileDir))
            {
                Directory.CreateDirectory(newFileDir);
            }
            try
            {
                string fileFullPath   = HotoUtils.GetMapPath(fileName);
                string toFileFullPath = HotoUtils.GetMapPath(newFileName);
                return(HotoThumbnail.MakeThumbnailImage(fileFullPath, toFileFullPath, 180, 180, cropWidth, cropHeight, X, Y));
            }
            catch
            {
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// 文件上传方法C
        /// </summary>
        /// <param name="postedFile">文件流</param>
        /// <param name="isThumbnail">是否生成缩略图</param>
        /// <param name="isWater">是否打水印</param>
        /// <param name="isReOriginal">是否返回文件原名称</param>
        /// <returns>服务器文件路径</returns>
        public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, bool _isImage, bool _isReOriginal)
        {
            try
            {
                string fileExt          = HotoUtils.GetFileExt(postedFile.FileName);                                //文件扩展名,不含“.”
                string originalFileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得文件原名
                string fileName         = HotoUtils.GetRamCode() + "." + fileExt;                                   //随机文件名
                string dirPath          = GetUpLoadPath();                                                          //上传目录相对路径

                //检查文件扩展名是否合法
                if (!CheckFileExt(fileExt))
                {
                    return("{\"msg\": 0, \"msgbox\": \"不允许上传" + fileExt + "类型的文件!\"}");
                }
                //检查是否必须上传图片
                if (_isImage && !IsImage(fileExt))
                {
                    return("{\"msg\": 0, \"msgbox\": \"对不起,仅允许上传图片文件!\"}");
                }
                //检查文件大小是否合法
                if (!CheckFileSize(fileExt, postedFile.ContentLength))
                {
                    return("{\"msg\": 0, \"msgbox\": \"文件超过限制的大小啦!\"}");
                }
                //获得要保存的文件路径
                string serverFileName          = dirPath + fileName;
                string serverThumbnailFileName = dirPath + "small_" + fileName;
                string returnFileName          = serverFileName;
                //物理完整路径
                string toFileFullPath = HotoUtils.GetMapPath(dirPath);
                //检查有该路径是否就创建
                if (!Directory.Exists(toFileFullPath))
                {
                    Directory.CreateDirectory(toFileFullPath);
                }
                //保存文件
                postedFile.SaveAs(toFileFullPath + fileName);
                //如果是图片,检查图片尺寸是否超出限制
                if (IsImage(fileExt) && (this.siteConfig.attachimgmaxheight > 0 || this.siteConfig.attachimgmaxwidth > 0))
                {
                    HotoThumbnail.MakeThumbnailImage(toFileFullPath + fileName, toFileFullPath + fileName, this.siteConfig.attachimgmaxwidth, this.siteConfig.attachimgmaxheight);
                }
                //是否生成缩略图
                if (IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0)
                {
                    HotoThumbnail.MakeThumbnailImage(toFileFullPath + fileName, toFileFullPath + "small_" + fileName, this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, "Cut");
                    returnFileName += "," + serverThumbnailFileName; //返回缩略图,以逗号分隔开
                }
                //是否打图片水印
                if (IsWaterMark(fileExt) && isWater)
                {
                    switch (this.siteConfig.watermarktype)
                    {
                    case 1:
                        HotoWaterMark.AddImageSignText(serverFileName, serverFileName,
                                                       this.siteConfig.watermarktext, this.siteConfig.watermarkposition,
                                                       this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize);
                        break;

                    case 2:
                        HotoWaterMark.AddImageSignPic(serverFileName, serverFileName,
                                                      this.siteConfig.watermarkpic, this.siteConfig.watermarkposition,
                                                      this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency);
                        break;
                    }
                }
                //如果需要返回原文件名
                if (_isReOriginal)
                {
                    return("{\"msg\": 1, \"msgbox\": \"" + serverFileName + "\", \"mstitle\": \"" + originalFileName + "\"}");
                }
                return("{\"msg\": 1, \"msgbox\": \"" + returnFileName + "\"}");
            }
            catch
            {
                return("{\"msg\": 0, \"msgbox\": \"上传过程中发生意外错误!\"}");
            }
        }