Пример #1
0
 public static Stream CreateResizedImageFile(Stream originalStream, string ext, double x, double y, double q, string contentType)
 {
     return(ImageResizer.CreateResizedImageFile(originalStream, x, y, q, getImageFormat(contentType)));
 }
Пример #2
0
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            Stream     imageStream;
            BinaryData binaryData;
            var        propNameParam = context.Request.QueryString["NodeProperty"];
            var        propertyName  = string.Empty;
            var        widthParam    = context.Request.QueryString["width"];
            var        heightParam   = context.Request.QueryString["height"];

            if (!string.IsNullOrEmpty(propNameParam))
            {
                propertyName = propNameParam.Replace("$", "#");
                binaryData   = this.GetBinary(propertyName);
            }
            else
            {
                binaryData = this.Binary;
            }

            if (DocumentPreviewProvider.Current != null && DocumentPreviewProvider.Current.IsPreviewOrThumbnailImage(NodeHead.Get(this.Id)))
            {
                // get preview image with watermark or redaction if necessary
                imageStream = string.IsNullOrEmpty(propertyName)
                    ? DocumentPreviewProvider.Current.GetRestrictedImage(this)
                    : DocumentPreviewProvider.Current.GetRestrictedImage(this, propertyName);
            }
            else
            {
                imageStream = binaryData.GetStream();
            }

            //set compressed encoding if necessary
            if (MimeTable.IsCompressedType(this.Extension))
            {
                context.Response.Headers.Add("Content-Encoding", "gzip");
            }

            context.Response.ContentType = binaryData.ContentType;

            imageStream.Position = 0;

            if (!string.IsNullOrEmpty(widthParam) && !string.IsNullOrEmpty(heightParam))
            {
                int width;
                int height;
                if (!int.TryParse(widthParam, out width))
                {
                    width = 200;
                }
                if (!int.TryParse(heightParam, out height))
                {
                    height = 200;
                }

                // compute a new, resized stream on-the-fly
                using (var resizedStream = ImageResizer.CreateResizedImageFile(imageStream, width, height, 80, getImageFormat(binaryData.ContentType)))
                {
                    resizedStream.CopyTo(context.Response.OutputStream);
                }
            }
            else
            {
                imageStream.CopyTo(context.Response.OutputStream);
            }

            imageStream.Close();
        }
Пример #3
0
 public Stream GetDynamicThumbnailStream(int width, int height, string contentType)
 {
     return(ImageResizer.CreateResizedImageFile(Binary.GetStream(), width, height, 80, getImageFormat(contentType)));
 }
Пример #4
0
        /// <summary>
        /// Gets a stream containing the image binary in the specified property (or the default Binary field).
        /// Image dimensions can be determined by providing the width and height values among the parameters.
        /// The image is also redacted if the user does not have enough permissions to see the full image
        /// in case of preview images, only restricted ones.
        /// </summary>
        /// <param name="propertyName">Name of the binary property to serve, default is Binary.</param>
        /// <param name="parameters">Optional parameters that may include image width and height.</param>
        /// <param name="contentType">Out parameter, filled with the binary content type to be served to the client.</param>
        /// <param name="fileName">Out parameter, filled with the file name to serve to the client.</param>
        public Stream GetImageStream(string propertyName, IDictionary <string, object> parameters, out string contentType, out BinaryFileName fileName)
        {
            Stream imageStream;

            object widthParam     = null;
            object heightParam    = null;
            object watermarkParam = null;

            parameters?.TryGetValue("width", out widthParam);
            parameters?.TryGetValue("height", out heightParam);
            parameters?.TryGetValue("watermark", out watermarkParam);

            var binaryData = !string.IsNullOrEmpty(propertyName) ? GetBinary(propertyName) : Binary;

            contentType = binaryData?.ContentType ?? string.Empty;
            fileName    = string.IsNullOrEmpty(propertyName) || string.CompareOrdinal(propertyName, "Binary") == 0
                ? new BinaryFileName(Name)
                : binaryData?.FileName ?? string.Empty;

            if (DocumentPreviewProvider.Current != null && DocumentPreviewProvider.Current.IsPreviewOrThumbnailImage(NodeHead.Get(Id)))
            {
                var options = new PreviewImageOptions
                {
                    BinaryFieldName = propertyName
                };

                if (watermarkParam != null &&
                    ((int.TryParse((string)watermarkParam, out var wmValue) && wmValue == 1) ||
                     (bool.TryParse((string)watermarkParam, out var wmBool) && wmBool)))
                {
                    options.RestrictionType = RestrictionType.Watermark;
                }

                // get preview image with watermark or redaction if necessary
                imageStream = DocumentPreviewProvider.Current.GetRestrictedImage(this, options);
            }
            else
            {
                imageStream = binaryData?.GetStream();
            }

            if (imageStream == null)
            {
                return(new MemoryStream());
            }

            imageStream.Position = 0;

            int ConvertImageParameter(object param)
            {
                if (param != null)
                {
                    // we recognize int and string values as well
                    switch (param)
                    {
                    case int i:
                        return(i);

                    case string s when int.TryParse(s, out var pint):
                        return(pint);
                    }
                }

                return(200);
            }

            // no resize parameters: return the original stream
            if (widthParam == null || heightParam == null)
            {
                return(imageStream);
            }

            var width  = ConvertImageParameter(widthParam);
            var height = ConvertImageParameter(heightParam);

            // compute a new, resized stream on-the-fly
            var resizedStream = ImageResizer.CreateResizedImageFile(imageStream, width, height, 80, getImageFormat(contentType));

            // in case the method created a new stream, we have to close the original to prevent memory leak
            if (!ReferenceEquals(resizedStream, imageStream))
            {
                imageStream.Dispose();
            }

            return(resizedStream);
        }