We are using Aspose.Imaging to resize customer uploaded jpg’s and png’s down to around 1k by 1k. We’ve noticed that when resizing large images (not large in terms of file size for example this occurs with a 2k png) but large in terms of width and height (9k by 11k) that our Azure App Service crashes with a catastrophic error and restarts. We get no error information because the entire app service restarts. When we run the app service locally in azure emulation mode the image resizing works correctly. It’s only when running the app service in Azure. We are using a Azure S1 (A Series compute equivalent) with 1.75 GB memory. Any help you give would be greatly appreciated.
Here’s the resizing code. DotNetFramework 4.7.2 (VB.Net)
When I attempt to upload the 2k image to this forum it tells me the image I’m attempting to upload is over 80 megapixels. Its dimensions are 9k by 11k and as I mentioned the file size on my drive is under 2k.
Here’s a link to the image.
‘’’
‘’’ Proportionally resize the specified image file using the max width and height given, and
‘’’ if the original image is > 1MB, save as JPG at 90% quality
‘’’
‘’’ Filepath of the original image to be resized
‘’’ Folder to hold the finished resized image
‘’’ Maximum desired width, in pixels
‘’’ Maximum desired height, in pixels
‘’’ Image resizing algorithm to use (see Aspose documentation)
‘’’ To scale only the width, set maxHeight to 0
‘’’ to scale only the height set maxWidth to 0
Public Function ResizeImage(originalImageFilePath As String, imagesOutputFolder As String, maxWidth As Integer, maxHeight As Integer, Optional algorithm As ResizeType = ResizeType.HighQualityResample) As ResizeImageResults
Dim results = New ResizeImageResults()
Dim resizedFileName As String = ""
Try
'Instantiate license.
Dim AsposeLic As New License
AsposeLic.SetLicense("Aspose.Total.lic")
'Get physical file size of original image
Dim fi = New IO.FileInfo(originalImageFilePath)
Dim sourceFileSize = fi.Length
Dim sourceFileName As String = Path.GetFileName(originalImageFilePath)
Dim sourceFileNameWithoutExt As String = Path.GetFileNameWithoutExtension(originalImageFilePath)
'Load the original image.
Using img As Image = Aspose.Imaging.Image.Load(originalImageFilePath)
Dim newHeight As Integer = img.Height
Dim newWidth As Integer = img.Width
Dim isResized As Boolean = False
'Get the new, resized dimensions
GetResizedDimensions(img.Width, img.Height, maxWidth, maxHeight, newWidth, newHeight)
'If the new dimensions are not the same, resize the image to the new dimensions
If newHeight <> img.Height Or newWidth <> img.Width Then
AtlasWebUILog.LogInformation(String.Format("/ProduceReview/Resizing image '{0}' from {1}h x {2}w to {3}h x {4}w", originalImageFilePath, img.Height, img.Width, newHeight, newWidth))
img.Resize(newWidth, newHeight, algorithm)
isResized = True
End If
If sourceFileSize < 1048576 Then
'The source file is < 1MB, no need to downgrade the quality
If isResized = False Then
'No change was made to the original file dimensions
'Just copy the file to the output images folder
File.Copy(originalImageFilePath, imagesOutputFolder & "\" & sourceFileName)
results.IsChanged = False
results.ResizedFileName = sourceFileName
Else
'The file dimensions were resized
resizedFileName = sourceFileName
img.Save(imagesOutputFolder & "\" & resizedFileName, True)
results.IsChanged = True
results.ResizedFileName = resizedFileName
End If
Else
'File > 1MB, save as JPG and downgrade to 90% quality
'Save as JPG and set to 90% quality
Dim jpgOptions = New ImageOptions.JpegOptions()
jpgOptions.Quality = 90
'Change the file extension to .jpg
resizedFileName = sourceFileNameWithoutExt & ".jpg"
img.Save(imagesOutputFolder & "\" & resizedFileName, jpgOptions)
results.IsChanged = True
results.ResizedFileName = resizedFileName
End If
If results.IsChanged Then
AtlasWebUILog.LogInformation(String.Format("/ProduceReview/Image '{0}' resized and saved as '{1}'", originalImageFilePath, imagesOutputFolder & "\" & resizedFileName))
End If
End Using
Catch ex As Exception
AtlasWebUILog.LogErrorWithException(String.Format("/ProduceReview error when resizing the image '{0}':", originalImageFilePath), ex)
results.ResizedFileName = ""
results.IsError = True
results.ErrorMessage = ex.Message
End Try
Return results
End Function