How can I allow only certain file formats for uploads?

Cloudinary supports file type restriction on the server side for uploading.
Here are some examples in Python:

uploader.upload("tests/logo.png", allowed_formats = ['png'])

The next example will return an error:

uploader.upload("tests/logo.png", allowed_formats = ['jpg'])

However, you can convert the uploaded file to match the allowed format, for example:

uploader.upload("tests/logo.png", allowed_formats = ['jpg'], format= 'jpg')


You can use our jQuery library to validate file format, size and dimensions on the client-side.
For more information:
https://github.com/cloudinary/cloudinary_js#client-side-image-resizing-before-upload

Have more questions? Submit a request

Comments

  • Avatar
    David Wood

    So if I understand this correctly, this line will result in the file which is stored on Cloudinary being converted into a JPEG from a PNG. No PNG file will exist in Cloudinary, only a JPEG file.

    uploader.upload("tests/logo.png", allowed_formats = ['jpg'], format= 'jpg')

     

    When building the URL to display that file, if I use the generated public ID, then there is no file extension given by default. 

    Image files load fine when I don't give an extension but I feel a bit twitchy not providing an extension to the browser.

    Is it always fine to leave the extension off or will some browsers choke (does IE cope for example?) and therefore is it best to always add an extension on the end of the type you want to download and render? (I know it's possible to upload a JPEG and then download using a .gif extension to have conversions done on download).

  • Avatar
    Itay Taragano

    When omitting the extension part, Cloudinary delivers the image as the original image's format. When the extension part of URL is added (e.g., .jpg ), Cloudinary explicitly delivers the image as the given format (e.g., Cloudinary converts all other image formats to JPG on delivery).

    Certain very old viewers (e.g., browsers) might require the correct extension, but currently you don't really need the file extension (but it's a nice convention to have this).

  • Avatar
    David Wood

    Thank you Itay - excellent answer as usual! :)

    So if I use the above "format" argument, set to jpeg, but actually upload a gif, and then I do not specify a file extension, clients will actually download a gif because there was a never a JPEG file saved.

    I think I'll specify an extension since if anyone saves the image, both Windows and OSX use the extension to determine the file type.

     

  • Avatar
    Itay Taragano

    Hi David,

    Please note that it if you set the format parameter while uploading (as an incoming transformation), e.g.,:

    uploader.upload("my_image.gif", allowed_formats = ['jpg'], format= 'jpg')
    

    Cloudinary takes the original GIF image, converts it to JPG and only then stores the image (the JPG one) in your account, the GIF won't be stored at all.

    Therefore, delivering this stored Cloudinary image with no extension will actually result a JPG.

    I hope that makes sense.

  • Avatar
    David Wood

    Yes that makes perfect sense - my example was muddled and wasn't what I meant to be saying at all! Thanks again Itay!

Powered by Zendesk