a5_runimagemagiccommand Function
Syntax
Arguments
- fileInCharacter
The image file to transform. Supported file types include .pdf,.jpg, .jpeg, .bmp, and .heic.
- fileOutCharacter
The filename for the transformed image.
- commandCharacter
The ImageMagick command that defines the transformation. Supports options used with the ImageMagick convert tool.
Returns
- resultPointer
Returns a dot variable with the following properties:
- hasErrorLogical
If the operation succeeds, result.hasError will be .F..
- errorTextCharacter
If the operation fails, result.errorText will contain an error message.
Description
Transforms an image using ImageMagick. Command is an ImageMagick transform command e.g. -resize 800x300
Discussion
The a5_runImageMagicCommand() function can be used to transform an image using ImageMagick. The method uses the ImageMagick "convert" tool to transform an image. You can pass in any command that is supported by the convert tool as the third argument to the function.
If an error occurs while executing the command, you can check the value of the returned dot variable to find out more information about the failure.
dim fileIn as c = "C:\temp\img.jpg" dim fileOut as c = "C:\temp\img_resize.jpg" dim result as p result = a5_runImageMagicCommand(fileIn, fileOut, "-resize 20%") if (result.hasError) then ' an error occurred: showvar(result.errorText) end if
The examples below demonstrate using a5_runImageMagicCommand() to resize, flip, colorize, or apply interesting effects to an image.
Example: Resizing an Image
dim fileIn as c = "C:\temp\img.jpg" dim fileOut as c = "C:\temp\img_resize.jpg" dim result as p ' Resize image to 20% it's original size: result = a5_runImageMagicCommand(fileIn, fileOut, "-resize 20%") ' Resize image to 500px wide. Height will be adjusted to maintain aspect ratio: result = a5_runImageMagicCommand(fileIn, fileOut, "-resize 500") ' Resize image to 300p high. Width will be adjusted to maintain aspect ratio: result = a5_runImageMagicCommand(fileIn, fileOut, "-resize x300") ' Resize image to 800x300 regardless of the original aspect ratio result = a5_runImageMagicCommand(fileIn, fileOut, "-resize 800x300!")
Example: Flipflopping Images
dim fileIn as c = "C:\temp\img.jpg" dim result as p ' Flips an image in the vertical direction: result = a5_runImageMagicCommand(fileIn, "C:\temp\img_flip.jpg", "-flip") ' Flops an image in the horizontal direction: result = a5_runImageMagicCommand(fileIn, "C:\temp\img_flop.jpg", "-flop")
Example: Apply Effects to an Image
dim fileIn as c = "C:\temp\img.jpg" dim result as p ' Convert the image to black & white: result = a5_runImageMagicCommand(fileIn, "C:\temp\img_blackAndWhite.jpg", "-monochrome") ' Convert the image to Sepia tones: result = a5_runImageMagicCommand(fileIn, "C:\temp\img_sepia.jpg", "-sepia-tone 80%") ' Apply a vignette effect: result = a5_runImageMagicCommand(fileIn, "C:\temp\img_vignette.jpg", "-vignette 100x65000")
Example: Converting an Image
See Supported Image Formats for a list of supported types by ImageMagick.
dim fin as c = "C:\temp\image.tiff" dim fout as c = "C:\temp\image.pdf" ? a5_runimagemagiccommand(fin, fout, "") = errorText = "" hasError = .F.
Example: Converting an .heic file to .jpg
dim fileIn as c = "C:\data\img.heic" dim fileOut as c = "C:\data\img_resize.jpg" dim result as p result = a5_runImageMagicCommand(fileIn, fileOut, "-resize 20%") if (result.hasError) then ' an error occurred: showvar(result.errorText) end if
For more information about the "convert" command and the options available, see the ImageMagick convert documentation.
See Also