223 lines
9.9 KiB
C#
223 lines
9.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Web;
|
|
using System.Web.UI;
|
|
using System.Web.UI.WebControls;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
|
|
|
|
public partial class admin_filemanager_upload : System.Web.UI.Page
|
|
{
|
|
//限制寬度尺寸不得超過1024
|
|
const double maxSize = 1024;
|
|
string scc = ConfigurationManager.AppSettings["shopCarCode"].ToString();
|
|
|
|
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
MyWeb.admin admin = new MyWeb.admin();
|
|
if (MyWeb.admin.chkAdmIP && (MyWeb.admin.chkTwIP || MyWeb.admin.chkAdmIP_Enable) && admin.isLoign())
|
|
{
|
|
if (admin.info.login_ip != MyWeb.admin.MyIP)
|
|
{
|
|
Response.Clear();
|
|
Response.StatusCode = 404;
|
|
Response.End();
|
|
}
|
|
|
|
try
|
|
{
|
|
MyWeb.fileSystem fileSystem = new MyWeb.fileSystem();
|
|
HttpPostedFile jpeg_image_upload = Request.Files["Filedata"];
|
|
string up_path = Server.MapPath("~/upload/" + Convert.ToString(Request["dirname"]).Replace("../", ""));
|
|
DirectoryInfo dir = new DirectoryInfo(up_path);
|
|
if (!dir.Exists) { dir.Create(); }
|
|
|
|
string savePath = up_path + (up_path.Substring(up_path.Length - 1, 1) != "/" ? "/" : "") + jpeg_image_upload.FileName.Replace(" ", "_");
|
|
|
|
int re = 0;
|
|
while (true)
|
|
{
|
|
FileInfo fInfo = new FileInfo(savePath);
|
|
if (fInfo.Exists)
|
|
{
|
|
string fname = jpeg_image_upload.FileName.Replace(" ", "_");
|
|
re++;
|
|
string[] f = fname.Split('.');
|
|
fname = f[0] + "(" + re.ToString() + ")" + (f.Length > 1 ? "." + f[1] : "");
|
|
savePath = up_path + (up_path.Substring(up_path.Length - 1, 1) != "/" ? "/" : "") + fname;
|
|
}
|
|
else { break; }
|
|
}
|
|
|
|
if (fileSystem.isPhoto(jpeg_image_upload.FileName))
|
|
{
|
|
string[] n = jpeg_image_upload.FileName.Split('.');
|
|
System.Drawing.Image Bm = new System.Drawing.Bitmap(jpeg_image_upload.InputStream);
|
|
|
|
foreach (PropertyItem pi in Bm.PropertyItems)
|
|
{
|
|
// orientation tag id is 274
|
|
if (pi.Id == 274)
|
|
{
|
|
switch (pi.Value[0])
|
|
{
|
|
case 2:
|
|
Bm.RotateFlip(RotateFlipType.RotateNoneFlipX);
|
|
break;
|
|
case 3:
|
|
Bm.RotateFlip(RotateFlipType.Rotate180FlipNone);
|
|
break;
|
|
case 4:
|
|
Bm.RotateFlip(RotateFlipType.RotateNoneFlipY);
|
|
break;
|
|
case 5:
|
|
Bm.RotateFlip(RotateFlipType.Rotate90FlipX);
|
|
break;
|
|
case 6:
|
|
Bm.RotateFlip(RotateFlipType.Rotate90FlipNone);
|
|
break;
|
|
case 7:
|
|
Bm.RotateFlip(RotateFlipType.Rotate270FlipX);
|
|
break;
|
|
case 8:
|
|
Bm.RotateFlip(RotateFlipType.Rotate270FlipNone);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
double newWidth = Bm.Width;
|
|
double newHeight = Bm.Height;
|
|
if (Bm.Width > maxSize)
|
|
{
|
|
newWidth = maxSize;
|
|
newHeight = maxSize / Bm.Width;
|
|
newHeight = newHeight * Bm.Height;
|
|
}
|
|
/*if (Bm.Width > Bm.Height)
|
|
{
|
|
if (Bm.Width > maxSize)
|
|
{
|
|
newWidth = maxSize;
|
|
newHeight = maxSize / Bm.Width;
|
|
newHeight = newHeight * Bm.Height;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (Bm.Height > maxSize)
|
|
{
|
|
newHeight = maxSize;
|
|
newWidth = maxSize / Bm.Height;
|
|
newWidth = newWidth * Bm.Width;
|
|
}
|
|
}*/
|
|
|
|
if (newWidth > 0 & newHeight > 0)
|
|
{
|
|
string picType = n[n.Length - 1].ToLower();
|
|
if (picType == "jpg") { picType = "jpeg"; }
|
|
System.Drawing.Image New_Image = new Bitmap((int)newWidth, (int)newHeight);
|
|
Graphics ObjGraphics = Graphics.FromImage(New_Image);
|
|
|
|
MyWeb.global global = new MyWeb.global();
|
|
|
|
ObjGraphics.InterpolationMode = global.InterpolationMode;
|
|
ObjGraphics.SmoothingMode = global.SmoothingMode;
|
|
ObjGraphics.CompositingQuality = global.CompositingQuality;
|
|
|
|
if (picType == "png")
|
|
{
|
|
ObjGraphics.Clear(Color.Transparent); //清空Graphics, 以透明色填充
|
|
}
|
|
else
|
|
{
|
|
ObjGraphics.Clear(Color.White); //清空Graphics, 以白色填充
|
|
}
|
|
|
|
//在指定位置按指定大小繪制原圖片的片段
|
|
ObjGraphics.DrawImage(Bm, new Rectangle(0, 0, (int)newWidth, (int)newHeight), new Rectangle(0, 0, Bm.Width, Bm.Height), GraphicsUnit.Pixel);
|
|
|
|
|
|
if (fileSystem.WatermarkMode == "2")
|
|
{
|
|
string FileNameAndPath = HttpContext.Current.Server.MapPath(fileSystem.WatermarkPath);
|
|
System.Drawing.Image Watermark_Image = System.Drawing.Image.FromFile(FileNameAndPath);
|
|
int w_Width = Convert.ToInt32(Math.Round(Convert.ToDouble(newWidth) * 0.8));
|
|
int w_Height = Convert.ToInt32(Watermark_Image.Height * (Convert.ToDouble(w_Width) / Convert.ToDouble(Watermark_Image.Width)));
|
|
int w_myX = ((int)newWidth - w_Width) / 2; //浮水印水平置中
|
|
int w_myY = ((int)newHeight - w_Height) / 2 + w_Height; //浮水印水平垂直置中向下偏移一個高度
|
|
var colorMatrix = new ColorMatrix();
|
|
colorMatrix.Matrix33 = (float)Convert.ToSingle(fileSystem.WatermarkPct);
|
|
var imageAttributes = new ImageAttributes();
|
|
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
|
|
ObjGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
|
ObjGraphics.DrawImage(
|
|
Watermark_Image,
|
|
new Rectangle(w_myX, w_myY, w_Width, w_Height),
|
|
0,
|
|
0,
|
|
Watermark_Image.Width,
|
|
Watermark_Image.Height,
|
|
GraphicsUnit.Pixel,
|
|
imageAttributes);
|
|
}
|
|
|
|
//下方設定使JPG質量
|
|
EncoderParameters EPS = new EncoderParameters();
|
|
EncoderParameter EP = new EncoderParameter(Encoder.Quality, global.ImageCompressionQuality);
|
|
EPS.Param[0] = EP;
|
|
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
|
|
ImageCodecInfo ICI = null;
|
|
foreach (ImageCodecInfo codec in codecs)
|
|
{
|
|
if (codec.MimeType == "image/" + picType)
|
|
{
|
|
ICI = codec;
|
|
break;
|
|
}
|
|
}
|
|
New_Image.Save(savePath, ICI, EPS);
|
|
New_Image.Dispose();
|
|
ObjGraphics.Dispose();
|
|
|
|
}
|
|
else
|
|
{
|
|
jpeg_image_upload.SaveAs(savePath);
|
|
}
|
|
Bm.Dispose();
|
|
}
|
|
else if (fileSystem.isAllowed(jpeg_image_upload.FileName))
|
|
{
|
|
jpeg_image_upload.SaveAs(savePath);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// If any kind of error occurs return a 500 Internal Server error
|
|
Response.StatusCode = 500;
|
|
Response.Write("An error occured");
|
|
Response.End();
|
|
}
|
|
finally
|
|
{
|
|
Response.End();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Response.Clear();
|
|
Response.StatusCode = 404;
|
|
Response.End();
|
|
}
|
|
|
|
}
|
|
} |