Skip to main content
Inspiring
October 15, 2008
Answered

cfscript. recalculating numbers

  • October 15, 2008
  • 2 replies
  • 594 views
Hello

I have a database with images. There are 3 columns, one with the width, the other one with the height and the last one the name of the file.

I would like now to send width and height to a cfscript which recalculates the width/height values - I know this sounds strange but I need it for a representation on a website. So note that I don't want to resize the oiginal image, I just want to have values. I neither want to change anything in the database!

And I don't need to do that on several images at the time!

I imagine something like this: <cfset newDimension = rescaleImage(503,2004,250,500)>
where
'rescaleImage' would be the script
503 the width coming from my database
2004 the height coming from my database
250 the max. width
500 the max. height

So the script should check.
1. whether the dimension has to be resized (if smaller than the max. values the output should be the values similar to the ones in the database)
2. whether the width has to be reduced to the max. width and the height calculated proportionally to the max. width
3. whether the height has to be reduced to the max. height and the width calculated proportionally to the max. height

and the output could be a two value list containing width and height....

Does anybody know whether such a script already exists or can somebody help me wrting it (what means this somebody would have to write it ;-)

Many thanks

Actually I could calculate this stuff in Coldfusion with cfif and evaluate and so, but I think this is so much code each time. So i prefer sending values to a script and get the stuff back....
This topic has been closed for replies.
Correct answer Newsgroup_User
Not fully tested, and I do not know why you insisted on <cfscript> but
here you go.

<cfscript>
function rescaleImage(curX,curY,maxX,maxY)
{
var returnStr = structNew();
//if current X is greater then maximum X
if (curX GT maxX)
{
//set return X to the maximum and adjust current Y proportionally
returnStr.thisX = maxX;
curY = curY * (maxX/curX);
}
else
//else return current x
{
returnStr.thisX = curX;
}

//if current Y is greater then maximum Y
if (curY GT maxY)
{
// set return Y to maximum y and adjust return X proportionally.
returnStr.thisY = maxY;
returnStr.thisX = returnStr.thisX * (maxY/curY);
}
else
{
//else return current Y
returnStr.thisY = curY;
}
return returnStr;
}
</cfscript>

<cfset adjustedImg = rescaleImage(503,2004,250,500)>
<cfdump var="#adjustedImg#">
</body>

2 replies

Newsgroup_UserCorrect answer
Inspiring
October 15, 2008
Not fully tested, and I do not know why you insisted on <cfscript> but
here you go.

<cfscript>
function rescaleImage(curX,curY,maxX,maxY)
{
var returnStr = structNew();
//if current X is greater then maximum X
if (curX GT maxX)
{
//set return X to the maximum and adjust current Y proportionally
returnStr.thisX = maxX;
curY = curY * (maxX/curX);
}
else
//else return current x
{
returnStr.thisX = curX;
}

//if current Y is greater then maximum Y
if (curY GT maxY)
{
// set return Y to maximum y and adjust return X proportionally.
returnStr.thisY = maxY;
returnStr.thisX = returnStr.thisX * (maxY/curY);
}
else
{
//else return current Y
returnStr.thisY = curY;
}
return returnStr;
}
</cfscript>

<cfset adjustedImg = rescaleImage(503,2004,250,500)>
<cfdump var="#adjustedImg#">
</body>
Inspiring
October 16, 2008
Ian you are the best.
Thank you so much!!!!
Inspiring
October 15, 2008
Hi,
I don't know if it's useful for you, but I'm always using a javascript function to adjust images within a container (client-side in the browser).

cheers,
fober

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>

<body>
<style type="text/css">
div.image-container{
border: 1px solid red;
width:200px;
height:200px;
overflow: hidden;
}
</style>

<script type="text/javascript">
function resize(obj){
if(obj.height > obj.width)
obj.style.height='100%';
else
obj.style.width='100%';
}

</script>


<div class="image-container">
<img id="pic1" onload="resize(this)" src="sample-300-300.gif">
</div>

<div class="image-container">
<img id="pic2" onload="resize(this)" src="sample-100-300.gif">
</div>

<div class="image-container">
<img id="pic3" onload="resize(this)" src="sample-300-100.gif">
</div>


</body>
</html>

Inspiring
October 15, 2008
Dear fober1

Thank you for your input but I'm afraid that it doesn't solve my problem. And I would prefer not to do this client-side though not to use javascript.
I think I have to insist on cfscript...

Thank you anyway and I'm very thankful for further inputs...
Inspiring
October 15, 2008
Do a search at cflib.org. It has a wide range of functions. Just two examples are:

http://cflib.org/udf/imageResizeAspectRatio
http://cflib.org/udf/imageScale
...