Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with Fit Image with unit of measure in centimeter

Contributor ,
Aug 31, 2018 Aug 31, 2018

Hello everyone! The title says it all.

How do I program in this script the unit of measure for centimeters in text boxes so that it preserves the resolution of the original of the image and also keeping the aspect ratio if it assigns a value in width or height.

#target photoshop

var d = new Window("dialog", "Fit Image CM", {x:0, y:0, width:260, height:105} ,{closeButton: true});

    ctd_gp  = d.add("group", {x:5, y:-10, width:260, height:105});

    edt_w  = ctd_gp.add("edittext", {x:53, y:25, width:50, height:10}, " 25,5"); // Set a size W

    txt_w   = ctd_gp.add("statictext", {x:10, y:30, width:120, height:10}, " Width:                     cm");

    edt_h  = ctd_gp.add("edittext", {x:53, y:55, width:50, height:10}, " 15"); // Set a size H

    txt_h   = ctd_gp.add("statictext", {x:10, y:60, width:120, height:10}, " Height:                    cm");

    fit_bt = ctd_gp.add("button", {x:150, y:25, width:93,height:25}, "Fit", { name: "ok"});

     fit_bt .onClick = function() {

     d.close();

    }

d.center();

d.show();

TOPICS
Actions and scripting
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Aug 31, 2018 Aug 31, 2018

test it

fit_bt.onClick = function()

    {

    try

        {

        if (!app.documents.length) return;

        var fit_w = Number(edt_w.text.replace(/,/g, "."));

        if (fit_w <= 0 || isNaN(fit_w)) { alert("wrong W"); return; }

        var fit_h = Number(edt_h.text.replace(/,/g, "."));

        if (fit_h <= 0 || isNaN(fit_h)) { alert("wrong H"); return; }

        var old_units = app.preferences.rulerUnits;

        app.preferences.rulerUnits = Units.CM;

        var w = Number(activeDocument.width.value);

...
Translate
Adobe
LEGEND ,
Aug 31, 2018 Aug 31, 2018

I'm sleepy so I don't know whether you want somethng like UnvitValue(10, 'cm') when for example rulers are set to pixels?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 31, 2018 Aug 31, 2018

Here in my area it is not common to work with pixels.

It would be the same as this script however, I need the unit of measure to be in centimeters instead of pixels here in my script.

Screenshot_1.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Aug 31, 2018 Aug 31, 2018

test it

fit_bt.onClick = function()

    {

    try

        {

        if (!app.documents.length) return;

        var fit_w = Number(edt_w.text.replace(/,/g, "."));

        if (fit_w <= 0 || isNaN(fit_w)) { alert("wrong W"); return; }

        var fit_h = Number(edt_h.text.replace(/,/g, "."));

        if (fit_h <= 0 || isNaN(fit_h)) { alert("wrong H"); return; }

        var old_units = app.preferences.rulerUnits;

        app.preferences.rulerUnits = Units.CM;

        var w = Number(activeDocument.width.value);       

        var h = Number(activeDocument.height.value);       

        var w1;

        var h1;

        if (w/h > fit_w/fit_h)

            {

            w1 = fit_w;

            }

        else

            {

            h1 = fit_h;

            }

        d.close();

        activeDocument.resizeImage(w1, h1);

        w = Number(activeDocument.width.value);       

        h = Number(activeDocument.height.value);       

        w = Math.round(w*10)/10;

        h = Math.round(h*10)/10;

        app.preferences.rulerUnits = old_units;

        if (w > fit_w || h > fit_h) { alert("Fit Error: " + w + "  " + fit_w + "   " + h + "  " + fit_h); }

        }

    catch (e) { alert(e); }

    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 31, 2018 Aug 31, 2018

Fantastic! Nice work @r-bin This will greatly improve my workflow.

Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 31, 2018 Aug 31, 2018

I tested it and it worked very well, except when I add a higher value, I see that it did not change the size, was it your choice?

I added these 3 line of code before the function fit_bt.onClick = function () to have the current size of the document.

var doc = activeDocument;

app.preferences.rulerUnits = Units.CM;

edt_w.text =(doc.width.value.toFixed(2));

edt_h.text =(doc.height.value.toFixed(2));

If possible, could you correct this bug?

Thank you so much again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 01, 2018 Sep 01, 2018

Resize is done in any cases, for large and small values. And what do you want?

You can do a check for the fact that the size is already fitted and do nothing.

#target photoshop

var d = new Window("dialog", "Fit Image CM", {x:0, y:0, width:260, height:105} ,{closeButton: true});

ctd_gp  = d.add("group", {x:5, y:-10, width:260, height:105});

edt_w  = ctd_gp.add("edittext", {x:53, y:25, width:50, height:10}, " 25,5"); // Set a size W

txt_w   = ctd_gp.add("statictext", {x:10, y:30, width:120, height:10}, " Width:                     cm");

edt_h  = ctd_gp.add("edittext", {x:53, y:55, width:50, height:10}, " 15"); // Set a size H

txt_h   = ctd_gp.add("statictext", {x:10, y:60, width:120, height:10}, " Height:                    cm");

fit_bt = ctd_gp.add("button", {x:150, y:25, width:93,height:25}, "Fit", { name: "ok"});

// your new code //////////

var doc = activeDocument;

app.preferences.rulerUnits = Units.CM;

edt_w.text =(doc.width.value.toFixed(2));

edt_h.text =(doc.height.value.toFixed(2));

//////////////////////////

fit_bt.onClick = function() 

    { 

    try  

        { 

        if (!app.documents.length) return; 

 

        var fit_w = Number(edt_w.text.replace(/,/g, ".")); 

        if (fit_w <= 0 || isNaN(fit_w)) { alert("wrong W"); return; } 

 

        var fit_h = Number(edt_h.text.replace(/,/g, ".")); 

        if (fit_h <= 0 || isNaN(fit_h)) { alert("wrong H"); return; } 

 

        var old_units = app.preferences.rulerUnits; 

 

        app.preferences.rulerUnits = Units.CM; 

 

        var w = Number(activeDocument.width.value);         

        var h = Number(activeDocument.height.value);         

 

        var w1; 

        var h1; 

 

        if (w/h > fit_w/fit_h) w1 = fit_w; 

        else                   h1 = fit_h; 

 

        d.close(); 

                 

        if ((w1 && Math.round(w*100)/100 != Math.round(fit_w*100)/100) || (h1 && Math.round(h*100)/100 != Math.round(fit_h*100)/100))                                                                           

            activeDocument.resizeImage(w1, h1); 

        else

            alert("Already fitted!", "Warning")

 

        w = Number(activeDocument.width.value);         

        h = Number(activeDocument.height.value);         

 

        w = Math.round(w*100)/100; 

        h = Math.round(h*100)/100; 

 

        app.preferences.rulerUnits = old_units; 

 

        if (w > fit_w || h > fit_h) { alert("Fit to:\t" + fit_w + " x " + fit_h + "\n\nResult:\t" + w + " x " + h, "Fit problem"); } 

        } 

    catch (e) { alert(e); } 

    } 

d.center();

d.show();

What did not work for you I did not understand.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 01, 2018 Sep 01, 2018
LATEST

r-bin  escreveu

Resize is done in any cases, for large and small values. And what do you want?

You can do a check for the fact that the size is already fitted and do nothing.

#target photoshop var d = new Window("dialog", "Fit Image CM", {x:0, y:0, width:260, height:105} ,{closeButton: true});  ctd_gp  = d.add("group", {x:5, y:-10, width:260, height:105}); edt_w  = ctd_gp.add("edittext", {x:53, y:25, width:50, height:10}, " 25,5"); // Set a size W txt_w   = ctd_gp.add("statictext", {x:10, y:30, width:120, height:10}, " Width:                     cm");   edt_h  = ctd_gp.add("edittext", {x:53, y:55, width:50, height:10}, " 15"); // Set a size H txt_h   = ctd_gp.add("statictext", {x:10, y:60, width:120, height:10}, " Height:                    cm"); fit_bt = ctd_gp.add("button", {x:150, y:25, width:93,height:25}, "Fit", { name: "ok"});  // your new code ////////// var doc = activeDocument; app.preferences.rulerUnits = Units.CM; edt_w.text =(doc.width.value.toFixed(2)); edt_h.text =(doc.height.value.toFixed(2)); //////////////////////////  fit_bt.onClick = function()       {       try            {           if (!app.documents.length) return;              var fit_w = Number(edt_w.text.replace(/,/g, "."));           if (fit_w <= 0 || isNaN(fit_w)) { alert("wrong W"); return; }              var fit_h = Number(edt_h.text.replace(/,/g, "."));           if (fit_h <= 0 || isNaN(fit_h)) { alert("wrong H"); return; }              var old_units = app.preferences.rulerUnits;              app.preferences.rulerUnits = Units.CM;              var w = Number(activeDocument.width.value);                   var h = Number(activeDocument.height.value);                      var w1;           var h1;              if (w/h > fit_w/fit_h) w1 = fit_w;           else                   h1 = fit_h;              d.close();                              if ((w1 && Math.round(w*100)/100 != Math.round(fit_w*100)/100) || (h1 && Math.round(h*100)/100 != Math.round(fit_h*100)/100))                                                                                         activeDocument.resizeImage(w1, h1);           else              alert("Already fitted!", "Warning")            w = Number(activeDocument.width.value);                   h = Number(activeDocument.height.value);                      w = Math.round(w*100)/100;           h = Math.round(h*100)/100;              app.preferences.rulerUnits = old_units;              if (w > fit_w || h > fit_h) { alert("Fit to:\t" + fit_w + " x " + fit_h + "\n\nResult:\t" + w + " x " + h, "Fit problem"); }           }       catch (e) { alert(e); }       }    d.center(); d.show(); 

What did not work for you I did not understand.

That's right, I would like to scale for small and large values, nothing more! Thank you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines