diff --git a/docs/en/Image-Manipulation.md b/docs/en/Image-Manipulation.md index 1050b6005e..9d946e31dd 100644 --- a/docs/en/Image-Manipulation.md +++ b/docs/en/Image-Manipulation.md @@ -76,8 +76,8 @@ public interface IImageResizer **Example usage:** ```csharp -var result = await _imageResizer.ResizeAsync( - stream, /* A stream object that represents an image */ +var resizeResult = await _imageResizer.ResizeAsync( + imageStream, /* A stream object that represents an image */ new ImageResizeArgs { Width = 100, @@ -88,6 +88,16 @@ var result = await _imageResizer.ResizeAsync( ); ``` +> **Note:** If `resizeResult.State` returns 'Done', then it means that the resize operation was successful. However, if it returns any other state than 'Done', the stream you're using might be corrupted. Therefore, you can perform a check like the one below and assign the correct stream to the main stream: + +```csharp +if (resizeResult.Result is not null && imageStream != resizeResult.Result && resizeResult.Result.CanRead) +{ + await imageStream.DisposeAsync(); + imageStream = resizeResult.Result; +} +``` + > You can use `MimeTypes.Image.Jpeg` constant instead of the `image/jpeg` magic string used in that example. ### ImageResizeArgs @@ -170,12 +180,23 @@ public interface IImageCompressor **Example usage:** ```csharp -var result = await _imageCompressor.CompressAsync( - stream, /* A stream object that represents an image */ +var compressResult = await _imageCompressor.CompressAsync( + imageStream, /* A stream object that represents an image */ mimeType: "image/jpeg" ); ``` +> **Note:** If `compressResult.State` returns 'Done', then it means that the compression operation was successful. However, if it returns any other state than 'Done', the stream you're using might be corrupted. Therefore, you can perform a check like the one below and assign the correct stream to the main stream: + +```csharp + +if (compressResult.Result is not null && imageStream != compressResult.Result && compressResult.Result.CanRead) +{ + await imageStream.DisposeAsync(); + imageStream = compressResult.Result; +} +``` + ### ImageCompressResult The `ImageCompressResult` is a generic class that is used to return the result of the image compression operations. It has the following properties: diff --git a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs index 11789776c7..e32a7d104e 100644 --- a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs +++ b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs @@ -28,10 +28,25 @@ public class ImageCompressor : IImageCompressor, ITransientDependency { Check.NotNull(stream, nameof(stream)); + if(!stream.CanRead) + { + return new ImageCompressResult(stream, ImageProcessState.Unsupported); + } + + if(!stream.CanSeek) + { + var memoryStream = new MemoryStream(); + await stream.CopyToAsync(memoryStream, CancellationTokenProvider.FallbackToProvider(cancellationToken)); + SeekToBegin(memoryStream); + stream = memoryStream; + } + foreach (var imageCompressorContributor in ImageCompressorContributors) { var result = await imageCompressorContributor.TryCompressAsync(stream, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); + SeekToBegin(stream); + if (result.State == ImageProcessState.Unsupported) { continue; @@ -64,4 +79,12 @@ public class ImageCompressor : IImageCompressor, ITransientDependency return new ImageCompressResult(bytes, ImageProcessState.Unsupported); } + + protected virtual void SeekToBegin(Stream stream) + { + if (stream.CanSeek) + { + stream.Seek(0, SeekOrigin.Begin); + } + } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs index 139fc194dd..4afc879089 100644 --- a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs +++ b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs @@ -38,9 +38,24 @@ public class ImageResizer : IImageResizer, ITransientDependency ChangeDefaultResizeMode(resizeArgs); + if(!stream.CanRead) + { + return new ImageResizeResult(stream, ImageProcessState.Unsupported); + } + + if(!stream.CanSeek) + { + var memoryStream = new MemoryStream(); + await stream.CopyToAsync(memoryStream, CancellationTokenProvider.FallbackToProvider(cancellationToken)); + SeekToBegin(memoryStream); + stream = memoryStream; + } + foreach (var imageResizerContributor in ImageResizerContributors) { var result = await imageResizerContributor.TryResizeAsync(stream, resizeArgs, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); + + SeekToBegin(stream); if (result.State == ImageProcessState.Unsupported) { @@ -85,4 +100,12 @@ public class ImageResizer : IImageResizer, ITransientDependency resizeArgs.Mode = ImageResizeOptions.DefaultResizeMode; } } + + protected virtual void SeekToBegin(Stream stream) + { + if (stream.CanSeek) + { + stream.Seek(0, SeekOrigin.Begin); + } + } } \ No newline at end of file