Transformation

carkeys
carkeys Member Posts: 2
edited February 20 in Developer APIs

Hi All!


I Ran this command Transformation all my images, it's says all done but i dont see the changes, and the updated image return with the original info.


what's wrong?


const result = await cloudinary.api.resources({ type: 'upload', max_results: 500 });

for (const image of result.resources) {

const { public_id, format } = image;


try {

const updatedImage = await cloudinary.api.update(public_id, {

type: 'upload',

resource_type: 'image',

transformation: [

{ width: 1000, crop: 'scale' },

{ quality: 'auto', fetch_format: 'auto' }

],

overwrite: true,

});


} catch (error) {

console.log(error);

}

}

Tagged:

Answers

  • Zachary
    Zachary Member, Cloudinary Staff Posts: 25

    Hey there,

    Thanks for writing in. It looks like you are trying to use the results of result before you've actually received any information. If you console log image, I think you'll find that it returns undefined.

    You might try something more like:

    cloudinary.api.resources({ type: 'upload', max_results: 500 })
      .then(result => {
        // Use the 'result' variable here for further processing or logic
        console.log(result);
        // You can continue your code logic here
      })
      .catch(error => console.error(error));
    
  • carkeys
    carkeys Member Posts: 2

    async function resizeImages() {

    try {


    const result = await cloudinary.api.resources({ type: 'upload', max_results: 700 });

    for (const image of result.resources) {

    const { public_id, format } = image;


    try {

    await cloudinary.uploader.explicit(public_id, {

    type: 'upload',

    resource_type: 'image',

    transformation: [

    { width: 1000, crop: 'scale' },

    { quality: 'auto', fetch_format: 'auto' }

    ],

    overwrite: true,

    });

    console.log(public_id);


    } catch (error) {

    console.log(error);

    }

    }


    console.log('All images resized successfully.');

    } catch (error) {

    console.error('Error resizing images:', error);

    }

    }


    Full code

  • atdatu
    atdatu Member, Cloudinary Staff Posts: 9

    Hi @carkeys ,

    Just want to clarify if your end goal here is to get the URL with the transformation w_1000,c_scale ? If this is the case, then you can use the url method to generate the transformation on-the-fly:

    const cloudinary = require('cloudinary').v2;
    cloudinary.url('<public_id>',{transformation: [{width: 1000, crop: "scale"}], secure: true})
    //output
    //https://res.cloudinary.com/<cloudname>/image/upload/w_1000,c_scale/<public_id>