Пример #1
0
 private void CacheTransformedImage(ImageTransformationModel request, byte[] transformedImageBytes)
 {
     Task.Run(() =>
     {
         // We shouldn't wait for the image to be saved to cache before returning the image to the requester
         imageCacheHandler.Set(transformedImageBytes, request);
         logger.LogInformation($"Saving {request} in cache");
     }).ConfigureAwait(false);
 }
Пример #2
0
 private byte[] OriginalImage(ImageTransformationModel request)
 {
     if (!originalImageStorage.Exists(request.Name))
     {
         logger.LogInformation($"{request.Name} does not exist");
         return(null);
     }
     logger.LogInformation($"{request.Name} requested without transformation. Serving original image");
     return(originalImageStorage.Get(request.Name));
 }
Пример #3
0
        private byte[] TransformExistingImageAndCache(ImageTransformationModel request)
        {
            var originalImageBytes = originalImageStorage.Get(request.Name);

            var transformedImageBytes = imageTransformer.Transform(originalImageBytes, request);

            CacheTransformedImage(request, transformedImageBytes);

            logger.LogInformation($"{request} has been processed. Served from transformer");

            return(transformedImageBytes);
        }
        /// <summary>
        /// Transform image bytes from one format or resolution to another, optionally with a watermark
        /// </summary>
        /// <param name="bytes">Byte array containing the image file data</param>
        /// <param name="request">Image transformation parameters</param>
        /// <returns>Array of bytes representing the transformed image file</returns>
        public byte[] Transform(byte[] bytes, ImageTransformationModel request)
        {
            var stopwatch = Stopwatch.StartNew();

            byte[] returnBytes;

            var image = LoadImage(bytes);

            var currentImageFormat = image.RawFormat;
            var newImageFormat     = ParseImageFormatOrDefault(request.Format) ?? currentImageFormat;
            var currentDims        = (image.Width, image.Height);
            var maxDims            = (request.MaxWidth, request.MaxHeight);
            var newDims            = CalculateNewDimensions(currentDims, maxDims);

            if (string.IsNullOrWhiteSpace(request.Watermark) && string.IsNullOrWhiteSpace(request.BackgroundColour) && newDims == currentDims && newImageFormat == currentImageFormat)
            {
                logger.LogTrace("No transformation required. Returning original image");
                // No transformation required
                returnBytes = bytes;
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(request.BackgroundColour))
                {
                    logger.LogTrace("Colouring background");
                    image = ColourBackground(request.BackgroundColour, image);
                }

                if (!string.IsNullOrWhiteSpace(request.Watermark))
                {
                    logger.LogTrace("Drawing watermark");
                    DrawWatermark(request.Watermark, image);
                }

                if (newDims != currentDims)
                {
                    logger.LogTrace("Resizing image");
                    image = ResizeImage(image, newDims);
                }

                returnBytes = SaveImageToByteArray(image, newImageFormat);
            }
            image.Dispose();

            logger.LogInformation($"Transformed image in {stopwatch.ElapsedMilliseconds} ms");

            return(returnBytes);
        }
Пример #5
0
 /// <summary>
 /// Attempt to get image from transformed image cache, or transform original image and store in cache, or return image if no transformation requested
 /// </summary>
 /// <param name="request">Descriptor of the requested transformed image</param>
 /// <returns>Bytes represenging the requested image file</returns>
 public byte[] Get(ImageTransformationModel request)
 {
     if (request.IsOriginalImage)
     {
         // No transformation requested. Return original image, and don't cache.
         return(OriginalImage(request));
     }
     if (imageCacheHandler.Exists(request))
     {
         // Transformation exists in cache storage. Return image
         return(ImageFromCache(request));
     }
     else if (!originalImageStorage.Exists(request.Name))
     {
         // The original image doesn't exist.
         return(ImageDoesNotExist(request));
     }
     else
     {
         // Image exists, but the requested transform doesn't exist in cache. Transform image, return and cache.
         return(TransformExistingImageAndCache(request));
     }
 }
        /// <summary>
        /// Checks to see whether a transformed image exists in cache that satisfies the transform requirements
        /// </summary>
        /// <param name="request">Descriptor of the requested transformed image</param>
        /// <returns>true if transformed image exists in cache</returns>
        public bool Exists(ImageTransformationModel request)
        {
            var filename = requestConverter.ConvertTo(request);

            return(cacheStorage.Exists(filename));
        }
        /// <summary>
        /// Writes bytes to cache, indexed to name and transform requirements
        /// </summary>
        /// <param name="bytes">byte array containing the transformed image file data</param>
        /// <param name="request">Descriptor of the requested transformed image</param>
        public void Set(byte[] bytes, ImageTransformationModel request)
        {
            var filename = requestConverter.ConvertTo(request);

            cacheStorage.Set(filename, bytes);
        }
        /// <summary>
        /// Reads bytes from cache, indexed to name and transform requirements
        /// </summary>
        /// <param name="request">Descriptor of the requested transformed image</param>
        public byte[] Get(ImageTransformationModel request)
        {
            var filename = requestConverter.ConvertTo(request);

            return(cacheStorage.Get(filename));
        }
Пример #9
0
 private byte[] ImageFromCache(ImageTransformationModel request)
 {
     logger.LogInformation($"{request} exists in cache. Serving from cache");
     return(imageCacheHandler.Get(request));
 }
Пример #10
0
 private byte[] ImageDoesNotExist(ImageTransformationModel request)
 {
     logger.LogInformation($"{request} does not exist");
     return(null);
 }