Created
August 6, 2019 11:54
-
-
Save bennadel/b88c0efc86ff6a46ce79186cf39011fb to your computer and use it in GitHub Desktop.
Fixing ImageScaleToFit() Invalid Size Errors In Earlier Versions Of Lucee CFML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
img = imageNew( | |
source = "", | |
width = 16, | |
height = 2000, | |
canvasColor = "ff3366" | |
); | |
width = imageGetWidth( img ); | |
height = imageGetHeight( img ); | |
boxSize = 100; | |
// When scaling the image, whichever dimension has the largest magnitude becomes | |
// our limiting factor. This is the magnitude that drives the proportional scaling. | |
sizeConstraint = max( width, height ); | |
// We only need to resize the image if some part of it doesn't fit inside the box. | |
if ( sizeConstraint > boxSize ) { | |
// CAUTION: In earlier versions of Lucee, this will not work because the | |
// proportional scaling of the image will cause the WIDTH to be LESS THAN 1. As | |
// such, the underlying Java code will try to create a canvas of WIDTH = 0, which | |
// is invalid. | |
imageScaleToFit( img, boxSize, boxSize, "highestPerformance" ); | |
} | |
imageWrite( img, "./output-scaled.png" ); | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
img = imageNew( | |
source = "", | |
width = 16, | |
height = 2000, | |
canvasColor = "ff3366" | |
); | |
width = imageGetWidth( img ); | |
height = imageGetHeight( img ); | |
boxSize = 100; | |
// When scaling the image, whichever dimension has the largest magnitude becomes | |
// our limiting factor. This is the magnitude that drives the proportional scaling. | |
sizeConstraint = max( width, height ); | |
// We only need to resize the image if some part of it doesn't fit inside the box. | |
if ( sizeConstraint > boxSize ) { | |
// In order to get around the proportional scaling issues in earlier versions of | |
// Lucee, we cannot rely on the auto-scaling of the image. Instead, we have to | |
// explicitly calculate the dimensions and then use the imageResize() function | |
// instead of the imageScaleToFit() function. | |
scalingFactor = ( boxSize / sizeConstraint ); | |
// Make sure none of the scaled values drop below 1 - the scaled image has to | |
// have non-zero dimensions. | |
scaledWidth = max( fix( width * scalingFactor ), 1 ); | |
scaledHeight = max( fix( height * scalingFactor ), 1 ); | |
imageResize( img, scaledWidth, scaledHeight, "highestPerformance" ); | |
} | |
imageWrite( img, "./output-resized.png" ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment