Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

GMImage.Scale

Resize image by using simple ratio algorithm.

Component Version macOS Windows Linux Server iOS SDK
GraphicsMagick 2.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "GMImage.Scale"; ImageRef; Geometry )   More

Parameters

Parameter Description Example
ImageRef The image reference number. 1
Geometry The new size. Geometry is specified as a width and height "100x200". "300x200"

Result

Returns "OK" on success.

Description

Resize image by using simple ratio algorithm.

GraphicsMagick has added a number of qualifiers to the standard geometry string for use when resizing images. The form of an extended geometry string is "<width>x<height>{+-}<xoffset>{+-}<yoffset>{%}{!}{<}{>}". Extended geometry strings should only be used when resizing an image. Using an extended geometry string for other applications may cause the API call to fail. The available qualifiers are shown in the following table:

QualifierDescription
%Interpret width and height as a percentage of the current size.
!Resize to width and height exactly, loosing original aspect ratio.
<Resize only if the image is smaller than the geometry specification.
>Resize only if the image is greater than the geometry specification.

See GMImage.SetDensity and GMImage.SetResolutionUnits to set the resolution in DPI.
See also GMImage.SetFilterType to pick algorithm.

Examples

Scales the image to 50% with calculating ourself

Let ( [
$Image = MBS("GMImage.NewFromContainer"; GraphicsMagick::image);
$Width = MBS("GMImage.GetWidth";$Image);
$Height = MBS("GMImage.GetHeight";$Image);
$Width = $Width/2;
$Height = $Height/2;
$ScaleResult = MBS("GMImage.Scale";$Image; $Width & "x" & $Height);
$Result = MBS("GMImage.WriteToPNGContainer"; $Image);
$Error = MBS("GMImage.Release";$Image)
];
$Result)

Scales the image to 50% easily

Let ( [
$Image = MBS("GMImage.NewFromContainer"; GraphicsMagick::image);
$ScaleResult = MBS("GMImage.Scale";$Image; "50%");
$Result = MBS("GMImage.WriteToPNGContainer"; $Image);
$Error = MBS("GMImage.Release";$Image)
];
$Result)

Scales the image down to 640 pixel width, if it is bigger

Let ( [
$Image = MBS("GMImage.NewFromContainer"; GraphicsMagick::image);
$ScaleResult = MBS("GMImage.Scale";$Image; "640>");
$Result = MBS("GMImage.WriteToPNGContainer"; $Image);
$Error = MBS("GMImage.Release";$Image)
];
$Result)

Scale as script:

Set Variable [$Image; Value:MBS("GMImage.NewFromContainer"; Image Scaling::InputImage)]
Set Variable [$r; Value:MBS("GMImage.Scale"; $Image; "800x600")]
Set Field [Image Scaling::Result; MBS("GMImage.WriteToPNGContainer"; $Image)]
Set Variable [$r; Value: MBS("GMImage.Release"; $Image)]

Download picture and scale it down:

Set Variable [ $curl ; Value: MBS("CURL.New") ]
Set Variable [ $r ; Value: MBS("CURL.SetOptionURL"; $curl; MIX::URL) ]
Set Variable [ $r ; Value: MBS("CURL.SetOptionFollowLocation"; $curl; 1) ]
Set Variable [ $r ; Value: MBS("CURL.SetOptionMaxRedirs"; $curl; 3) ]
Set Variable [ $result ; Value: MBS("CURL.Perform"; $curl) ]
Set Variable [ $debug ; Value: MBS( "CURL.GetDebugMessages"; $curl) ]
If [ $result = "OK" ]
    Set Variable [ $Image ; Value: MBS( "CURL.GetResultAsContainer"; $curl) ]
    Set Variable [ $ImageRef ; Value: MBS("GMImage.NewFromContainer"; $image) ]
    If [ MBS("IsError") = 0 ]
        Set Variable [ $r ; Value: MBS("GMImage.Scale";$ImageRef; "256x256>") ]
        Set Variable [ $ScaledImage ; Value: MBS("GMImage.WriteToJPEGContainer"; $ImageRef) ]
        If [ MBS("IsError") = 0 ]
            Set Field [ MIX::Image ; $ScaledImage ]
        End If
        Set Variable [ $r ; Value: MBS("GMImage.Release";$ImageRef) ]
    End If
End If
Set Variable [ $r ; Value: MBS("CURL.Release"; $curl) ]

Scale with error checking:

Let ( [
Image = MBS("GMImage.NewFromContainer"; MyContainer);
// did loading work?
Error = MBS("IsError");
// don't scale if it failed
ScaleResult = If(Error; ""; MBS("GMImage.Scale"; Image; "500>"));
NewName = "Preview.png" ;
// pass through old container, if we can't load it.
NewImage = If(Error; MyContainer; MBS("GMImage.WriteToPNGContainer"; Image; NewName));

ReleaseResult = MBS("GMImage.Release"; Image)
];
NewImage)

Read image and scale down for preview:

Set Variable [ $image ; Value: MBS( "GMImage.NewFromContainer"; $input) ]
If [ MBS("IsError") = 0 ]
    # limit size
    Set Variable [ $scale ; Value: MBS("GMImage.Scale";$Image; "1000>") ]
    # remove metadata
    Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $image; "IPTC"; "" ) ]
    Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $image; "EXIF"; "" ) ]
    Set Variable [ $r ; Value: MBS( "GMImage.SetProfile"; $image; "XMP"; "" ) ]
    # save
    Set Variable [ $NewImage ; Value: MBS("GMImage.WriteToJPEGContainer"; $Image) ]
    If [ MBS("IsError") = 0 ]
        Set Field [ Get Preview::Preview ; $NewImage ]
        Set Field [ Get Preview::Made using ; "GraphicsMagick" ]
        Set Variable [ $r ; Value: MBS("GMImage.Release";$Image) ]
        Exit Script [ Text Result: ]
    End If
    Set Variable [ $r ; Value: MBS("GMImage.Release";$Image) ]
End If

Standardize all images to a maximum of 800x800 points @ 144 dpi which is 1600x1600 pixels:

Set Variable [$ImageRef; Value:MBS("GMImage.NewFromContainer"; Test::BigImage)]
Set Variable [$Result; Value:MBS("GMImage.Scale"; $ImageRef; "1600x1600") // maximum size in pixels ]
Set Variable [$Result; Value:MBS("GMImage.SetResolutionUnits"; $ImageRef; 1) // resolution is in DPI ]
Set Variable [$Result; Value:MBS("GMImage.SetDensity"; $ImageRef; "144x144") // set the DPI value ]
Set Variable [$Result; Value:MBS("GMImage.SetQuality"; $ImageRef; 90) // save as JPEG with 90% ]
Set Field [Test::Result; MBS( "GMImage.WriteToJPEGContainer"; $ImageRef )]
Set Variable [$Error; Value:MBS("GMImage.Release";$ImageRef)]

See also

Release notes

  • Version 14.5
    • Added a check to GMImage.Scale to return an error if width or height is > 99999.

Example Databases

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 17th February 2024


GMImage.Sample - GMImage.Scaling