Image thumbnail automatic creation: How does it work?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hello,

I'm trying to wrire a process that imports images into the /images folder and creates the appropriate records in the Nop_Pictures & Nop_ProductPictures tables.

I've been able to do this just fine. What I notice is that as long as I put an image named 0000123_0.jpg, for example, into the images/ folder, that NOP automatically will create the different thumbnail images inside of images/thumbs as soon as I execute a search that returns the product in question.

This is a great thing.

However, my coworker is having problems on his machine seeing this happen. It just doesn't seem to work on his machine.

So, my question is, how does this actually work under the covers, and how can I go about digging into that code? I have downloaded the source code from CodePlex, but the code behind for the search page doesn't seem to do any magic image creation.

Thank you!
Josh
13 years ago
From memory. When a page is loaded that requires an image of a certain size it checks that its avaliable and if not takes the orginial image and creates the thumbnail.  If your coworker is having issues I would check file permissions the images directory.
13 years ago
Thank you,

I think I discovered that there is a setting that I had set in my own installation which he had not yet set.

That setting is:

"Media.Images.StoreInDB"

And, GetPictureUrl does indeed create the thumb if "StoreInDb" (wrapper on top of that setting above) is set:

  /// <summary>
        /// Get a picture URL
        /// </summary>
        /// <param name="picture">Picture instance</param>
        /// <param name="targetSize">The target picture size (longest side)</param>
        /// <param name="showDefaultPicture">A value indicating whether the default picture is shown</param>
        /// <returns></returns>
        public static string GetPictureUrl(Picture picture, int targetSize,
            bool showDefaultPicture)
        {
            string url = string.Empty;
            if (picture == null || picture.LoadPictureBinary().Length == 0)
            {
                if(showDefaultPicture)
                {
                    url = GetDefaultPictureUrl(targetSize);
                }
                return url;
            }

            string[] parts = picture.Extension.Split('/');
            string lastPart = parts[parts.Length - 1];
            switch (lastPart)
            {
                case "pjpeg":
                    lastPart = "jpg";
                    break;
                case "x-png":
                    lastPart = "png";
                    break;
                case "x-icon":
                    lastPart = "ico";
                    break;
            }

            string localFilename = string.Empty;
            if (picture.IsNew)
            {
                string filter = string.Format("{0}*.*", picture.PictureId.ToString("0000000"));
                string[] currentFiles = System.IO.Directory.GetFiles(PictureManager.LocalThumbImagePath, filter);
                foreach (string currentFileName in currentFiles)
                    File.Delete(Path.Combine(PictureManager.LocalThumbImagePath, currentFileName));

                picture = UpdatePicture(picture.PictureId, picture.LoadPictureBinary(), picture.Extension, false);
            }
           lock (s_lock)
            {
                if (targetSize == 0)
                {
                    localFilename = string.Format("{0}.{1}", picture.PictureId.ToString("0000000"), lastPart);
                    if (!File.Exists(Path.Combine(PictureManager.LocalThumbImagePath, localFilename)))
                    {
                        if (!System.IO.Directory.Exists(PictureManager.LocalThumbImagePath))
                        {
                            System.IO.Directory.CreateDirectory(PictureManager.LocalThumbImagePath);
                        }
                        File.WriteAllBytes(Path.Combine(PictureManager.LocalThumbImagePath, localFilename), picture.LoadPictureBinary());
                    }
                }
    
           else
                {
                    localFilename = string.Format("{0}_{1}.{2}", picture.PictureId.ToString("0000000"), targetSize, lastPart);
                    if (!File.Exists(Path.Combine(PictureManager.LocalThumbImagePath, localFilename)))
                    {
                        if (!System.IO.Directory.Exists(PictureManager.LocalThumbImagePath))
                        {
                            System.IO.Directory.CreateDirectory(PictureManager.LocalThumbImagePath);
                        }
                        using (MemoryStream stream = new MemoryStream(picture.LoadPictureBinary()))
                        {
                            var b = new Bitmap(stream);

                            var newSize = CalculateDimensions(b.Size, targetSize);

                            if (newSize.Width < 1)
                                newSize.Width = 1;
                            if (newSize.Height < 1)
                                newSize.Height = 1;

                            var newBitMap = new Bitmap(newSize.Width, newSize.Height);
                            var g = Graphics.FromImage(newBitMap);
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                            g.DrawImage(b, 0, 0, newSize.Width, newSize.Height);
                            var ep = new EncoderParameters();
                            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, PictureManager.ImageQuality);
                            newBitMap.Save(Path.Combine(PictureManager.LocalThumbImagePath, localFilename), getImageCodeInfo("image/jpeg"), ep);
                            newBitMap.Dispose();
                            b.Dispose();
                        }
                    }
                }
            }
            url = CommonHelper.GetStoreLocation() + "images/thumbs/" + localFilename;
            return url;
        }

[u][/u]
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.