15 hours ago15 hr With MBS FileMaker Plugin, we can take existing images and scale them down. A typical use case is a user of your solution picking an image to import into a container field. But that image may be in different formats and may be huge. Typical images from modern digital cameras or mobile phones may come in high resolutions with file sizes over 5 MB. When storing thousands of images, we like to keep an eye on the file sizes and scale images down before writing them into container. The first script takes an image from a container field. Of course we can directly read it from a file path or a variable. But often we use global fields as the first place to store the newly imported image. We load it and scale it down to a maximum of 2000 by 2000 pixels. That is usually good to print the image on a paper. At 300 dpi this would be 6.6 inches or 16.9cm in size. You may use smaller sizes if the images are only needed to show on the web or for emails. Then we like to save them either as PNG or JPEG. We use PNG whenever the input image has an alpha channel. JPEG images can't handle transparency. We build a new file name based on the input file name without the extension. Just to make sure that FileMaker has a file name for exporting the field content. Since we like to keep files a bit smaller than a million bytes, we go down to 80% for the quality. In some solutions where the size really matters, we may even go down with size and quality until we hit the maximum size allowed. Here is the sample script:# Scale and save in file Image Scaling # Scale picture down to save space and save as JPEG or PNG # # Load from container Set Variable [ $Image ; Value: MBS("GMImage.NewFromContainer"; Image Scaling::InputImage) ] # In case of error If [ MBS("IsError") ] Show Custom Dialog [ "Failed to load image" ; $image ] Set Field [ Image Scaling::Info ; $Image ] Exit Script [ Text Result: ] Else # Scale down to maximum 2000 by 2000 pixels Set Variable [ $r ; Value: MBS("GMImage.Scale"; $Image; ">2000x2000") ] Set Variable [ $r ; Value: MBS("GMImage.SetQuality"; $Image; 90) ] # get a new file name Set Variable [ $filename ; Value: MBS( "Files.FileName"; GetAsText ( Image Scaling::InputImage ) ) ] Set Variable [ $filename ; Value: MBS( "Files.FileNameWithoutExtension"; $filename ) ] # For images with alpha channel, we use PNG, otherwise JPEG If [ MBS( "GMImage.GetMatte"; $Image ) ] # write out Set Field [ Image Scaling::Result ; MBS("GMImage.WriteToPNGContainer"; $Image; $filename & ".png") ] Else Set Field [ Image Scaling::Result ; MBS("GMImage.WriteToJPEGContainer"; $Image; $filename & ".jpg") ] # # let's check size Set Variable [ $size ; Value: MBS("Container.GetTotalSize"; Image Scaling::Result) ] If [ $size > 1000000 // over 1 MB ] # try again with 80% quality Set Variable [ $r ; Value: MBS("GMImage.SetQuality"; $Image; 80) ] Set Field [ Image Scaling::Result ; MBS("GMImage.WriteToJPEGContainer"; $Image) ] End If End If Set Field [ Image Scaling::Info ; MBS("GMImage.GetWidth";$Image) & " x " & MBS("GMImage.GetHeight";$Image) ] End If # Release image Set Variable [ $r ; Value: MBS("GMImage.Release";$Image) ]If you like, you can remove some metadata from the image before saving. We can remove XMP, EXIF and IPTC with the following lines. Just call them before doing the write: # remove metadata Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $Image; "EXIF"; "" ) ] Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $Image; "IPTC"; "" ) ] Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $Image; "XMP"; "" ) ]Since FileMaker 26 can handle WebP images, we can leverage this for even smaller file sizes. We set a quality and use GMImage.WriteToWebPContainer function to write the image as WebP. If you go down on the quality to a lower number, the file size may even be smaller. # Scale and save as WebP in file Image Scaling # Scale picture down to save space and save as WebP if smaller # # Load from container Set Variable [ $OriginalSize ; Value: MBS("Container.GetTotalSize"; Image Scaling::InputImage) ] Set Variable [ $Image ; Value: MBS("GMImage.NewFromContainer"; Image Scaling::InputImage) ] # In case of error If [ MBS("IsError") ] Show Custom Dialog [ "Failed to load image" ; $image ] Set Field [ Image Scaling::Info ; $Image ] Exit Script [ Text Result: ] Else # Scale down to maximum 2000 by 2000 pixels Set Variable [ $r ; Value: MBS("GMImage.Scale"; $Image; ">2000x2000") ] Set Variable [ $r ; Value: MBS("GMImage.SetQuality"; $Image; 80) ] # get a new file name Set Variable [ $filename ; Value: MBS( "Files.FileName"; GetAsText ( Image Scaling::InputImage ) ) ] Set Variable [ $filename ; Value: MBS( "Files.FileNameWithoutExtension"; $filename ) & ".webp" ] # write out Set Variable [ $Result ; Value: MBS("GMImage.WriteToWebPContainer"; $Image; $filename) ] # check size and only save if new image is smaller Set Variable [ $NewSize ; Value: MBS("Container.GetTotalSize"; $Result) ] If [ $NewSize < $OriginalSize ] Set Field [ Image Scaling::Result ; $Result] Set Field [ Image Scaling::Info ; MBS("GMImage.GetWidth";$Image) & " x " & MBS("GMImage.GetHeight";$Image) ] Else Set Field [ Image Scaling::Result ; $Result ] End If End If # Release image Set Variable [ $r ; Value: MBS("GMImage.Release";$Image) ]You may find these scripts in our Image Scaling.fmp12 sample file. Depending on the project you may modify them: Do you convert all images to JPEG, PNG or WebP? Or do you like to only save the new image if it is smaller?Do you like to scale down or leave the pixel size? Do you like to remove metadata? Please try and let us know if you have questions.
Create an account or sign in to comment