Question

I've been trying to programmatically resize and crop images with ColdFusion 10. What's driving me nuts is that I can not make it equally crop images from bottom and top while maintaining the same width in the process.

Here is what I currently have, just a few simple lines:

    <cfimage source="images/test/airateapple.png" name="myImage" overwrite="yes">

    <cfif ImageGetWidth(myImage) gte 1024>
        <cfset ImageSetAntialiasing(myImage,"on")>
        <cfset ImageScaleToFit(myImage,800,"","mediumquality")>
        <cfif ImageGetHeight(myImage) gt 350> 
            <cfset sizeToCrop= ImageGetHeight(myImage) - 350>
            <cfset ImageCrop(myImage,0, sizeToCrop
                               , ImageGetWidth(myImage)
                               , ImageGetHeight(myImage) )>
        </cfif>

        <cfset finalImage=myImage>
    </cfif>

    <!--- Display the modified image in a browser. --->
    <cfimage source="#finalImage#" action="writeToBrowser">

For example, if image height is 500px after resizing is done, it should crop an additional 150px. More specifically, crop 75px from bottom and 75px from the top. Is it possible?

Was it helpful?

Solution

<cfset sizeToCrop= ImageGetHeight(myImage) - 350>
<cfset ImageCrop(myImage, 0 
                     , #sizeToCrop# 
                     , #ImageGetWidth(myImage)#
                     , #ImageGetHeight(myImage)#
                   )>

If you output the parameters, you can see your y and height values are off. Say the original image dimensions are 500px x 500px. Right now you are starting the crop too low, (ie y=150px) and using the original height instead of the desired height (ie 350px).

       // current code (wrong)
       ImageCrop(myImage, 0 , 150 , 500 , 500 )

To grab the center of the image, you need to start cropping at y=75 (ie excess height / 2). Then use the desired height (ie 350px), not the original:

       // ImageCrop( img, x, y, width, height )
       yPosition  = (originalHeight - desiredHeight) / 2;
       ImageCrop(myImage, 0, yPosition, originalWidth, desiredHeight );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top