nopcommerce product image URL

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
9 years ago
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
9 years ago
luothree wrote:
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


I think you need to implement a controller action that takes product id and return product picture by querying on Product, Picture and Product_Picture_Mapping tables
9 years ago
Thanks for your fast reply.

I am doing some affiliate sites (by manufacturers) as presentation to different customers. Actually I like to have two kinds of datum from my NOP system. One is image URL, one is the item detail URL (like a landing page).

How can I get these two kinds of info from NOP system. I set my SQL for remote connection, I can access remotely to NOP backend database. I am new to MVC and NOP, used to web form, it is a simple way to do the work.

Thanks

James
9 years ago
luothree wrote:
Thanks for your fast reply.

I am doing some affiliate sites (by manufacturers) as presentation to different customers. Actually I like to have two kinds of datum from my NOP system. One is image URL, one is the item detail URL (like a landing page).

How can I get these two kinds of info from NOP system. I set my SQL for remote connection, I can access remotely to NOP backend database. I am new to MVC and NOP, used to web form, it is a simple way to do the work.

Thanks

James


Hi,


Since you can access nop SQL Db, you can access the product detail url using UrlRecord.

Run this query, you will get the idea.

Select Product.Name, UrlRecord.Slug from UrlRecord  inner join Product on Product.Id = UrlRecord.EntityId where EntityName = 'Product' and IsActive = 1
9 years ago
Hi Jey

Thanks a lot! Yes, you solved one of my questions: for the product URL. Yes, I saw there are Category and Blog SEO URLs too. You are right, I just need the Product URL.

Do you know whether it is possible to setup product image (the main default image)? I like to have an image for each product at external sites too.

Thanks again!

James
9 years ago
luothree wrote:
Hi Jey

Thanks a lot! Yes, you solved one of my questions: for the product URL. Yes, I saw there are Category and Blog SEO URLs too. You are right, I just need the Product URL.

Do you know whether it is possible to setup product image (the main default image)? I like to have an image for each product at external sites too.

Thanks again!

James


Hi,

For image urls, have a look at GetPictureUrl method under PictureService in Nop.Services project. So you can follow the same logic to construct the image name and path for you. Here it is, [based on nop 3.20]

        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;
        }
7 years ago
In case that there are people trying  to figure out how to get picture urls for a massive ammount of product they will find out that GetPictureUrl() is a little bit slow during the process. You can use :
 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/", ""));


providing you have a productId.
This will return the picture Url. Change the path if necessary.
If you have any questions about the process please PM me.
7 years ago
Thanks, this did the trick for me.
3 years ago
If you're looking for straight T-SQL to do the same thing, based on the answer above.  Please note that I had to remove 'content/' from the URL generation because I was using NopCom v4.x

SELECT
                p.Id,
                p.Name,
                mainPix.SeoFilename,
                mainPix.PictureId,
                mainPix.MimeType,
                'https://www.mysite.com/' + --store root
                'images/thumbs/' + --image store
                RIGHT('0000000' + CAST(mainPix.PictureId AS VARCHAR(20)), 7) + '_' + mainPix.SeoFilename + '.'
                + REPLACE (mainPix.MimeType, 'image/', '') AS PictureUrl
FROM            dbo.Product                                            p
    OUTER APPLY ( SELECT    TOP ( 1 )
                            SeoFilename,
                            PictureId,
                            MimeType
                  FROM      dbo.Picture
                      JOIN  dbo.Product_Picture_Mapping ON Product_Picture_Mapping.PictureId = Picture.Id
                  WHERE     ProductId = p.Id
                  ORDER BY  dbo.Product_Picture_Mapping.DisplayOrder ) mainPix;
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.