Recently, a bug was reported in nopCommerce 3.70 version that is related to image resizing. Today, we will go over the process / step to fix this bug in 3.70 version.
Bug / Issue:
Go to: ../Libraries/Nop.Services/Media/PictureService.cs
Open "PictureService.cs" file and look for this:
/// <
summary
>
/// Calculates picture dimensions whilst maintaining aspect
/// </
summary
>
/// <
param
name
=
"originalSize"
>The original picture size</
param
>
/// <
param
name
=
"targetSize"
>The target picture size (longest side)</
param
>
/// <
param
name
=
"resizeType"
>Resize type</
param
>
/// <
param
name
=
"ensureSizePositive"
>A value indicatingh whether we should ensure that size values are positive</
param
>
/// <
returns
></
returns
>
protected virtual Size CalculateDimensions(Size originalSize, int targetSize,
ResizeType resizeType = ResizeType.LongestSide, bool ensureSizePositive = true)
This is the old code (with bug):
case ResizeType.LongestSide:
if (originalSize.Height > originalSize.Width)
{
// portrait
newSize.Width = (int)(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
newSize.Height = targetSize;
}
else
{
// landscape or square
newSize.Height = (int)(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
newSize.Width = targetSize;
}
break;
case ResizeType.Width:
newSize.Height = (int)(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
newSize.Width = targetSize;
break;
case ResizeType.Height:
newSize.Width = (int)(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
newSize.Height = targetSize;
break;
Fix: New code (note addition of 4 instances of Math.Round)
case ResizeType.LongestSide:
if (originalSize.Height > originalSize.Width)
{
// portrait
newSize.Width = (int)Math.Round(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
newSize.Height = targetSize;
}
else
{
// landscape or square
newSize.Height = (int)Math.Round(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
newSize.Width = targetSize;
}
break;
case ResizeType.Width:
newSize.Height = (int)Math.Round(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
newSize.Width = targetSize;
break;
case ResizeType.Height:
newSize.Width = (int)Math.Round(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
newSize.Height = targetSize;
break;
The method should look like this after applying this fix:
protected virtual Size CalculateDimensions(Size originalSize, int targetSize,
ResizeType resizeType = ResizeType.LongestSide, bool ensureSizePositive = true)
{
var newSize = new Size();
switch (resizeType)
{
case ResizeType.LongestSide:
if (originalSize.Height > originalSize.Width)
{
// portrait
newSize.Width = (int)Math.Round(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
newSize.Height = targetSize;
}
else
{
// landscape or square
newSize.Height = (int)Math.Round(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
newSize.Width = targetSize;
}
break;
case ResizeType.Width:
newSize.Height = (int)Math.Round(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
newSize.Width = targetSize;
break;
case ResizeType.Height:
newSize.Width = (int)Math.Round(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
newSize.Height = targetSize;
break;
default:
throw new Exception("Not supported ResizeType");
}
if (ensureSizePositive)
{
if (newSize.Width < 1)
newSize.Width = 1;
if (newSize.Height < 1)
newSize.Height = 1;
}
return newSize;
}
GitHub work item: https://github.com/nopSolutions/nopCommerce/issues/1031
A copy of this article is also available on Arvixe Blog.