Conditionally change fetch format

Options
rupertxyz
rupertxyz Member Posts: 2

Hi there,

I'm trying to figure out whether it's possible to conditionally change the fetch format. My users are uploading in various formats and currently I'm converting all images to .jpg. IF however, the uploaded file is a PNG, I want this file to stay a PNG in case it has transparent background.

Currently, I'm checking the file type in my backend, but I have to download files first which causes requests to take a bit more time

Does anyone know if I can solve this with Cloudinary natively?

Thank you!

Rupert

Tagged:

Answers

  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 53
    Options

    Hi @rupertxyz,

    You can use our eval feature as part of your upload call (or either via an upload preset) to run this logic which could look like this:

    if (resource_info.format == 'png') { upload_options['format'] = 'png' } else { upload_options['format'] = 'jpg' } 
    

    Hope that helps.

    Best,

    Loic

  • rupertxyz
    rupertxyz Member Posts: 2
    Options

    Thanks, Loic. I think that doesn't solve my issue.

    Users can upload in various formats (pdf, svg, ...), so I'm converting files with fetch_format. My question was whether Cloudinary offers a feature where I can convert conditionally depending on the uploaded format (if it's PNG, I want to keep PNG, in all other cases JPG).

    The eval feature does not convert, correct? If, for instance, the uploaded file is an SVG, the code above would upload the file as a JPG.

    So it seems checking the file type before in my code is the way to go for now, correct?