Can anyone suggest a simple plugin to edit a user pic? The idea is to choose a picture from a MediaLibrary and select an square area of a picture that will go on a user pic. I am working on my own, but a few things are challenging and very from device to device. I wish I could look at an existing solutions/code. Thanks.
No answers in 10 days. That’s ok as I am pioneering in this field Can anyone help me then to scale & crop images correctly, so that they would be the same on any device.
If you want to scale a image to the size of a certain rect keeping its aspect ratio this might help.
if photo.height > rect.height then hScale = rect.height/photo.height photo:scale( hScale, hScale ) if photo.contentWidth > width then wScale = width/photo.contentWidth photo:scale( wScale, wScale ) end elseif photo.width > width then wScale = width/photo.width photo:scale( wScale, wScale ) elseif photo.width < width then wScale = width/photo.width photo:scale( wScale, wScale ) end
If you run this code on a few emulated devices with different resolutions, you will see that same scale value gives DIFFERENT results!!
Here is my scale value for ALL the devices: 0.22529644268775, also the resolution of the input file is obviously the same, as the code works with one file (3036x4048).
- Samsung (1080x1920), output file: 1026 x 1368
- HTC (540x960), output file: 513 x 684
- iPad (2048x2732), output file: 1460 x 1947
To get the same result I guess, the scale value must be different for different resolutions, even though in config.lua I got this:
width = 720, height = 1280,
All I have to do is to figure out the formula, using physical resolution of the screen… Is there a method how to read it? I have not found a proper answer… Environment library does not help here…
Here I came out with a peculiarity of command.scale command.
It needs scale-value counted out of physical resolution of the screen, NOT the one set in config.lua. I wish the command.scale was based upon display.contentWidth & … Height (which are values from config.lua).
In my particular case width of screen is set in config.lua as 720, the finale width of an image-file got to be 684 pixels (=720/20*19). The result got to be 684 pixels NO MATTER what is set in config.lua!
So, the right scale value is a proportion of two pairs of values!
scale_1 = 684/photo.contentWidth scale_2 = display.pixelWidth/display.contentWidth finale_scale = scale_1/scale_2
(or Height instead of Width if needed)
Now, if I scale the image (photo:scale) and save it via display.save the result is what needed no matter what is the physical resolution of the screen.
It took me a few days to figure out how command.scale works for images… I even started to like the idea which I read on this forum: someone wanted to send an image to a server and do all the manipulations (scaling, cropping) there using the server’s programming language…