diff --git a/src/umbraco.editorControls/imagecropper/ImageInfo.cs b/src/umbraco.editorControls/imagecropper/ImageInfo.cs index c85292cc81..054b8f676c 100644 --- a/src/umbraco.editorControls/imagecropper/ImageInfo.cs +++ b/src/umbraco.editorControls/imagecropper/ImageInfo.cs @@ -1,9 +1,11 @@ using System; using System.Drawing; using System.IO; +using System.Net; using System.Web; -using umbraco.editorControls.imagecropper; using Umbraco.Core.IO; +//using Umbraco.Core.IO; + namespace umbraco.editorControls.imagecropper { @@ -19,47 +21,34 @@ namespace umbraco.editorControls.imagecropper public string Path { get; set; } public string RelativePath { get; set; } + private readonly MediaFileSystem _fs; + public ImageInfo(string relativePath) { - RelativePath = relativePath; - Path = IOHelper.MapPath(relativePath); - if (File.Exists(Path)) + _fs = FileSystemProviderManager.Current.GetFileSystemProvider(); + + try { - string fileName = Path.Substring(Path.LastIndexOf('\\') + 1); - Name = fileName.Substring(0, fileName.LastIndexOf('.')); + RelativePath = relativePath; - byte[] buffer = null; - - using (FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read)) - { - buffer = new byte[fs.Length]; - fs.Read(buffer, 0, (int) fs.Length); - fs.Close(); - } - - try - { - image = Image.FromStream(new MemoryStream(buffer)); - - Width = image.Width; - Height = image.Height; - Aspect = (float) Width/Height; - DateStamp = File.GetLastWriteTime(Path); - } - catch (Exception) - { - Width = 0; - Height = 0; - Aspect = 0; - } + //This get's the IFileSystem's path based on the URL (i.e. /media/blah/blah.jpg ) + Path = _fs.GetRelativePath(relativePath); + image = Image.FromStream(_fs.OpenFile(Path)); + Name = _fs.GetFileName(Path); + DateStamp = _fs.GetLastModified(Path).Date; + Width = image.Width; + Height = image.Height; + Aspect = (float)Width / Height; + } - else + catch (Exception) { Width = 0; Height = 0; Aspect = 0; } + } public bool Exists @@ -67,10 +56,10 @@ namespace umbraco.editorControls.imagecropper get { return Width > 0 && Height > 0; } } - public string Directory - { - get { return Path.Substring(0, Path.LastIndexOf('\\')); } - } + //public string Directory + //{ + // get { return Path.Substring(0, Path.LastIndexOf('\\')); } + //} public void GenerateThumbnails(SaveData saveData, Config config) { @@ -78,18 +67,20 @@ namespace umbraco.editorControls.imagecropper { for (int i = 0; i < config.presets.Count; i++) { - Crop crop = (Crop) saveData.data[i]; - Preset preset = (Preset) config.presets[i]; + Crop crop = (Crop)saveData.data[i]; + Preset preset = (Preset)config.presets[i]; // Crop rectangle bigger than actual image - if(crop.X2 - crop.X > Width || crop.Y2 - crop.Y > Height) + if (crop.X2 - crop.X > Width || crop.Y2 - crop.Y > Height) { crop = preset.Fit(this); } + var tmpName = Name.Substring(0, Name.LastIndexOf('.')); + ImageTransform.Execute( Path, - String.Format("{0}_{1}", Name, preset.Name), + String.Format("{0}_{1}", tmpName, preset.Name), crop.X, crop.Y, crop.X2 - crop.X, diff --git a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs index 8e78209a8c..2c24740338 100644 --- a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs +++ b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs @@ -2,35 +2,39 @@ using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing; +using Umbraco.Core.IO; using System.IO; + namespace umbraco.editorControls.imagecropper { [Obsolete("IDataType and all other references to the legacy property editors are no longer used this will be removed from the codebase in future versions")] public class ImageTransform { + private static readonly MediaFileSystem _fs = FileSystemProviderManager.Current.GetFileSystemProvider(); + public static void Execute(string sourceFile, string name, int cropX, int cropY, int cropWidth, int cropHeight, int sizeWidth, int sizeHeight, long quality) { - if (!File.Exists(sourceFile)) return; - string path = sourceFile.Substring(0, sourceFile.LastIndexOf('\\')); + if (!_fs.FileExists(sourceFile)) return; + + + string path = string.Empty; + + //http or local filesystem + if (sourceFile.Contains("/")) + path = sourceFile.Substring(0, sourceFile.LastIndexOf('/')); + else + path = sourceFile.Substring(0, sourceFile.LastIndexOf('\\')); // TODO: Make configurable and move to imageInfo //if(File.Exists(String.Format(@"{0}\{1}.jpg", path, name))) return; - byte[] buffer = null; + //Do we need this check as we are always working with images that are already in a folder?? + //DirectoryInfo di = new DirectoryInfo(path); + //if (!di.Exists) di.Create(); - using (FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)) - { - buffer = new byte[fs.Length]; - fs.Read(buffer, 0, (int)fs.Length); - fs.Close(); - } - - Image image = Image.FromStream(new MemoryStream(buffer)); - - DirectoryInfo di = new DirectoryInfo(path); - if (!di.Exists) di.Create(); + Image image = Image.FromStream(_fs.OpenFile(sourceFile)); using (Image croppedImage = CropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight))) { @@ -58,7 +62,13 @@ namespace umbraco.editorControls.imagecropper EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; - img.Save(path, jpegCodec, encoderParams); + var fileStream = new MemoryStream(); + + img.Save(fileStream, jpegCodec, encoderParams); + fileStream.Position = 0; + _fs.AddFile(path, fileStream, true); + //just to be sure + fileStream.Dispose(); } private static ImageCodecInfo GetEncoderInfo(string mimeType)