Skip to main content
Participating Frequently
July 17, 2017
Question

Scale image by 1.5x if under certain width/height

  • July 17, 2017
  • 2 replies
  • 1255 views

Hi all,

I am currently writing a script whereby I resize the canvas to certain resolutions, only issue is if the user uses a logo too small I would like to automatically resize this to ensure that it is big enough?

For example, if it is under 200px it could be scaled to be 2x as big?

Below is a brief description of what I have done already.

// Prompt user to select NoImages file. Clicking "Cancel" returns null.

    var NoImages = File.openDialog("Select your logo file", false);

    if (NoImages !== null) {

        var doc = open(NoImages, OpenDocumentType.PNG.JPEG); // Open PNG file

       

        if (doc == null) {

          throw "Something is wrong with the file.";

        }

        var startState = doc.activeHistoryState;       // save for undo

        var initialPrefs = app.preferences.rulerUnits; // will restore at end

        app.preferences.rulerUnits = Units.PIXELS;     // use pixels

   

        // Folder selection dialog

        var destFolder = Folder.selectDialog( "Choose an output folder");

        if (destFolder == null) {

          // User canceled, just exit

          throw "";

        }

        // Save icons in PNG using Save for Web.

        var sfw = new ExportOptionsSaveForWeb();

        sfw.format = SaveDocumentType.PNG;

        sfw.PNG8 = false; // use PNG-24

        sfw.transparency = true;

        doc.info = null;  // delete metadata

       

       var no_images = [

          {"name": "no-image-1-1", "width":1170, "height":1170},

          {"name": "no-image-2-3", "width":779, "height":1170},

          {"name": "no-image-3-2", "width":1170, "height":779},

          {"name": "no-image-3-4", "width":879, "height":1170},

          {"name": "no-image-4-3", "width":1170, "height":879},

          {"name": "no-image-7-2", "width":1170, "height":334},

          {"name": "no-image-9-3", "width":1170, "height":391},

          {"name": "no-image-11-5", "width":1170, "height":532},

          {"name": "no-image-16-9", "width":1170, "height":658}

        ];

        var no_image;

        for (i = 0; i < no_images.length; i++)  {

            no_image = no_images;

            doc.resizeCanvas(no_image.width, no_image.height, // width, height

            null, ResampleMethod.BICUBICSHARPER);

            createFill();

            var destFileName = no_image.name + ".png";

            doc.exportDocument(new File(destFolder + "/" + destFileName), ExportType.SAVEFORWEB, sfw);

            doc.activeHistoryState = startState; // undo resize

        }

    alert("No Images created!");

    }

This topic has been closed for replies.

2 replies

c.pfaffenbichler
Community Expert
Community Expert
July 18, 2017

In the title you mention »1.5x«, in the post »2x« – I assume you can adapt the code to whichever you need in this case.

Participating Frequently
July 19, 2017

Hi thanks for the response! I have included part of your code into my function and I am getting the error "Invalid enumeration value"

       var no_images = [

          {"name": "no-image-1-1", "width":1170, "height":1170},

          {"name": "no-image-2-3", "width":779, "height":1170},

          {"name": "no-image-3-2", "width":1170, "height":779},

          {"name": "no-image-3-4", "width":879, "height":1170},

          {"name": "no-image-4-3", "width":1170, "height":879},

          {"name": "no-image-7-2", "width":1170, "height":334},

          {"name": "no-image-9-3", "width":1170, "height":391},

          {"name": "no-image-11-5", "width":1170, "height":532},

          {"name": "no-image-16-9", "width":1170, "height":658}

        ];

        var no_image;

        for (i = 0; i < no_images.length; i++)  {

            no_image = no_images;

            var myDocument = app.activeDocument;

            if (myDocument.width < 300 && myDocument.height < 300) {

               myDocument.resizeImage(new UnitValue (200, "%"), new UnitValue (200, "%"), myDocument.resolution, ResampleMethod.BICUBICAUTOMATIC);

            };

            doc.resizeCanvas(no_image.width, no_image.height, // width, height

            null, ResampleMethod.BICUBICSHARPER);

            createFill();

            var destFileName = no_image.name + ".png";

            doc.exportDocument(new File(destFolder + "/" + destFileName), ExportType.SAVEFORWEB, sfw);

            doc.activeHistoryState = startState; // undo resize

        }

c.pfaffenbichler
Community Expert
Community Expert
July 20, 2017

I have got it working now!

It is was the ResampleMethod not being correct so I changed that to just be BICUBIC and is working a dream. Thank you for your help with this though!


It is was the ResampleMethod not being correct so I changed that to just be BICUBIC and is working a dream.

What is your Photoshop version?

’cause Bicubic Automatic has been introduced a while back already.

c.pfaffenbichler
Community Expert
Community Expert
July 18, 2017

// 2017, use it at your own risk;

#target photoshop

if (documents.length > 0) {

var myDocument = app.activeDocument;

var originalRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

if (myDocument.width < 200 && myDocument.height < 200) {

myDocument.resizeImage(new UnitValue (200, "%"), new UnitValue (200, "%"), myDocument.resolution, ResampleMethod.BICUBICAUTOMATIC)

};

app.preferences.rulerUnits = originalRulerUnits;

};