Aspose.Image Aspose.Resize JPG image Issues

We use Aspose.Images to resize large high quality images for our website. One issue we are continually having with Aspose, is that when we get jpgs that are 15MB that are greater than 1500px by 1500px we need to resize them. We have this logic in an Azure Function and find that calling Aspose.Resize seems very memory heavy and can sometimes cause the function to timeout. I would love to get your help on how best leverage Aspose here:

For jpg images 15MB to 25MB that we need resize down to a maximum of 1500px wide by 1500px high, is there a formula or a simple way to shrink that image first so that we reduce the overall image size to under 14MB without compromising too much quality? We don’t appear to have this issue pngs.

Next, a separate issue with jpgs, is that sometimes when we get very tall images, Aspose during resizing will flip the image so that it is turned wrong. Is there a parameter for Aspose.Resize to prevent turning an upright image on its size during resizing?

Thanks in advance for your help!

@GPTW_Tech

To address your issues with resizing JPG images using Aspose, here are some suggestions:

1. Reducing Image Size Before Resizing

For large JPG images (15MB to 25MB) that you want to resize to a maximum of 1500px by 1500px, you can first reduce the image quality to decrease the file size without significantly compromising the visual quality. You can achieve this by adjusting the compression level when saving the image. Here’s a code example that demonstrates how to resize an image while controlling the quality:

using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;

// Load the image
using (Image image = Image.Load("path/to/your/image.jpg"))
{
    // Set the desired width and height
    int newWidth = 1500;
    int newHeight = 1500;

    // Resize the image
    image.Resize(newWidth, newHeight, ResizeType.LanczosResample);

    // Save the image with reduced quality to decrease file size
    JpegOptions options = new JpegOptions
    {
        Quality = 80 // Adjust quality (0-100) to reduce file size
    };
    image.Save("path/to/your/resized_image.jpg", options);
}

In this example, the Quality property of JpegOptions can be adjusted to find a balance between image quality and file size. A lower quality value will result in a smaller file size.

2. Preventing Image Flipping During Resizing

If you encounter issues with images being flipped during resizing, you can use the RotateFlip method to ensure the image maintains its correct orientation. Here’s how you can do that:

using Aspose.Imaging;

// Load the image
using (Image image = Image.Load("path/to/your/image.jpg"))
{
    // Check the orientation and apply necessary rotation
    // This is a simple example; you may need to check EXIF data for accurate orientation
    image.RotateFlip(RotateFlipType.RotateNoneFlipNone);

    // Resize the image as needed
    image.Resize(newWidth, newHeight, ResizeType.LanczosResample);

    // Save the resized image
    image.Save("path/to/your/resized_image.jpg");
}

Make sure to check the image’s EXIF data for orientation if you want to handle various orientations accurately.

Conclusion

By adjusting the quality during the save operation and ensuring the correct orientation before resizing, you can effectively manage the memory usage and prevent timeouts in your Azure Function. If you continue to experience performance issues, consider optimizing your Azure Function’s memory settings or processing images in smaller batches.

Feel free to reach out if you have further questions or need additional assistance!

Sources:
[1]: Crop, Rotate and Resize Images - Aspose Documentation

Your suggestion to resize and then save will fail because that is exactly what we are currently doing. in your example when we call image.Resize, this causes the memory to balloon and often will cause our function app to timeout or the logic to freeze when run locally. Do you have an alternate solution where we can adjust the image prior to resizing – essentially getting the image smaller than 14MB before calling img. Resize.