BoxLang 🚀 A New JVM Dynamic Language Learn More...
Helper service layer for interacting with ImageMagick in CFML
Ensure you either set the IMAGEMAGICKPATH in your .env
to the path of the magick executable OR in your
ColdBox settings structure
IMAGEMAGICKPATH=/usr/bin/magick
Check this is the correct path and your CFML server can execute this
by running the verifyImageMagick() function
Instantiate the helper service via the wirebox DSL Helpers@ImageMagick
property name="imageService" inject="Helpers@ImageMagick";
Convert an image to webp, stripping metadata, and capping it at 1600px on its longest side:
imageService.convert(
path = '/tmp/uploads/photo.jpg',
outputPath = '/var/www/images/photo.webp',
quality = 75,
resize = 1600
);
Generate a few resized versions of the same source image in one call:
var resizedPaths = imageService.resize(
path = '/tmp/uploads/photo.jpg',
outputs = [
{resizeDir: '/var/www/images/small', width: 100},
{resizeDir: '/var/www/images/banner', width: 1200, height: 400}
]
);
Validate and convert a real multipart upload in a handler action:
function uploadPhoto(event, rc, prc) {
var fileName = imageService.validateUpload(
formField = 'file',
outputs = [
{uploadDir: '/var/www/images/original', type: 'jpg'},
{uploadDir: '/var/www/images/webp', type: 'webp'}
],
extensions = 'png,jpg,jpeg'
);
}
Every function validates its arguments and throws a typed
ImageMagick.*Exception (e.g.
ImageMagick.ConversionException,
ImageMagick.UploadValidationException) on failure - wrap
calls in try/catch where you need to handle those.
For the full list of functions, their arguments, return values, and exceptions, see docs/functions.md.
$
box install ImageMagickHelpers