Getting the dimensions of a picture file

You might want to know the dimensions of a picture the user is selecting, for example to check it is not too large or too small for it's intended use.

The imageFileDimensions function

This custom function is called with one parameter pFilePath, the path to an image file.

get imageFileDimensions("/Disk/Folder/Picture File")
put item 2 of imageFileDimensions(pMyFile) into theHeight
set the naturalDimensions of me to imageFileDimensions(it)

The imageFileDimensions function gets the width and height of a picture file in any of the formats that LiveCode can import. It does this by importing the file as an image object and checking the new image's width and height properties.

Importing the image

When the file is imported, the new image appears on the screen. Since it's only going to be there long enough to get the height and width, we don't want the user to see the annoying flash of the image appearing and disappearing, so the first thing this handler does is to use the lock screen command to prevent changes from appearing in the window.

lock screen 

With the screen safely locked, the handler uses the import command to import the file into the stack, where it becomes a new image.

import paint from file pFilePath

Getting the width and height

The width and height of this image are automatically set to the picture's natural width and height, so we can check these properties to get the width and height of the original picture file.

put the width of last image into tWidth
put the height of last image into tHeight

Once the handler has got the properties, we no longer need the imported image, so we delete it. This removes the image object we created with the import command, leaving the original picture file where it was. We can then use the unlock screen command as no more screen updates will be made.

delete last image
unlock screen

Finally, the handler returns the width and height, separated by a comma (the , operator). The calling handler can use both, or (if it only needs one of the height or width) can check item 1 or item 2 of the return value.

return tWidth, tHeight

The imageFileDimensions function code

function imageFileDimensions pFilePath
	local tWidth, tHeight
   
	##  so user doesn't see screen updated
	lock screen
   
	## create a new image
	import paint from file pFilePath
   
	put the width of last image into tWidth
	put the height of last image into tHeight
	delete last image
	unlock screen
   
	return tWidth, tHeight
end imageFileDimensions

1 Comments

Jean-Marc Burnod

Hello,
To know width and height of a lot of png file, you may use the function below.

function GetWidthHeightPngFile pPathIm --
put empty into rGetWidthHeightPngFile
open file pPathIm for binary read
read from file pPathIm for 24
put it into tData
close file pPathIm
if byte 13 to 16 of tData <> "IHDR" then -- "Invalid PNG format"
return empty
exit GetWidthHeightPngFile
end if
local tWidth, tHeight
put binaryDecode("MM", byte 17 to 24 of tData, tWidth, tHeight)\
into tNumConvertedVals
if tNumConvertedVals <> 2 then -- Couldn't decode binary data
return empty
exit GetWidthHeightPngFile
end if
put tWidth & "," & tHeight into rGetWidthHeightPngFile
return rGetWidthHeightPngFile
end GetWidthHeightPngFile

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.