简体   繁体   English

基于像素尺寸的Photoshop脚本调整图像大小(脚本现在无法正常工作)

[英]Photoshop Script Resize Image Based on Pixel Dimensions (script was working now not)

This script is part of a larger Photoshop action we use to process our product shots after they have been edited in Photoshop. 此脚本是较大的Photoshop操作的一部分,在Photoshop中编辑产品拍摄后,我们将使用该脚本来处理它们。 I recently discovered this part of the action (a script) had stopped working properly. 我最近发现操作的这一部分(脚本)已停止正常工作。 It is supposed to take any image who's longest side is larger than 2400px and reduce it to 2400px, ignoring any image who's longest side is not larger than 2400px. 假定将最长边大于2400px的任何图像减小到2400px,而忽略最长边不大于2400px的任何图像。 But now it is running regardless of the images size which is blowing up smaller images and in the process trashing them. 但是现在无论图像大小如何,它都在运行,这会炸毁较小的图像并在处理过程中将其丢弃。

We recently upgraded our Mac clients to OS 10.11 from 10.8 and during that process had to run a couple a Photoshop update as well. 我们最近将Mac客户端从10.8升级到了OS 10.11,在此过程中还必须运行几个Photoshop更新。 I would assume that one or both of these factor in. We are running Photoshop CS6 and that did not change. 我会假定其中一个或两个因素都起作用。我们正在运行Photoshop CS6,但并没有改变。

#target photoshop
// 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 = 2400;
var fHeight = 2400;

// resize. if height > width (portrait-mode) AND height is larger than 2400px -- resize based on height. otherwise, resize based on width only if width is greater than 2400px
if(doc.height > 2400,"px" || doc.width > 2400,"px") {
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
    }
else {
    doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
    }
}'

Any help with this would be appreciated. 任何帮助,将不胜感激。

Found a solution. 找到了解决方案。 Thanks to Mark Dee's comment (thanks Mark) on the syntax of the initial argument I decided that simplifying the syntax would be best. 感谢Mark Dee对初始参数的语法的评论(感谢Mark),我决定简化语法是最好的。

The issue I ran into is that when giving PS a number it sets the units by the current unit of your ruler. 我遇到的问题是,给PS编号时,它将按标尺的当前单位设置单位。 So if the user's ruler is not set to the right unit, the script won't work. 因此,如果用户的标尺未设置为正确的单位,则该脚本将无法工作。 Here is my fix: 这是我的解决方法:

#target photoshop
// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument; 
// setting the ruler unit to pixels
app.preferences.rulerUnits = Units.PIXELS;

// these are our values for the end result width and height (in pixels) of our image
var fWidth = 2400;
var fHeight = 2400;

// resize. if height > width (portrait-mode) AND height is larger than 2400px -- resize based on height. otherwise, resize based on width only if width is greater than 2400px
if(doc.height > 2400 || doc.width > 2400) {
if (doc.height > doc.width) {
    doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
    }
else {
    doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
    }
}
// setting the ruler unit back to inches
app.preferences.rulerUnits = Units.INCHES;

The only issue I have with it now is that it manually sets the units back to inches, I would prefer if it just put it back to "what it was previous" but I am not sure how to do that and will have to invesitgate further. 我现在遇到的唯一问题是,它手动将单位设置为英寸,我希望将其放回“以前的状态”,但是我不确定如何做到这一点,因此必须进一步研究。

You can get and store the current rulerUnits setting in a variable which you can use to test against (and change to) the ruleUnits you need for your script and. 您可以获取当前的rulerUnits设置并将其存储在一个变量中,该变量可用于测试(和更改为)脚本和所需的ruleUnits。 After the script is finished you can use that same variable to restore the rulerUnits setting back to the way it was. 脚本完成后,您可以使用相同的变量将rulerUnits设置恢复为原来的状态。

    // store current unit value
    var userPrefUnits = app.preferences.rulerUnits;

    // change to pixels if not pixels
    if(userPrefUnits !== Units.PIXELS){
       app.preferences.rulerUnits = Units.PIXELS;
    }

    // code based on pixels

    // reset preference if userPrefUnits was not pixels
    if(userPrefUnits !== Units.PIXELS){
       app.preferences.rulerUnits = userPrefUnits;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM