06-12-2023, 07:48 PM
Another new feature introduced in 0.7.0 is the ability to trigger uploads and downloads to and from the browser from within your QBJS program. The following example shows how to use this feature. (As a bonus it also uses the new SaveImage method from the new 2D graphics library.)
View in QBJS
Code: (Select All)
Import FS From "lib/io/fs.bas"
Import G2D From "lib/graphics/2d.bas"
Dim Shared img As Long
Screen _NewImage(600, 600, 32)
Print "Welcome to Image Decorator!"
Print "Press any key to upload an image file..."
Sleep
' Prompt the user to upload a PNG file
' and save it to the temp directory
' Call the OnUpload sub with complete
MkDir "temp"
FS.UploadFile "temp", ".png, .jpg", sub_OnUpload
' Wait for the image to be uploaded
Do
_Limit 60
Loop Until img
' Decorate the image
DecorateImage
' Make a copy of the image
Dim imgCopy As Long
imgCopy = _CopyImage(_Dest)
' Save the image
Color 0
_PrintMode _KeepBackground
Print "Press any key to save the new image..."
Sleep
' Save the image to the virtual file system
G2D.SaveImage imgCopy, "temp/newimage.png"
' Prompt the user to download the image file
FS.DownloadFile "temp/newimage.png"
Sub OnUpload (filename As String)
img = _LoadImage(filename)
End Sub
Sub DecorateImage
' Scale the image to fit the screen
Dim As Double scale
Dim As Integer w, h, x, y
w = _Width(img)
h = _Height(img)
If w > h Then
scale = h / w
w = _Width
h = w * scale
Else
scale = w / h
h = _Height
w = h * scale
End If
' Center the image
w = w - 50
h = h - 50
x = (_Width - w) / 2
y = (_Height - h) / 2
' Draw a drop shadow
Cls , 15
G2D.Shadow _RGB(100, 100, 100), 8, 8, 15
Line (x, y)-(x + w - 1, y + h - 1), 0, BF
G2D.ShadowOff
' Draw the image
_PutImage (x, y)-(x + w, y + h), img
End Sub
View in QBJS