Skip to main content
Known Participant
March 10, 2011
Answered

Scaling images with ScriptUI

  • March 10, 2011
  • 2 replies
  • 3026 views

Is it possible to selectively scale an image for display in a window/container with ScriptUI? The best I can get is a cropped image, but what I'm really needing is a thumbnail sized image.

JJ

This topic has been closed for replies.
Correct answer _Jongware_-9BC6tI

Peter Kahrel describes how to do just that in his ScriptUI Beginners' Guide. (He credits Marc Autret for this trick, by the way, but I don't know where to find that particular post.)

2 replies

JJ_FulksAuthor
Known Participant
March 11, 2011

I think I got it. It works after an Image control's size property is explicity set, allowing each image to be individually scaled. Very slick code!

_Jongware_-9BC6tICorrect answer
Inspiring
March 10, 2011

Peter Kahrel describes how to do just that in his ScriptUI Beginners' Guide. (He credits Marc Autret for this trick, by the way, but I don't know where to find that particular post.)

JJ_FulksAuthor
Known Participant
March 10, 2011

Jongware, thanks for the link to Kahrel's ScriptUI guide. In the example he shows, it looks like the Image.onDraw() event callback function is being overwritten by way of prototyping it. I'm still very new to ScriptUI and JS, so my interpretation of Kahrel's/Marc's method may be off. If this is the case, though, then it would scale every Image object that is displayed in a ScriptUI dialog. I'm looking to scale images variably and not to a specific ratio across the board.

Here's Kahrel's function ...

Image.prototype.onDraw = function()
{ // written by Marc Autret
   // "this" is the container; "this.image" is the graphic
   if( !this.image ) return;
   var WH = this.size,
   wh = this.image.size,
   k = Math.min(WH[0]/wh[0], WH[1]/wh[1]),
   xy;
   // Resize proportionally:
   wh = [k*wh[0],k*wh[1]];
   // Center:
   xy = [ (WH[0]-wh[0])/2, (WH[1]-wh[1])/2 ];
   this.graphics.drawImage(this.image,xy[0],xy[1],wh[0],wh[1]);
   WH = wh = xy = null;
}
Marc Autret
Legend
March 11, 2011

JJ_Fulks wrote:

In the example he shows, it looks like the Image.onDraw() event callback function is being overwritten by way of prototyping it. (...) If this is the case, though, then it would scale every Image object that is displayed in a ScriptUI dialog. I'm looking to scale images variably and not to a specific ratio across the board.

You are absolutely right. I sent this code to Peter to illustrate a way to get a common (=prototyped) image rescale routine, but of course you might want to use it only on a specific Image container. Then you have to specificaly write a myImage.onDraw handler. That's exactly the method I use in that script:

http://www.indiscripts.com/post/2010/12/scriptui-challenge-jpeg-links-explorer

[The Adobe documentation is not very explicit about the (fundamental!) difference between the ScriptUI Image and the ScriptUI ScriptUIImage objects. Peter and I have conducted some investigations on that subject. The next release of ScriptUI for Dummies might provide more details and secret tricks...]

@+

Marc