Search code examples
photoshop-script

Canvas resize photoshop script


Would it be possible to write a script to resize each image to the closest round number (for example if the original image is 791x1265px then it could be resized to 800x1300px)

Thanks!


Solution

  • Pretty Easy and small script can do it :) Enjoy

    Note : You have two choices for script; before running script either use static base value (default) or if you want to add prompt on each run then uncomment below line of var base and comment the var base line :) Hope that is what you were looking :)

        //get Original Ruler Units;
    var origRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;    
    
        //get Active document scales
    var origWidth = app.activeDocument.width;
    var origHeight = app.activeDocument.height;
    
        //define base
    var base = 100; //change your base like 10;100 etc; use below code to make a prompt on each run;
    //var base = prompt("Enter Your Base number",""); //use this code if you want prompt for each run . uncomment by rermoving first two "//"
    
        //magical Mathematics XD
    var roundWidth = Math.ceil(origWidth / base) * base;
    var roundHeight = Math.ceil(origHeight / base) * base;
    
        //resize canvas
    app.activeDocument.resizeCanvas (roundWidth, roundHeight);
    
        //Restores Original Ruler Units;
    app.preferences.rulerUnits = origRuler;    
    

    Edit: Updated script to avoid ruler units conflicts and changed Math.round to Math.ceil as per @Sergey Suggestion!