Skip to content

Instantly share code, notes, and snippets.

@yozef
Last active February 28, 2017 15:39
Show Gist options
  • Save yozef/5841069 to your computer and use it in GitHub Desktop.
Save yozef/5841069 to your computer and use it in GitHub Desktop.
Appcelerator Titanium code for uploading image to Cloudinary + fetching transparency version. rawImage is the TiBlob Object.
// Register for an Account ☞ http://cloudinary.com/invites/lpov9zyyucivvxsnalc5/n8b98rtyuredob4geg2z
// replace these
var cloudinaryCloudName = 'Your_Cloud_Name';
var cloudinaryAPIKey = 999999999999;
var cloudinaryAPISecret = 'XXXXXXXXXX-XXXXX-XX';
// call this function & pass in TiBlob Object
function cloudImageClean(rawImage) {
var nowUnix = moment().unix(); // requires momentjs
var signature = Ti.Utils.sha1('timestamp=' + nowUnix + cloudinaryAPISecret);
var url = 'http://api.cloudinary.com/v1_1/' + cloudinaryCloudName + '/image/upload';
var client = Ti.Network.createHTTPClient({
onload : function(e) {
Ti.API.info("Received Obj: " + this.responseText);
var response = JSON.parse(this.responseText);
var transparentImg= 'http://res.cloudinary.com/' + cloudinaryCloudName + '/image/upload/c_scale,e_make_transparent:30,r_40,w_447/' + response.public_id + '.png';
// Tadaaa! Do what you wish with the transparentImg
},
onerror : function(e) {
Ti.API.info(JSON.stringify(e));
},
timeout:4000 // in ms
});
client.setRequestHeader("enctype", "multipart/form-data");
client.setRequestHeader("Content-Type", "image/png");
client.open("POST", url);
var params = {
api_key:cloudinaryAPIKey,
file: rawImage,
//public_id: someUniqueName, // if added, make sure the signature sha1 starts with:'public_id=' + someUniqueName + '&timestamp=...'
signature:signature,
timestamp:nowUnix,
};
client.send(params);
};
@kjschulz
Copy link

kjschulz commented May 10, 2016

Very helpful, thanks!

In the interest of making it a little more objective, adding another method, and reusing in several of my own Titanium projects (and hopefully for others), I forked your Gist and then pushed my changes to the following repo if you are interested: https://github.com/kjschulz/TiCloudinary. I made sure to attribute your work and link back here. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment