Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript resize image in different sizes where only the width matters

I am trying to make a script for Photoshop which resizes an open image in different sizes where only the width matters. The goals is to after every resize it should reinstate the original state of the image and run for a different width. There are some scripts online which nearly do this but I only get errors. The error I get is "undefined is not an object".

I have the following script at the moment but I am stuck:

// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;


// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 940;


// our web export optionsvar options = new ExportOptionsSaveForWeb();

function SaveJPEG(saveFile, quality) {
  var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb();
  exportOptionsSaveForWeb.format = SaveDocumentType.JPEG;
  exportOptionsSaveForWeb.includeProfile = false;
  exportOptionsSaveForWeb.interlaced = false;
  exportOptionsSaveForWeb.optimized = true;
  exportOptionsSaveForWeb.quality = 80;


}


var newName = 'F' + doc.name + '.jpg';

doc.exportDocument(File(doc.path + '/' + newName), ExportType.SAVEFORWEB, options);

doc.activeHistoryState = doc.historyStates.index(0);

The undefined error is stopping the script at the var newName line. I have to add that is for Photoshop CS6.

Any help would be very appreciated thanks.

like image 590
user310777 Avatar asked Dec 06 '15 12:12

user310777


People also ask

How do you scale an image in JavaScript?

// Dynamically create a canvas element var canvas = document. createElement("canvas"); var ctx = canvas. getContext("2d"); // Actual resizing ctx. drawImage(img, 0, 0, 300, 300); // Show resized image in preview element var dataurl = canvas.

How do you proportionally resize an image?

If the Shift key is held down whilst moving the handle, then the proportions of the object will be preserved. For example, if I hold Shift and drag the bottom edge upwards to reduce the size by half, then the right edge will automatically move to the left to reduce the width of the object by the same amount.


1 Answers

If the problem is that it's breaking your script, you should try to use the try..catch statement

...

var newName;

try {
  newName = 'F' + doc.name + '.jpg';
} catch (e) {
  newName = 'F' + 'name-error' + '.jpg';
  console.log('doc.name is not accessible: ' + e.message;);
}

...
like image 71
Elvio Cavalcante Avatar answered Sep 24 '22 14:09

Elvio Cavalcante