GithubHelp home page GithubHelp logo

imageoptimizerwebjob's Introduction

Azure Image Optimizer

Build status NuGet

An Azure App Services WebJob that compresses all images being uploaded or deployed to your website. It removes all unneeded bytes from the file to make it as small as possible - without quality loss. When the compression algorithms have finished running, the old image is replaced by the new optimized one.

Industry standard algorithms are being used to provide optimal compression.

Content

Supported file types

  • JPG
  • Gif
  • PNG

Supported platforms

  • Azure App Services (formerly known as Azure Websites)
  • ASP.NET WebForms
  • ASP.NET MVC
  • ASP.NET Core

The Azure.ImageOptimizer will only run on Azure. Installing the NuGet package locally won't do anything until published to Azure.

Install

In your ASP.NET or ASP.NET Core project, install the NuGet package Azure.ImageOptimizer.

ASP.NET

nuget install-package Azure.ImageOptimizer

ASP.NET Core

dotnet add package Azure.ImageOptimizer

When the NuGet package is installed into your web project, simply deploy the project to Azure like you normally do.

The NuGet package will automatically add an Azure Webjob to the deployed artifacts that is performing all of the image optimizations.

Verify installation

Log in to the Azure Portal and navigate to the website you just published. Select the Webjobs blade to make sure you see ImageOptimizer show up on the list like this:

Portal Webjobs

Then head to the SCM dashboard for Azure Webjobs which is located here:

https:// mywebsite.scm.azurewebsites.net/azurejobs/

replace mywebsite with the name of your website.

You should see this:

Scm Webjobs List

Clicking on the ImageOptimizer link will take you to the details page where you can see the console output:

Scm Webjobs details

When you see this, it means that the ImageOptimizer is running correctly.

Track progress

Go to the debug console located at:

https:// mywebsite.scm.azurewebsites.net/DebugConsole

...and click the LogFiles folder. You should then see something similar to this:

Scm Debugconsole

Notice the two files called ImageOptimizer.Cache and ImageOptimizer.log.

The .cache file is used by the optimizer engine to keep track on what files have been previously compressed, so that it doesn't run on files twice unless it has changed.

The .log file is the interesting one containing information about what files were optimized and by how much.

Reset Image Optimizer

To reset the ImageOptimizer Webjob, delete the .cache and .log files from the Azure Debug Console (see the Track Progress section for details).

Then restart the Webjob from the Azure Portal by clicking Stop and then Start.

Portal Webjobs Restart

Configure

By default, ImageOptimizer will optimize all image files located at the root of your website or any subfolder hereof.

This behavior can be customized so that only certain folders are included in the optimzation and even only a certain type of image files (like .png but not .gif).

Here's how to do that.

In the root of your web project, add a file with the name imageoptimizer.json and give it this content.

{
  "optimizations": [
    {
      "includes": [ "img/**/*.*" ],
      "lossy": true
    }
  ]
}

The above JSON shows how to automatically compress all image files inside the img/ folder and its sub folders.

ASP.NET Core projects uses a wwwroot folder, so make sure to include that folder in the includes element if you're using ASP.NET Core. It would then look like this:

{
  "optimizations": [
    {
      "includes": [ "wwwroot/img/**/*.*" ],
      "lossy": true
    }
  ]
}

You can also exclude certain files (or globbing patterns) like this:

{
  "optimizations": [
    {
      "includes": [ "img/**/*.*" ],
      "excludes": [ "**/*.jpg" ],
      "lossy": true
    }
  ]
}

In the above example we are including all files inside the img/ directory, with the exception of an .jpg files.

Use multiple definitions together by adding more rules to the optimizations array.

{
  "optimizations": [
    {
      "includes": [ "wwwroot/albums/*/thumbnail/*.*" ],
      "lossy": true
    },
    {
      "includes": [ "wwwroot/albums/*/*.*" ],
      "lossy": false
    }
  ]
}

This is how the Photo Gallery web app is configured. It will perform lossy compression on images located in wwwroot/albums/*/thumbnail/ folder and lossless compression to any files above the /thumbnail/ sub folder.

Lossy vs. lossless compression

Lossless compression is the process of removing reducing the file size of the image without any quality degredation. In other words, using lossless compression is completely safe to use because it doesn't reduce the quality of the image.

Lossless compression usually yields between 5% and 20% recuction of file size.

Lossy compression is differernt from lossless in that it does reduce the quality of the image it compresses. However, the algorithms being used are so sophisticated that the quality loss may not be visible to the human eye.

The benefit of using lossy compression is that it reduces the file size significantly more than lossless compression.

Lossy compression yields file reductions of up to 75% of the original size.

Best practices

Here are some best practices for image optimization

Compress in advance

All images that are part of your project can be optimized in advance before deploying to Azure. Use any good compression tool such as the Image Optimizer extension for Visual Studio (it uses the exact same algorithms as this NuGet package).

Compress only user uploaded images

When everything is compressed in advance (see above trick), you only need to run the ImageOptimizer on the folders that contain images that didn't exist in advance - also knows as user generated/uploaded images.

Make sure to configure accordingly.

Limit what to compress

When you configure the ImageOptimizer, make sure you don't include the entire website but only includes the folder paths/globbing where the image files are. You'll use fewer resources and avoid unneeded optimizations.

imageoptimizerwebjob's People

Contributors

madskristensen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

imageoptimizerwebjob's Issues

[Question] How to use code in Compressor.cs to compress images in Azure blobs

Is there a possibility that I can use the code in Compressor.cs to compress image file located in Azure blob storage and save the output file to another blob storage. I'm creating an azure function app to do this task but I cannot find a way to modify the code to support azure blob locations as input and output paths (the cjpeg, pingo etc. exe files directly write the compressed image file to the output path. In my case the output path is a azure blob).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.