Properly preserve transparency across image types

Our image resize robot allows you to set a custom background for the resized images. By default, a white background is added to transparent images. In order to preserve transparency, please use "background": "none". Check this demo to learn how to properly use the /file/filter robot to preserve transparency for file types that it supports (png, gif, etc.).



If you want Transloadit to handle uploads for you, here is some example HTML and JavaScript that can be integrated directly into your website.

We have a great new JavaScript uploader in the works called Uppy and you are welcome to give that a shot, but for now, the following example uses our jQuery SDK and it is still the recommended way to integrate Transloadit into web browsers.

<html>
<head><title>Some demo title</title></head>
<body>
<form action="/uploads" enctype="multipart/form-data" method="POST">
  <input type="file" name="my_file" multiple="multiple" />
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//assets.transloadit.com/js/jquery.transloadit2-v2-latest.js"></script>
<script type="text/javascript">
$(function() {
  $('form').transloadit({
    wait: true,
    triggerUploadOnFileSelection: true,
    params: {
      auth: {
        key: "YOUR_TRANSLOADIT_AUTH_KEY",
      },
      steps: {
        "nontransparent": {
          "use": ":original",
          "robot": "/file/filter",
          "accepts": [
            [
              "${file.mime}",
              "=",
              "image/jpeg"
            ],
            [
              "${file.mime}",
              "=",
              "image/jpg"
            ]
          ],
          "error_on_decline": false
        },
        "maybetransparent": {
          "use": ":original",
          "robot": "/file/filter",
          "declines": [
            [
              "${file.mime}",
              "=",
              "image/jpeg"
            ],
            [
              "${file.mime}",
              "=",
              "image/jpg"
            ]
          ],
          "error_on_decline": false
        },
        "resize_nontransparent": {
          "use": "nontransparent",
          "robot": "/image/resize",
          "result": true,
          "resize_strategy": "fit",
          "width": 400,
          "height": 300
        },
        "resize_maybetransparent": {
          "use": "maybetransparent",
          "robot": "/image/resize",
          "result": true,
          "resize_strategy": "fit",
          "background": "none",
          "width": 400,
          "height": 300
        }
      }
    },
  });
});
</script>
</body>
</html>

Notifying your server

Transloadit will then accept the upload on behalf of your site, process all the files according to your instructions and export to your storage platform of choice. Your visitor will now be able to upload links to the results (example) to your website's /uploads URL.

This takes the pressure off your platform, and ensures the user will have a smooth experience.

Typically, it is advisable to save a record of the result in your database, so you will need to inspect the transloadit POST payload that was sent to your /uploads, or Notification URL. In PHP, that would look like this:

print_r(json_decode($_POST['transloadit'], true));

Would you like to handle your own file uploads? Or do you have a big library of files ready to be encoded? You could still leverage Transloadit's highly scalable encoding platform by letting us import from your source or post files directly from your server.

For these cases, we have a REST API available. You can write code in your favorite language to talk to it. Setting it up should be easy, since we have SDKs available for all major languages and platforms.

This demo uses :original, which indicates a user upload that was received by Transloadit. So, for this SDK, you will likely want to add one of our import Robots and use their downloads instead.

Here is an example of our PHP SDK.

// Install "transloadit/php-sdk" via Composer (https://getcomposer.org)
use transloadit\Transloadit;
$transloadit = new Transloadit(array(
  "key"    => "YOUR_TRANSLOADIT_AUTH_KEY",
  "secret" => "YOUR_TRANSLOADIT_AUTH_SECRET",
));

$response = $transloadit->createAssembly(array(
  "files" => array(
    "./lolcat.jpg",
  ),
  "params" => array(
    "steps" => json_decode({
      "nontransparent": {
        "use": ":original",
        "robot": "/file/filter",
        "accepts": [
          [
            "${file.mime}",
            "=",
            "image/jpeg"
          ],
          [
            "${file.mime}",
            "=",
            "image/jpg"
          ]
        ],
        "error_on_decline": false
      },
      "maybetransparent": {
        "use": ":original",
        "robot": "/file/filter",
        "declines": [
          [
            "${file.mime}",
            "=",
            "image/jpeg"
          ],
          [
            "${file.mime}",
            "=",
            "image/jpg"
          ]
        ],
        "error_on_decline": false
      },
      "resize_nontransparent": {
        "use": "nontransparent",
        "robot": "/image/resize",
        "result": true,
        "resize_strategy": "fit",
        "width": 400,
        "height": 300
      },
      "resize_maybetransparent": {
        "use": "maybetransparent",
        "robot": "/image/resize",
        "result": true,
        "resize_strategy": "fit",
        "background": "none",
        "width": 400,
        "height": 300
      }
    }, true),
  ),
));

Would you like to handle your own file uploads? Or do you have a big library of files ready to be encoded? You could still leverage Transloadit's highly scalable encoding platform by letting us import from your source or post files directly from your server.

For these cases, we have a REST API available. You can write code in your favorite language to talk to it. Setting it up should be easy, since we have SDKs available for all major languages and platforms.

This demo uses :original, which indicates a user upload that was received by Transloadit. So, for this SDK, you will likely want to add one of our import Robots and use their downloads instead.

Here is an example of our Ruby SDK.

# gem install transloadit
transloadit = Transloadit.new(
  :key    => 'YOUR_TRANSLOADIT_AUTH_KEY',
  :secret => 'YOUR_TRANSLOADIT_AUTH_SECRET'
)

nontransparent = transloadit.step 'nontransparent', '/file/filter',
  :use => ":original",
  :accepts => [["${file.mime}","=","image/jpeg"],["${file.mime}","=","image/jpg"]],
  :error_on_decline => false
})
maybetransparent = transloadit.step 'maybetransparent', '/file/filter',
  :use => ":original",
  :declines => [["${file.mime}","=","image/jpeg"],["${file.mime}","=","image/jpg"]],
  :error_on_decline => false
})
resize_nontransparent = transloadit.step 'resize_nontransparent', '/image/resize',
  :use => "nontransparent",
  :result => true,
  :resize_strategy => "fit",
  :width => 400,
  :height => 300
})
resize_maybetransparent = transloadit.step 'resize_maybetransparent', '/image/resize',
  :use => "maybetransparent",
  :result => true,
  :resize_strategy => "fit",
  :background => "none",
  :width => 400,
  :height => 300
})

assembly = transloadit.assembly(
  :steps => [ nontransparent, maybetransparent, resize_nontransparent, resize_maybetransparent ]
)

response = assembly.submit! open('./lolcat.jpg')
until response.finished?
  sleep 1; response.reload!
end

if !response.error?
 # handle success
end

Would you like to handle your own file uploads? Or do you have a big library of files ready to be encoded? You could still leverage Transloadit's highly scalable encoding platform by letting us import from your source or post files directly from your server.

For these cases, we have a REST API available. You can write code in your favorite language to talk to it. Setting it up should be easy, since we have SDKs available for all major languages and platforms.

This demo uses :original, which indicates a user upload that was received by Transloadit. So, for this SDK, you will likely want to add one of our import Robots and use their downloads instead.

Here is an example of our Node.js SDK.

// npm install --save --save-exact transloadit
var TransloaditClient = require('transloadit');
var transloadit       = new TransloaditClient({
  authKey    : "YOUR_TRANSLOADIT_AUTH_KEY",
  authSecret : "YOUR_TRANSLOADIT_AUTH_SECRET"
});

transloadit.addFile("myimage", "./lolcat.jpg");

var options = {
  params: {
    steps: {
      "nontransparent": {
        "use": ":original",
        "robot": "/file/filter",
        "accepts": [
          [
            "${file.mime}",
            "=",
            "image/jpeg"
          ],
          [
            "${file.mime}",
            "=",
            "image/jpg"
          ]
        ],
        "error_on_decline": false
      },
      "maybetransparent": {
        "use": ":original",
        "robot": "/file/filter",
        "declines": [
          [
            "${file.mime}",
            "=",
            "image/jpeg"
          ],
          [
            "${file.mime}",
            "=",
            "image/jpg"
          ]
        ],
        "error_on_decline": false
      },
      "resize_nontransparent": {
        "use": "nontransparent",
        "robot": "/image/resize",
        "result": true,
        "resize_strategy": "fit",
        "width": 400,
        "height": 300
      },
      "resize_maybetransparent": {
        "use": "maybetransparent",
        "robot": "/image/resize",
        "result": true,
        "resize_strategy": "fit",
        "background": "none",
        "width": 400,
        "height": 300
      }
    }
  }
};
transloadit.createAssembly(options, function(err, result) {
  if (err) {
    throw new Error(err);
  }

  console.log(result);
});

Would you like to handle your own file uploads? Or do you have a big library of files ready to be encoded? You could still leverage Transloadit's highly scalable encoding platform by letting us import from your source or post files directly from your server.

For these cases, we have a REST API available. You can write code in your favorite language to talk to it. Setting it up should be easy, since we have SDKs available for all major languages and platforms.

This demo uses :original, which indicates a user upload that was received by Transloadit. So, for this SDK, you will likely want to add one of our import Robots and use their downloads instead.

Here is an example of our Go SDK.

// go get github.com/transloadit/go-sdk
options := transloadit.DefaultConfig
options.AuthKey = "YOUR_TRANSLOADIT_AUTH_KEY"
options.AuthSecret = "YOUR_TRANSLOADIT_AUTH_SECRET"
client, err := transloadit.NewClient(options)
if err != nil {
    panic(err)
}

assembly := client.CreateAssembly()
assembly.AddFile("myimage", "./lolcat.jpg")

assembly.AddStep("nontransparent", map[string]interface{}{
  "use": ":original",
  "robot": "/file/filter",
  "accepts": [["${file.mime}","=","image/jpeg"],["${file.mime}","=","image/jpg"]],
  "error_on_decline": false
})assembly.AddStep("maybetransparent", map[string]interface{}{
  "use": ":original",
  "robot": "/file/filter",
  "declines": [["${file.mime}","=","image/jpeg"],["${file.mime}","=","image/jpg"]],
  "error_on_decline": false
})assembly.AddStep("resize_nontransparent", map[string]interface{}{
  "use": "nontransparent",
  "robot": "/image/resize",
  "result": true,
  "resize_strategy": "fit",
  "width": 400,
  "height": 300
})assembly.AddStep("resize_maybetransparent", map[string]interface{}{
  "use": "maybetransparent",
  "robot": "/image/resize",
  "result": true,
  "resize_strategy": "fit",
  "background": "none",
  "width": 400,
  "height": 300
})

info, err := assembly.Upload()
if err != nil {
    panic(err)
}

Instead of sending encoding instructions directly to Transloadit, you can also save them in a secured way as a "Template". You can then refer to the template_id in your requests, making sure that secrets and instructions no longer have to be transmitted, which might be a security risk in many cases.

Here is the 'raw' Template, in case you would like to save these instructions safely in your account.

{
  "steps": {
    "nontransparent": {
      "use": ":original",
      "robot": "/file/filter",
      "accepts": [
        [
          "${file.mime}",
          "=",
          "image/jpeg"
        ],
        [
          "${file.mime}",
          "=",
          "image/jpg"
        ]
      ],
      "error_on_decline": false
    },
    "maybetransparent": {
      "use": ":original",
      "robot": "/file/filter",
      "declines": [
        [
          "${file.mime}",
          "=",
          "image/jpeg"
        ],
        [
          "${file.mime}",
          "=",
          "image/jpg"
        ]
      ],
      "error_on_decline": false
    },
    "resize_nontransparent": {
      "use": "nontransparent",
      "robot": "/image/resize",
      "result": true,
      "resize_strategy": "fit",
      "width": 400,
      "height": 300
    },
    "resize_maybetransparent": {
      "use": "maybetransparent",
      "robot": "/image/resize",
      "result": true,
      "resize_strategy": "fit",
      "background": "none",
      "width": 400,
      "height": 300
    }
  }
}

Try it out

Maximum size for all demos is 100MB.

0% Complete

If you are wondering about security, we can save secrets server-side and encrypt them for you inside a Template. Furthermore, we also support Signature Authentication to protect instructions from being tampered with.

Transloadit is a service for companies with developers, aimed at handling large amounts of media. While we are happy to help you integrate Transloadit into automated systems, we cannot provide support for visitors wanting to do one-off media conversions.