OrchardCMS / Orchard

Orchard is a free, open source, community-focused Content Management System built on the ASP.NET MVC platform.
https://orchardproject.net
BSD 3-Clause "New" or "Revised" License
2.37k stars 1.12k forks source link

ResizeMediaUrl fails for vector images #8745

Closed AndreaPiovanelli closed 7 months ago

AndreaPiovanelli commented 8 months ago

https://github.com/OrchardCMS/Orchard/blob/9644ceda1f9b077d44aaa4ffaab103c19a59ddba/src/Orchard.Web/Modules/Orchard.MediaProcessing/Shapes/MediaShapes.cs#L56C31-L56C31

Display.ResizeMediaUrl calls throw exceptions everytime the function is called for a media which isn't a proper image (it fails for vector images, for instance). The exception is the following:

Orchard.MediaProcessing.Shapes.MediaShapes - tenant - An error occurred while rendering shape Transform_Resize_w_180_h_0_m_pad_a_middlecenter_c__s_ for image /Media/tenant/mediafile.svg [https://tenant.url/page]
ImageResizer.ImageCorruptedException (0x80004005): File may be corrupted, empty, or may contain a PNG image with a single dimension greater than 65,535 pixels. ---> System.ArgumentException: Invalid parameter.
   in System.Drawing.Bitmap..ctor(Stream stream, Boolean useIcm)
   in ImageResizer.ImageBuilder.DecodeStream(Stream s, ResizeSettings settings, String optionalPath)
   in ImageResizer.ImageBuilder.LoadImage(Object source, ResizeSettings settings, Boolean restoreStreamPos)
   in ImageResizer.ImageBuilder.LoadImage(Object source, ResizeSettings settings, Boolean restoreStreamPos)
   in ImageResizer.ImageBuilder.BuildJob(ImageJob job)
   in ImageResizer.ImageBuilder.BuildInternal(ImageJob job)
   in ImageResizer.ImageBuilder.BuildInQueue(ImageJob job, Boolean useSemaphore, Int32 maxQueuingMilliseconds, CancellationToken cancel)
   in ImageResizer.ImageBuilder.Build(ImageJob job)
   in ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings, Boolean disposeSource, Boolean addFileExtension)
   in ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings, Boolean disposeSource)
   in Orchard.MediaProcessing.Providers.Filters.ResizeFilter.ApplyFilter(FilterContext context)
   in Orchard.MediaProcessing.Services.ImageProfileManager.GetImageProfileUrl(String path, String profileName, ContentItem contentItem, FilterRecord[] customFilters)
   in Orchard.MediaProcessing.Shapes.MediaShapes.MediaUrl(Object Shape, Object Display, TextWriter Output, String Profile, String Path, ContentItem ContentItem, FilterRecord CustomFilter)

We already implemented a wrapper solving the issue, which you can find here: https://github.com/LaserSrl/Laser.Orchard.Platform/blob/aaab8efef30ffd5ee4277cfb84741411bd812449/src/Modules/Laser.Orchard.StartupConfig/Services/ThemeHelperService.cs#L116

Shall we move that code directly to Orchard.MediaProcessing module? Any suggestion for other possible solutions to this issue?

sebastienros commented 8 months ago

How is the wrapper you point to solving the issue?

AndreaPiovanelli commented 8 months ago

How is the wrapper you point to solving the issue?

It checks for the presence of ImagePart (line 118) and, if found, resizes the media with current operations. If there is not ImagePart, the media doesn't get resized and original unprocessed media url is displayed (lines 121 and 122).

MatteoPiovanelli-Laser commented 8 months ago

As a note: That check for ImagePart, if moved to MediaProcessing, would likely be in the ImageProfileManager, rather than in the shape. Anyway, even if it were in the ResizeMediaUrl shape, it would still provide cleaner fix than having to check for it in every shape where that is used (or, as we did, using a wrapper around ResizeMediaUrl)