Ok,
Here we go. I hope i'm not missing any parts of it:
1. Products_Edit.aspx.vb, function Save Add the following line
' Corneliu - Build a Small Image if only a medium image exists
ProductUtils.BuildProductImages(p, Request.PhysicalApplicationPath)
Just before:
If String.IsNullOrEmpty(Me.SmallImageAlternateTextField.Text) Then
p.ImageFileSmallAlternateText = p.ProductName & " " & p.Sku
Else
p.ImageFileSmallAlternateText = Me.SmallImageAlternateTextField.Text
End If
2. Add this to your App_Code\ProductUtils.vb
Public Shared Function BuildProductImages(ByVal product As Catalog.Product, ByVal physicalRoot As String) As Boolean
If String.IsNullOrEmpty(product.ImageFileMedium) Then
Return False
End If
If String.IsNullOrEmpty(product.ImageFileSmall) Then
Dim newSmallImage As String = ResizeImage(product.ImageFileMedium, "_sm", physicalRoot, WebAppSettings.ImagesSmallWidth, WebAppSettings.ImagesSmallHeight)
If newSmallImage IsNot Nothing Then
product.ImageFileSmall = newSmallImage
End If
End If
Dim medImageFileName As String = GetFullPhysicalFileName(physicalRoot, product.ImageFileMedium)
' check if the medium image is "too large"
Dim medImageInfo As ImageInfo = ImageHelper.GetImageInformation(medImageFileName)
If medImageInfo.Height > WebAppSettings.ImagesMediumHeight Or medImageInfo.Width > WebAppSettings.ImagesMediumWidth Then
' resize this image
Dim oldMedImage As String = product.ImageFileMedium
Dim newMedImage As String = ResizeImage(oldMedImage, "_med", physicalRoot, WebAppSettings.ImagesMediumWidth, WebAppSettings.ImagesMediumHeight)
If newMedImage IsNot Nothing Then
product.ImageFileMedium = newMedImage
Dim shouldAdd As Boolean = True
If product.AdditionalImages IsNot Nothing Then
For Each image As Catalog.ProductImage In product.AdditionalImages
If image.FileName = oldMedImage Then
shouldAdd = False
Exit For
End If
Next
End If
If shouldAdd Then
Dim newImage As New Catalog.ProductImage()
newImage.FileName = oldMedImage
newImage.ProductId = product.Bvin
newImage.Bvin = String.Empty
Catalog.ProductImage.Insert(newImage)
End If
End If
End If
End Function
Public Shared Function GetFullPhysicalFileName(ByVal physicalRoot As String, ByVal file As String) As String
Return Path.Combine(physicalRoot, file)
End Function
Public Shared Function ResizeImage(ByVal oldFile As String, ByVal newSufix As String, ByVal physicalRoot As String, ByVal newWidth As Integer, ByVal newHeight As Integer) As String
If String.IsNullOrEmpty(oldFile) Then
Return Nothing
End If
Dim newImageName As String = GetNewFullFileName(oldFile, newSufix)
If newImageName = oldFile Then
Return Nothing
End If
Dim ext As String = Path.GetExtension(newImageName)
If ext.ToLower() <> ".jpg" Then
newImageName = newImageName.Substring(0, newImageName.Length - ext.Length) + ".jpg"
End If
Dim newImageFileName As String = GetFullPhysicalFileName(physicalRoot, newImageName)
Dim oldImageFileName As String = GetFullPhysicalFileName(physicalRoot, oldFile)
If File.Exists(newImageFileName) Then
Return newImageName
Else
Try
ResizeImage(oldImageFileName, newImageFileName, newWidth, newHeight)
Return newImageName
Catch ex As Exception
EventLog.LogEvent(ex)
Return Nothing
End Try
End If
Return Nothing
End Function
Public Shared Function GetNewFullFileName(ByVal oldFile As String, ByVal newSufix As String) As String
Dim imageName As String = Path.GetFileNameWithoutExtension(oldFile)
If imageName.EndsWith(newSufix) Then
Return imageName
End If
Dim newImageName As String
newImageName = imageName + newSufix + Path.GetExtension(oldFile)
newImageName = Path.Combine(Path.GetDirectoryName(oldFile), newImageName)
Return newImageName
End Function
Public Shared Function ResizeImage(ByVal oldFile As String, ByVal newFile As String, ByVal maxWidth As Integer, ByVal maxHeight As Integer) As Boolean
Dim g As Graphics = Nothing
Try
Dim originalImageInfo As ImageInfo = ImageHelper.GetImageInformation(oldFile)
Dim originalImg As Image = Image.FromFile(oldFile)
Dim originalImageSize As Size = New Size(originalImageInfo.Width, originalImageInfo.Height)
Dim newImageInfo As ImageInfo
newImageInfo = ImageHelper.GetProportionalImageDimensionsForImage(originalImageInfo, maxWidth, maxHeight)
Dim newImageSize As Size = New Size(newImageInfo.Width, newImageInfo.Height)
Dim img As Image = New Bitmap(newImageInfo.Width, newImageInfo.Height)
g = Graphics.FromImage(img)
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(originalImg, New Rectangle(New Point(0, 0), newImageSize), New Rectangle(New Point(0, 0), originalImageSize), GraphicsUnit.Pixel)
g.Dispose()
Dim ext As String = Path.GetExtension(newFile)
If ext.ToLower() <> ".jpg" Then
newFile = newFile.Substring(0, newFile.Length - ext.Length) + ".jpg"
End If
Dim qualityParam As EncoderParameter = New EncoderParameter(Encoder.Quality, 90)
' Jpeg image codec
Dim jpegCodec As ImageCodecInfo = GetEncoderInfo("image/jpeg")
Dim encoderParams As EncoderParameters = New EncoderParameters(1)
encoderParams.Param().SetValue(qualityParam, 0)
img.Save(newFile, jpegCodec, encoderParams)
Return True
Catch ex As Exception
EventLog.LogEvent(ex)
If g IsNot Nothing Then
g.Dispose()
End If
End Try
Return False
End Function
Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
For Each codec As ImageCodecInfo In codecs
If codec.MimeType = mimeType Then
Return codec
End If
Next
Return Nothing
End Function
Fix the imports at the top of the file:
Imports System.IO
Imports System.Web
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports Microsoft.VisualBasic
Imports BVSoftware.Bvc5.Core
Imports BVSoftware.Bvc5.Core.Utilities
Now if all is good you should be able to assign a single image to your product in the Medium image. Then click update. The code will automatically create a small and a medium image and assign them and use the original image as an aditional image.
Regards,
Corneliu.