Hi there
Having some problems with a simple code to import an image. Basically, this script is on a browse button. The user clicks it, finds the image they want and it should then import it into an image viewer. Except, whats happening is that the image is importing into the middle of the stack and not into the image viewer:
on mouseUp
set the cursor to watch
answer file "Please select an image file." with \
type "All Images|jpg,jpeg,gif,png|JPEG,GIFf,PNGf" or \
type "JPEG Images|jgp,jpeg|JPEG" or \
type "GIF Images|gif|GIFf" or \
type "PNG Images|png|PNGf"
if it is not empty then
import paint from file it
set the loc of it to img "image"
put it into img "Image"
else
beep
end if
end mouseUp
Essentially, I need the image to come in and resize itself to the dimensions of the image viewer in ratio. Is there an easy way to do this?
function ScaleObject pOrigW,pOrigH,pMaxW,pMaxH
if pOrigW is 0 and pOrigH is 0 then
answer "Error"
return round(pMaxW),round(pMaxH)
else
put pMaxW/pOrigW into tPctW
put pMaxH/pOrigH into tPctH
IF tPctH < tPctW THEN
IF tPctH < 1 THEN
put pOrigH * tPctH into tNewH
put pOrigW * tPctH into tNewW
ELSE
put pOrigH into tNewH
put pOrigW into tNewW
END if
ELSE
IF tPctW < 1 THEN
put pOrigH * tPctW into tNewH
put pOrigW * tPctW into tNewW
ELSE
put pOrigH into tNewH
put pOrigW into tNewW
END if
END if
return round(tNewW),round(tNewH)
end if
end ScaleObject
on mouseUp
set the cursor to watch
answer file "Please select an image file." with \
type "All Images|jpg,jpeg,gif,png|JPEG,GIFf,PNGf" or \
type "JPEG Images|jgp,jpeg|JPEG" or \
type "GIF Images|gif|GIFf" or \
type "PNG Images|png|PNGf"
if it is not empty then
import paint from file it
put last image into image "image"
else
beep
end if
end mouseUp
If you set the lockLoc (-> properties inspector -> size and position) to true then the imported image will adapt to the proportions/size of image "image". If you do not set the LockLoc of image "image" to true image "image" will take on the size/proportion of the imported image. If you have varying proportions of the imported image then you would have to adjust the size/proportions of the imported image in a script like JosepM posted.
HI Guys
Thanks so much for your help.
I put in the script you wrote BN and it works however, I now get a second image still coming up in the middle of the stack. In other words, having locked the size of the image container, the image goes into it but a second image appears in the middle of the stack. How can this happen when the script is saying where to put the image?
Also, I'm not sure how to connect the resize script Joseph sent. If I say ScaleObject to the same browse button after the image import to invoke the function, I get an error.
on mouseUp
set the cursor to watch
answer file "Please select an image file." with \
type "All Images|jpg,jpeg,gif,png|JPEG,GIFf,PNGf" or \
type "JPEG Images|jgp,jpeg|JPEG" or \
type "GIF Images|gif|GIFf" or \
type "PNG Images|png|PNGf"
if it is not empty then
## No need for a "temporary" image!
## with these lines you are "putting" the image file
## directly into your image "image"
## At first we lock the screen, so the scaling is not causing any flicker!
lock screen
put url("binfile:" & it) into image "image"
## Now scale that thin to the desired width and heigth, I use 600*400 in my example
## Get the original height and width of the image file
put the formattedwidth of img "image" into tCurrentW
put the formattedheight of img "image" into tCurrentH
put 600 into tMaxW
put 400 into tMaxH
## Get new max height and width
put ScaleObject(tCurrentW,tCurrentH,tMaxW,tMaxH) into tNewDimensions
put item 1 of tNewDimensions into tNewW
put item 2 of tNewDimensions into tNewH
## Changing height/width of an object will also affect its loc, unfortunately!
## so we restore it later!:
put the loc img "image" into tOldLoc
set the width of img "image" to tNewW
set the height of img "image" to tNewH
set the loc img "image" to tOldLoc
## Done!
unlock screen
else
beep
end if
end mouseUp
Please check all unknown terms in the dictionary for more info.
I am so tempted to go back and thank you for one of your posts from 2006 but I think it might be a moderator's headache to resurrect another old thread so I will make do with saying " thank you Klausimausi" here
SparkOut wrote:I am so tempted to go back and thank you for one of your posts from 2006 but I think it might be a moderator's headache to resurrect another old thread so I will make do with saying " thank you Klausimausi" here
Well, I'm a moderator and wouldn't mind, so go ahead if you like!