Skip to main content
vinoth_mannu
Inspiring
October 10, 2012
Answered

Need help to solve if condition

  • October 10, 2012
  • 1 reply
  • 787 views

Hi All,

I need to resize the images to thumbnail veiw the condition is width should not exceed 75 pixels and height should not exceed 50 pixels

app.preferences.rulerUnits = Units.PIXELS     

     var Wid = docRef.width;

     var Hig = docRef.height;

                if (Number(Wid) > 75)

                    {

                         docRef.resizeImage(75,null,);                 

                                           }                                   

                else if(Number(Hig) >= 50)

                    {

                        docRef.resizeImage(null,50,);

                     }

This works fine, but if the images are width 85 and hieght 200 px

The result is (75,176).  which is wrong.

Please help me to solve

regards,

Vinoth

This topic has been closed for replies.
Correct answer c.pfaffenbichler

The result is (75,176).  which is wrong.

It’s what you set it up to do by checking for height only if the width is smaller or equal 75, I guess.

You could try something like this:

// use it at your own risk;

#target photoshop

app.preferences.rulerUnits = Units.PIXELS;

var docRef = app.activeDocument;

var theTargetDiag = 75/ 50;

var Wid = docRef.width;

var Hig = docRef.height;

var theDiag = Wid / Hig;

if (theDiag >= theTargetDiag) {

docRef.resizeImage(new UnitValue (75, "px"),null);                

}                                  

else {

docRef.resizeImage(null, new UnitValue (50, "px"));

};

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
October 10, 2012

The result is (75,176).  which is wrong.

It’s what you set it up to do by checking for height only if the width is smaller or equal 75, I guess.

You could try something like this:

// use it at your own risk;

#target photoshop

app.preferences.rulerUnits = Units.PIXELS;

var docRef = app.activeDocument;

var theTargetDiag = 75/ 50;

var Wid = docRef.width;

var Hig = docRef.height;

var theDiag = Wid / Hig;

if (theDiag >= theTargetDiag) {

docRef.resizeImage(new UnitValue (75, "px"),null);                

}                                  

else {

docRef.resizeImage(null, new UnitValue (50, "px"));

};

vinoth_mannu
Inspiring
October 10, 2012

This works perfect

Thanks for helping, I am still a learner i would think i need more help from you in the future.

Thanks and regards,

Vinoth

JJMack
Community Expert
Community Expert
October 10, 2012

What you were doing is also what Fit image plug-in script does and scripts like the Image processor does to resize images to fit within some pixels area while maintaining the images aspect ratio. Fit an image into some pixel width and into some pixel height. In fact the images processor uses the Fit Image plugin script to do the work. Here is that code from the Image processor script

// use the fit image automation plug-in to do this work for me

function FitImage( inWidth, inHeight ) {

          if ( inWidth == undefined || inHeight == undefined ) {

                    alert( strWidthAndHeight );

                    return;

          }

          var desc = new ActionDescriptor();

          var unitPixels = charIDToTypeID( '#Pxl' );

          desc.putUnitDouble( charIDToTypeID( 'Wdth' ), unitPixels, inWidth );

          desc.putUnitDouble( charIDToTypeID( 'Hght' ), unitPixels, inHeight );

          var runtimeEventID = stringIDToTypeID( "3caa3434-cb67-11d1-bc43-0060b0a13dc4" );

          executeAction( runtimeEventID, desc, DialogModes.NO );

}

JJMack