Scale Image to fit my screen?

Hello,

my problem is, that if someone uploads in image, I have to scale it to fit screen.

So I wrote this script, but this will not work with other image-sizes…

any hint what to do better?

function createImage( path, pX, pY ) local image = display.newImage( path, true ) if ( image.width \> image.height ) then image.rotation = 90 end if ( image.width \> gX ) or ( image.height \> gY ) then image.xScale = ( 0.5 \* image.contentWidth )/ image.contentWidth image.yScale = image.xScale end image.x = pX image.y = pY return image end

Do you want it to fill the whole screen?

Either way, use display.newImageRect(). It lets you specify the size to display the image at :wink:

The point is that its stretched…

In css it would be for example:
{
Width: 100%
}

So it fills screen but doesnt get stretched

aspect ratio = width / height

calc image aspect ratio (iar), calc screen aspect ratio (sar)

if (iar > sar) fit to width, using same scale for height (causing letterbox at top/bottom)

else fit to height, using same scale for width (causing pillarbox at left/right)

(or invert if you’d rather fill the poor-ar-match axis and bleed the good-ar-match axis)

local scale = math.max(screenWidth / img.width, screenHeight  / img.height)  img:scale(scale, scale)

Do you want it to fill the whole screen?

Either way, use display.newImageRect(). It lets you specify the size to display the image at :wink:

The point is that its stretched…

In css it would be for example:
{
Width: 100%
}

So it fills screen but doesnt get stretched

aspect ratio = width / height

calc image aspect ratio (iar), calc screen aspect ratio (sar)

if (iar > sar) fit to width, using same scale for height (causing letterbox at top/bottom)

else fit to height, using same scale for width (causing pillarbox at left/right)

(or invert if you’d rather fill the poor-ar-match axis and bleed the good-ar-match axis)

local scale = math.max(screenWidth / img.width, screenHeight  / img.height)  img:scale(scale, scale)