Skip to main content
lesnicole
Inspiring
April 30, 2011
Question

Resize image to target document

  • April 30, 2011
  • 3 replies
  • 2140 views

I want people to be able to automate re-sizing my textures to their document size.

I created an action that does this with the texture file active:

It brings up the image size dialog box. I instruct the person with a stop to select their target document from the windows menu so that the image size is resized to that document.

This does work - it only requires that the user do that step. I'm trying to decide if that is sufficient. It works for me, but I know that users with little experience can get confused easily and I'm seeing other texture makers who have very swank scripts/actions that automatically place and size the texture. Not sure if I want to go that far, but I want to make sure I'm doing the most appropriate thing I can.

Any advice?

Thank you, kindly.

This topic has been closed for replies.

3 replies

lesnicole
lesnicoleAuthor
Inspiring
May 4, 2011

Oh, a further question.

I'm assuming that what I want to do the choices are either use the action that I created, where you are prompted to select the target document to resize to / and then also prompted to choose the target document to move the texture to.

OR

A script.

In otherwords, there is no other way to do this with an action.

JJMack
Community Expert
Community Expert
May 4, 2011

Action can use script and scripts can use actions.  So you can create a single tool that uses both script and actions. Actions are easier to create then scripts for that reasion I have create some scripts to be used within action to enhance what actions can do by adding a little logic through the use of scripts. My crafting action package contains a dozen scripts to enhance actions with.

http://www.mouseprints.net/old/dpr/JJMacksCraftingActions.zip

Contains:

Action Actions Palette Tips.txt

Action Creation Guidelines.txt

Action Dealing with Image Size.txt

Action Enhanced via Scripted Photoshop Functions.txt

CraftedActions.atn Sample Action set includes an example Watermarking action http://www.mouseprints.net/old/dpr/WM900x600.jpg

Sample Actions.txt Photoshop CraftedActions set saved as a text file. This file has some additional comments I inserted describing how the actions work.

12 Scripts for actions

JJMack
Paul Riggott
Inspiring
May 2, 2011

There is a script to place and size a texture here..

http://www.scriptsrus.talktalk.net

You will be able to amend/customise it to suit your need.

lesnicole
lesnicoleAuthor
Inspiring
May 4, 2011

That's a really cool script. Thank you!

Not to look a gift horse in the mouth, but it would be nice if it gave the option to rotate the texture rather than squishing it to the dimension.

My wish list: that it could work with Bridge so it's easier to see the texture you want to select.

Also, that it could work on an open texture. Sometimes I have them run an action to prep an overlay.

Paul Riggott
Inspiring
May 4, 2011

I have now modified the script so that you can select from Bridge/Use an open document or select a file.

JJMack
Community Expert
Community Expert
April 30, 2011

It can be done without any user interaction using a Photoshop script. You get the document canvas size width and hiegh. Place in your new texture background scale it to100% to get its width and height, then calulate the size you need and transform the placed texturred background to cover the canvas. Transform the placed layer and align the transformed layer the the horizontal and vertical center of the canvas.  I just hack at scripting don't actally know java do a lot of cot and paste and ise the ScriptListener to much. I hack one for you it does have one promt to select you new background texture file.

/* ========================================================== // 2010  John J. McAssey (JJMack) // ======================================================= */ // This script is supplied as is. It is provided as freeware. // The author accepts no liability for any problems arising from its use. /* Help Category note tag menu can be used to place script in automate menu <javascriptresource> <about>$$$/JavaScripts/NewBackground/About=JJMack's NewBackground .^r^rCopyright 2011 Mouseprints.^r^rScript^rNext Line!</about> <category>JJMack's Script</category> </javascriptresource> */ // enable double-clicking from Mac Finder or Windows Explorer #target photoshop // this command only works in Photoshop CS2 and higher // bring application forward for double-click events app.bringToFront(); // ensure at least one document open if (!documents.length) alert('There are no documents open.', 'No Document'); else {      // declare Global variables      main(); // at least one document exists proceed } /////////////////////////////////////////////////////////////////////////////// //                            main function                                  // /////////////////////////////////////////////////////////////////////////////// function main() {      try {           // declare local variables           var orig_ruler_units = app.preferences.rulerUnits;           app.preferences.rulerUnits = Units.PIXELS;     // Set the ruler units to PIXELS                 code();                 app.preferences.rulerUnits = orig_ruler_units;     // Reset units to original settings      }      // display error message if something goes wrong      catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); } } /////////////////////////////////////////////////////////////////////////////// //                           main function end                               // /////////////////////////////////////////////////////////////////////////////// function code() {                               CheckVersion();      //Prompt for background      var dir =  Folder("~/My Documents/My Pictures");      NewBackground = dir.openDlg("Select New Background Image" , "Select:*.nef;*.cr2;*.crw;*.dcs;*.raf;*.arw;*.orf;*.dng;*.psd;*.tif;*.jpg;*.png;*.bmp");      var layers = activeDocument.layers;      activeDocument.activeLayer = layers[layers.length-1];               // Target Bottom Layer      activeDocument.activeLayer.isBackgroundLayer=0;                    // Make it a normal Layer      placeImage(NewBackground);           var LB = activeDocument.activeLayer.bounds;      var LWidth = (LB[2].value) - (LB[0].value);      var LHeight = (LB[3].value) - (LB[1].value);      //alert("bounds = " + LB + "\nLWidth = " + LWidth + " LHeight = " + LHeight );      // Get Canvas size      var SWidth = app.activeDocument.width;                         // Area width      var SHeight = app.activeDocument.height;                    // Area height      // Resize Smart Object Layer by some pertentage to Fill the Canvas      if ( (LWidth >= SWidth) && (LHeight >= SHeight) ) {               // down size           // Both sides are larger test if Aspect Ratios are compatible           if ( ((SWidth <= SHeight ) &&  (LWidth <= LHeight )) || ((SHeight <= SWidth ) &&  (LHeight <= LWidth )) ) {                     // Landscape to Landscape or Portrait to Pottrait fit smaler side                if (SWidth >= SHeight) {                     // Landscape fit Height                     var HpercentageChange = ((SHeight/LHeight)*100)                     var NWidth = LWidth * HpercentageChange / 100 ;     // % of Width is new Width                     if ( NWidth >= SWidth ) { var percentageChange = HpercentageChange ; } // % OK                     else { var percentageChange = ((SWidth/LWidth)*100);}                }                else {                                   // Portrait fit width                     var WpercentageChange = ((SWidth/LWidth)*100);                     var NHeight = LHeight * WpercentageChange / 100 ;     // % of hight is new height                     if ( NHeight >= SHeight ) { var percentageChange = WpercentageChange ; } // % OK                     else { var percentageChange = ((SHeight/LHeight)*100); }                }           }           else {                                        // aspect Ratio change                if (SWidth >= SHeight) {                     // Landscape fit Portrait width to landscape width                     var percentageChange = ((SWidth/LWidth)*100);                }                else {                                   // Portrait fit Landscape height to Portrait height                     var percentageChange = ((SHeight/LHeight)*100);                }           }      }      else {                                             // up size           if ( LWidth <= SWidth && LHeight <= SHeight ) {               // Both image sides need to be increased in size                if ( SWidth <= SHeight ) {                     //test if fitting image width will work                     var WpercentageChange = ((SWidth/LWidth)*100);     //calc % fit image width to canvas width                     var NHeight = LHeight * WpercentageChange / 100 ;     // % of hight is new height                     if ( NHeight >= SHeight ) { var percentageChange = WpercentageChange ; } // % OK                     else { var percentageChange = ((SHeight/LHeight)*100); }                }                else {                     var HpercentageChange = ((SHeight/LHeight)*100)                     var NWidth = LWidth * HpercentageChange / 100 ;     // % of Width is new Width                     if ( NWidth >= SWidth ) { var percentageChange = HpercentageChange ; } // % OK                     else { var percentageChange = ((SWidth/LWidth)*100); }                }           }           else {                                        // only one side needs to be increassed in side                if ( SWidth <= LWidth ) {                     var HpercentageChange = ((SHeight/LHeight)*100)                     var NWidth = LWidth * HpercentageChange / 100 ;     // % of Width is new Width                     if ( NWidth >= SWidth ) { var percentageChange = HpercentageChange ; } // % OK                     else { var percentageChange = ((SWidth/LWidth)*100); }                }                                    // then fit the image Hieght                else {                     var WpercentageChange = ((SWidth/LWidth)*100);                     var NHeight = LHeight * WpercentageChange / 100 ;     // % of hight is new height                     if ( NHeight >= SHeight ) { var percentageChange = WpercentageChange ; } // % OK                     else { var percentageChange = ((SHeight/LHeight)*100); }                }           }      }      var NWidth = LWidth * percentageChange / 100 ;     // % of Width is new Width      var NHeight = LHeight * percentageChange / 100 ;     // % of hight is new height      // Transform Smart Object Layer      var userResampleMethod = app.preferences.interpolation;      app.preferences.interpolation = ResampleMethod.BILINEAR;     // resample interpolation biliner      activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);      app.preferences.interpolation = userResampleMethod;      SelectAll();      align('AdCV');      align('AdCH');      activeDocument.selection.deselect();      activeDocument.activeLayer.rasterize;      var nlayers = app.activeDocument.layers;      activeDocument.activeLayer.move(nlayers[nlayers.length-1], ElementPlacement.PLACEAFTER);      activeDocument.activeLayer.isBackgroundLayer=1 } ////////////////////////////////////////////////////////////////////////////////// //                    The end                              // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// //               Helper Functions                         // ////////////////////////////////////////////////////////////////////////////////// function SelectAll() {      var doc = activeDocument;      var frameRef = [      [0,0],      [0,app.activeDocument.height],      [app.activeDocument.width,app.activeDocument.height],      [app.activeDocument.width,0],      ]      doc.selection.select(frameRef); } function placeImage(file) {      // =======avoid bug in cs2 and maybe CS4 ==================================      var idslct = charIDToTypeID( "slct" );          var desc5 = new ActionDescriptor();          var idnull = charIDToTypeID( "null" );              var ref3 = new ActionReference();              var idChnl = charIDToTypeID( "Chnl" );              var idChnl = charIDToTypeID( "Chnl" );              var idRGB = charIDToTypeID( "RGB " );              ref3.putEnumerated( idChnl, idChnl, idRGB );          desc5.putReference( idnull, ref3 );          var idMkVs = charIDToTypeID( "MkVs" );          desc5.putBoolean( idMkVs, false );      executeAction( idslct, desc5, DialogModes.NO );      // Place in the file      var idPlc = charIDToTypeID( "Plc " );          var desc5 = new ActionDescriptor();          var idnull = charIDToTypeID( "null" );          desc5.putPath( idnull, new File( file ) );          var idFTcs = charIDToTypeID( "FTcs" );          var idQCSt = charIDToTypeID( "QCSt" );          var idQcsa = charIDToTypeID( "Qcsa" );          desc5.putEnumerated( idFTcs, idQCSt, idQcsa );          var idOfst = charIDToTypeID( "Ofst" );              var desc6 = new ActionDescriptor();              var idHrzn = charIDToTypeID( "Hrzn" );              var idPxl = charIDToTypeID( "#Pxl" );              desc6.putUnitDouble( idHrzn, idPxl, 0.000000 );              var idVrtc = charIDToTypeID( "Vrtc" );              var idPxl = charIDToTypeID( "#Pxl" );              desc6.putUnitDouble( idVrtc, idPxl, 0.000000 );          var idOfst = charIDToTypeID( "Ofst" );          desc5.putObject( idOfst, idOfst, desc6 );      executeAction( idPlc, desc5, DialogModes.NO );      // because can't get the scale of a smart object, reset to 100%      activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);      return app.activeDocument.activeLayer; } function align(method) {      var desc = new ActionDescriptor();      var ref = new ActionReference();      ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );      desc.putReference( charIDToTypeID( "null" ), ref );      desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );      try{           executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );      }catch(e){} } // CheckVersion function CheckVersion() {      var numberArray = version.split(".");      if ( numberArray[0] < 9 ) {           alert( "You must use Photoshop CS2 or later to run this script!" );           throw( "You must use Photoshop CS2 or later to run this script!" );      } }

JJMack
lesnicole
lesnicoleAuthor
Inspiring
May 4, 2011

JJMack,

Forgive me for not getting back to thanking you sooner. Suddenly got really busy.

OK, I figured out how to load this, ran it - but it's not resizing the texture, it's cropping it. also it's placing the texture as the background - which I realize would seem to make sense, but normally, textures are placed above the image.

Thank you for your generous offering of the script and your time.

I think it would be fun to figure all of this out, but I'm so busy, I'm realizing It will be best to hire / trade with someone to get exactly what I want. I have learned a little bit just by poking around and I'll continue to slowly try to learn this stuff better.

JJMack
Community Expert
Community Expert
May 4, 2011

Actually it does resize the  added texture image.  If the texture image does not have the same aspect ratio as your image the resized texture layer excess will be croped off.  You must crop or distort the texture to fit your image if their aspect ratios are different there is no way around that.  If you do not want cropping or distortion you need to use a seamless texture pattern. You would add a new empty layer select all and fill it with your seamless texture pattern.

Its also very easy to change the script to not convert any old background layer to a normal layer. not to rastersize the added resized layer and leave it as a smart object. To not move it to the bottom of the stack and convert it to a background layer.  The texture layer would be placed above the current targeted layer and resized to fill the canvas any excess will efectively be masked off by canvas size.

Use can use Paul's script it will rotate texture to match image orintation landscape or portrait if they don't match and then the texture will be distorted to cover the canvas if their aspect ratios are not the same.

JJMack