Exemplo n.º 1
0
        /// <summary>
        ///  formate of thumbnail.
        /// /thumbnail/100/200/guid
        /// /thumbnail/100/200/imagefolder/image/name.jpg.
        /// </summary>
        /// <param name="SiteDb"></param>
        /// <param name="RelativeUrl"></param>
        /// <returns></returns>
        public static Thumbnail GetThumbnail(SiteDb SiteDb, string RelativeUrl)
        {
            ThumbnailRequest request = ParseThumbnailRequest(RelativeUrl);

            Guid imageid = request.ImageId;

            if (request.ImageId == default(Guid))
            {
                if (string.IsNullOrEmpty(request.Path))
                {
                    //TODO should implement the not image icon
                }
                else
                {
                    string url   = request.Path;
                    var    index = url.IndexOf("?");
                    if (index > -1)
                    {
                        url = url.Substring(0, index);
                    }
                    Image koobooimage = SiteDb.Images.GetByUrl(url);

                    if (koobooimage != null)
                    {
                        imageid = koobooimage.Id;
                    }
                }
            }

            return(SiteDb.Thumbnails.GetThumbnail(imageid, request.Width, request.Height));
        }
Exemplo n.º 2
0
        public Thumbnail GetThumbnail(Guid imageid, int width, int height)
        {
            Guid thumbnailid = Kooboo.Data.IDGenerator.GetImageThumbNailId(imageid, height, width);

            Thumbnail thumbnail = Get(thumbnailid);

            if (thumbnail == null)
            {
                thumbnail = new Thumbnail();

                //Image koobooimage = SiteDb.ImagePool.Get(imageid);
                Kooboo.Sites.Models.Image koobooimage = SiteDb.Images.Get(imageid);

                if (koobooimage == null)
                {
                    return(null);
                }

                // calculate the width or height.
                if (height <= 0 && koobooimage.Width > 0)
                {
                    height = (int)width * koobooimage.Height / koobooimage.Width;
                }

                if (width <= 0 && koobooimage.Height > 0)
                {
                    width = (int)height * koobooimage.Width / koobooimage.Height;
                }

                if (height <= 0)
                {
                    height = 90;
                }
                if (width <= 0)
                {
                    width = 90;
                }


                thumbnail.Width     = width;
                thumbnail.Height    = height;
                thumbnail.ImageId   = imageid;
                thumbnail.Extension = koobooimage.Extension;

                thumbnail.Id          = thumbnailid;
                thumbnail.ContentType = "image";
                string imagetype = thumbnail.Extension;
                if (!string.IsNullOrEmpty(imagetype) && imagetype.StartsWith("."))
                {
                    imagetype = imagetype.Substring(1);
                }
                thumbnail.ContentType = thumbnail.ContentType + "/" + imagetype;

                //"image/svg+xml"
                if (!string.IsNullOrEmpty(imagetype) && imagetype.ToLower() == "svg")
                {
                    thumbnail.ContentType  = thumbnail.ContentType + "+xml";
                    thumbnail.ContentBytes = koobooimage.ContentBytes;
                }
                else
                {
                    if (koobooimage == null || koobooimage.ContentBytes == null)
                    {
                        return(null);
                    }
                    var contentBytes = Kooboo.Lib.Compatible.CompatibleManager.Instance.Framework.GetThumbnailImage(koobooimage.ContentBytes, width, height);
                    if (contentBytes == null)
                    {
                        return(null);
                    }

                    thumbnail.ContentBytes = contentBytes;
                }

                AddOrUpdate(thumbnail);
            }
            return(thumbnail);
        }