static MemoryStream GetMemoryStream(Picture picture, int targetSize)
 {
     var localUrl = EngineContext.Current.Resolve<IPictureService>().GetThumbLocalPath(picture, targetSize);
     var fileStream = new FileStream(localUrl, FileMode.Open, FileAccess.Read, FileShare.Read);
     var memoryStream = StreamToMemory(fileStream);
     fileStream.Close();
     return memoryStream;
 }
Example #2
0
        public virtual void DeleteCachedImages(Picture picture)
        {
            string filter = string.Format("{0}*.*", picture.Id.ToString("0000000"));

            var files = _cacheRootDir.EnumerateFiles(filter, SearchOption.AllDirectories);
            foreach (var file in files)
            {
                file.Delete();
            }
        }
        public void Can_save_and_load_picture()
        {
            var picture = new Picture
            {
                PictureBinary = new byte[] { 1, 2, 3 },
                MimeType = "image/pjpeg",
                SeoFilename = "seo filename 1",
                IsNew = true
            };

            var fromDb = SaveAndLoadEntity(picture);
            fromDb.ShouldNotBeNull();
            fromDb.PictureBinary.ShouldEqual(new byte[] { 1, 2, 3 });
            fromDb.MimeType.ShouldEqual("image/pjpeg");
            fromDb.SeoFilename.ShouldEqual("seo filename 1");
            fromDb.IsNew.ShouldEqual(true);
        }
        public bool AddEnclosue(SyndicationItem item, Picture picture, string pictureUrl)
        {
            if (picture != null && pictureUrl.HasValue())
            {
                long pictureLength = 10000;		// 0 omits the length attribute but that invalidates the feed

                if (picture.PictureBinary != null)
                    pictureLength = picture.PictureBinary.LongLength;

                var enclosure = SyndicationLink.CreateMediaEnclosureLink(new Uri(pictureUrl), picture.MimeType.EmptyNull(), pictureLength);

                item.Links.Add(enclosure);

                return true;
            }
            return false;
        }
 public virtual byte[] LoadPictureBinary(Picture picture)
 {
     return LoadPictureBinary(picture, this.StoreInDb);
 }
        public virtual string GetThumbLocalPath(Picture picture, int targetSize = 0, bool showDefaultPicture = true)
        {
            // 'GetPictureUrl' takes care of creating the thumb when not created already
            string url = this.GetPictureUrl(picture, targetSize, showDefaultPicture);

            if (url.HasValue())
            {
                var settings = this.CreateResizeSettings(targetSize);

                var cachedImage = _imageCache.GetCachedImage(picture, settings);
                if (cachedImage.Exists)
                {
                    return cachedImage.LocalPath;
                }

                if (showDefaultPicture)
                {
                    var fileName = this.GetDefaultImageFileName();
                    cachedImage = _imageCache.GetCachedImage(
                        0,
                        Path.GetFileNameWithoutExtension(fileName),
                        Path.GetExtension(fileName).TrimStart('.'),
                        settings);
                    if (cachedImage.Exists)
                    {
                        return cachedImage.LocalPath;
                    }
                }
            }

            return string.Empty;
        }
        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;
            }

            if (picture.IsNew)
            {
                _imageCache.DeleteCachedImages(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);
            }

            url = this.GetProcessedImageUrl(
                pictureBinary,
                picture.Id,
                picture.SeoFilename,
                MimeTypes.MapMimeTypeToExtension(picture.MimeType),
                targetSize,
                storeLocation);

            return url;
        }
 public virtual Size GetPictureSize(Picture picture)
 {
     byte[] pictureBinary = LoadPictureBinary(picture);
     return GetPictureSize(pictureBinary);
 }
        public virtual void DeletePicture(Picture picture)
        {
            if (picture == null)
                throw new ArgumentNullException("picture");

            // delete thumbs
            _imageCache.DeleteCachedImages(picture);

            // delete from file system
            if (!this.StoreInDb)
            {
                DeletePictureOnFileSystem(picture);
            }

            // delete from database
            _pictureRepository.Delete(picture);

            // event notification
            _eventPublisher.EntityDeleted(picture);
        }
 /// <summary>
 /// Adds an image to the cache.
 /// </summary>
 /// <param name="picture">The picture object needed for building the resulting file name.</param>
 /// <param name="buffer">The image binary data.</param>
 /// <param name="settings">The image processing settings. This object, if not <c>null</c>, is hashed and appended to the resulting file name.</param>
 public static void AddImageToCache(this IImageCache imageCache, Picture picture, byte[] buffer, object settings = null)
 {
     Guard.ArgumentNotNull(() => picture);
     imageCache.AddImageToCache(picture.Id, picture.SeoFilename, MimeTypes.MapMimeTypeToExtension(picture.MimeType), buffer, settings);
 }
Example #11
0
		protected virtual void WritePicture(XmlWriter writer, XmlExportContext context, Picture picture, int thumbSize, int defaultSize)
		{
			if (picture != null)
			{
				writer.WriteStartElement("Picture");
				writer.Write("Id", picture.Id.ToString());
				writer.Write("SeoFileName", picture.SeoFilename);
				writer.Write("MimeType", picture.MimeType);
				writer.Write("ThumbImageUrl", _pictureService.GetPictureUrl(picture, thumbSize, false, context.Store.Url));
				writer.Write("ImageUrl", _pictureService.GetPictureUrl(picture, defaultSize, false, context.Store.Url));
				writer.Write("FullSizeImageUrl", _pictureService.GetPictureUrl(picture, 0, false, context.Store.Url));
				writer.WriteEndElement();
			}
		}
        private PictureModel CreatePictureModel(ProductDetailsPictureModel model, Picture picture, int pictureSize)
        {
            var result = new PictureModel
            {
                PictureId = picture.Id,
                ThumbImageUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.ProductThumbPictureSizeOnProductDetailsPage),
                ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize, !_catalogSettings.HideProductDefaultPictures),
                FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                Title = model.Name,
                AlternateText = model.AlternateText
            };

            return result;
        }
        protected virtual string ProductPictureToHtml(Picture picture, Language language, string productName, string productUrl, string storeLocation)
        {
            if (picture != null && _mediaSettings.MessageProductThumbPictureSize > 0)
            {
                var imageUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.MessageProductThumbPictureSize, false, storeLocation);
                if (imageUrl.HasValue())
                {
                    var title = _localizationService.GetResource("Media.Product.ImageLinkTitleFormat", language.Id).FormatInvariant(productName);
                    var alternate = _localizationService.GetResource("Media.Product.ImageAlternateTextFormat", language.Id).FormatInvariant(productName);

                    var polaroid = "padding: 3px; background-color: #fff; border: 1px solid #ccc; border: 1px solid rgba(0,0,0,.2);";
                    var style = "max-width: {0}px; max-height: {0}px; {1}".FormatInvariant(_mediaSettings.MessageProductThumbPictureSize, polaroid);

                    var image = "<img src=\"{0}\" alt=\"{1}\" title=\"{2}\" style=\"{3}\" />".FormatInvariant(imageUrl, alternate, title, style);

                    if (productUrl.IsEmpty())
                        return image;

                    return "<a href=\"{0}\" style=\"border: none;\">{1}</a>".FormatInvariant(productUrl, image);
                }
            }
            return "";
        }
        public virtual byte[] LoadPictureBinary(Picture picture, bool fromDb)
        {
            if (picture == null)
                throw new ArgumentNullException("picture");

            byte[] result = null;
            if (fromDb)
            {
                result = picture.PictureBinary;
            }
            else
            {
                result = LoadPictureFromFile(picture.Id, picture.MimeType);
            }

            return result;
        }
        public PictureResult(Picture picture, int targetSize)
            : base(
         GetMemoryStream(picture, targetSize), picture.MimeType)
        {

        }
        /// <summary>
        /// Delete a picture on file system
        /// </summary>
        /// <param name="picture">Picture</param>
        protected virtual void DeletePictureOnFileSystem(Picture picture)
        {
            if (picture == null)
                throw new ArgumentNullException("picture");

            string lastPart = MimeTypes.MapMimeTypeToExtension(picture.MimeType);
            string fileName = string.Format("{0}-0.{1}", picture.Id.ToString("0000000"), lastPart);
            string filePath = GetPictureLocalPath(fileName);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
 /// <summary>
 /// Gets an instance of the <see cref="CachedImageResult"/> object, which contains information about a cached image.
 /// </summary>
 /// <param name="picture">The picture object for which to resolve a cached image.</param>
 /// <param name="settings">The image processing settings.</param>
 /// <returns>An instance of the <see cref="CachedImageResult"/> object</returns>
 /// <remarks>If the requested image does not exist in the cache, the value of the <c>Exists</c> property will be <c>false</c>.</remarks>
 public static CachedImageResult GetCachedImage(this IImageCache imageCache, Picture picture, object settings = null)
 {
     Guard.ArgumentNotNull(() => picture);
     return imageCache.GetCachedImage(picture.Id, picture.SeoFilename, MimeTypes.MapMimeTypeToExtension(picture.MimeType), settings);
 }