I am using nopcommerce 3.30. I want my other external sites to use the product images from my nopcommerce site, what is the rule for the image URL?
Thanks
James
public virtual string GetPictureUrl(Picture picture,
int targetSize = 0,
bool showDefaultPicture = true,
string storeLocation = null,
PictureType defaultPictureType = PictureType.Entity)
{
string url = string.Empty;
byte[] pictureBinary = null;
if (picture != null)
pictureBinary = LoadPictureBinary(picture);
if (picture == null || pictureBinary == null || pictureBinary.Length == 0)
{
if(showDefaultPicture)
{
url = GetDefaultPictureUrl(targetSize, defaultPictureType, storeLocation);
}
return url;
}
string lastPart = GetFileExtensionFromMimeType(picture.MimeType);
string thumbFileName;
if (picture.IsNew)
{
DeletePictureThumbs(picture);
//we do not validate picture binary here to ensure that no exception ("Parameter is not valid") will be thrown
picture = UpdatePicture(picture.Id,
pictureBinary,
picture.MimeType,
picture.SeoFilename,
false,
false);
}
lock (s_lock)
{
string seoFileName = picture.SeoFilename; // = GetPictureSeName(picture.SeoFilename); //just for sure
if (targetSize == 0)
{
thumbFileName = !String.IsNullOrEmpty(seoFileName) ?
string.Format("{0}_{1}.{2}", picture.Id.ToString("0000000"), seoFileName, lastPart) :
string.Format("{0}.{1}", picture.Id.ToString("0000000"), lastPart);
var thumbFilePath = GetThumbLocalPath(thumbFileName);
if (!File.Exists(thumbFilePath))
{
File.WriteAllBytes(thumbFilePath, pictureBinary);
}
}
else
{
thumbFileName = !String.IsNullOrEmpty(seoFileName) ?
string.Format("{0}_{1}_{2}.{3}", picture.Id.ToString("0000000"), seoFileName, targetSize, lastPart) :
string.Format("{0}_{1}.{2}", picture.Id.ToString("0000000"), targetSize, lastPart);
var thumbFilePath = GetThumbLocalPath(thumbFileName);
if (!File.Exists(thumbFilePath))
{
using (var stream = new MemoryStream(pictureBinary))
{
Bitmap b = null;
try
{
//try-catch to ensure that picture binary is really OK. Otherwise, we can get "Parameter is not valid" exception if binary is corrupted for some reasons
b = new Bitmap(stream);
}
catch (ArgumentException exc)
{
_logger.Error(string.Format("Error generating picture thumb. ID={0}", picture.Id), exc);
}
if (b == null)
{
//bitmap could not be loaded for some reasons
return url;
}
var newSize = CalculateDimensions(b.Size, targetSize);
if (newSize.Width < 1)
newSize.Width = 1;
if (newSize.Height < 1)
newSize.Height = 1;
using (var newBitMap = new Bitmap(newSize.Width, newSize.Height))
{
using (var g = Graphics.FromImage(newBitMap))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(b, 0, 0, newSize.Width, newSize.Height);
var ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(Encoder.Quality, _mediaSettings.DefaultImageQuality);
ImageCodecInfo ici = GetImageCodecInfoFromExtension(lastPart);
if (ici == null)
ici = GetImageCodecInfoFromMimeType("image/jpeg");
newBitMap.Save(thumbFilePath, ici, ep);
}
}
b.Dispose();
}
}
}
}
url = GetThumbUrl(thumbFileName, storeLocation);
return url;
}
var picture = _pictureService.GetPicturesByProductId(productId).FirstOrDefault();
if (picture == null)
return "";
var seName = picture.SeoFilename;
var pictureId = picture.Id;
var mimeType = picture.MimeType;
var sb = new StringBuilder();
sb.Append(_webHelper.GetStoreLocation());
sb.Append("content/images/thumbs/");
sb.Append(pictureId.ToString().PadLeft(7, '0'));
sb.Append("_" + seName);
sb.Append("." + mimeType.Replace("image/", ""));