471,089 Members | 1,251 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

calculate image height or width

Sorry folks, I'm on a roll!!!

I need to calculate the height or width of an image if you change
either the width or height and don't change BOTH of them at the same
time, this is to "vector" the image.

Here is the class method, it obviously doesn't work:

[PHP]
/**
* Perform calculation solely upon re-entered width and height if a
calculation percentage was not entered nor chosen. Can be called
statically
*
* @access protected
* @param int $width (reference)
* @param int $height (reference)
* @param int $origWidth
* @param int $origHeight
* @return array $dimArray Array consisting of recalculated width and
height
*/
function &calculate_width_and_height(&$width, &$height, $origWidth,
$origHeight) {
print_r("width = $width and origWidth = $origWidth and height =
$height and origHeight = $origHeight<P>");
if ((int)$origWidth === 0 || (int)$origHeight === 0) return array();
// TO AVOID DIVISION BY ZERO
if ((int)$origWidth !== (int)$width && (int)$origHeight !==
(int)$height) return array($width, $height); // NO NEED TO CALCULATE
SINCE CHANGING ALL DIMENSIONS
if ((int)$origWidth !== (int)$width) $height = $height / ($width /
$origWidth);
if ((int)$origHeight !== (int)$height) $width = $width / ($height /
$origHeight);
print_r("width = $width and origWidth = $origWidth and height =
$height and origHeight = $origHeight<P>");
return array($width, $height);
}
[/PHP]

Ok, folks, what on earth do I do? The calculations are horrifically
wrong. How do I fix this? The values of $width and $height are entered
via FORM posts.

Phil

Apr 10 '06 #1
1 3726
Never mind, I got it on my own:

/**
* Perform calculation solely upon re-entered width and height if a
calculation percentage was not entered nor chosen. Can be called
statically
*
* @access protected
* @param int $width (reference)
* @param int $height (reference)
* @param int $origWidth
* @param int $origHeight
* @return array $dimArray Array consisting of recalculated width and
height
*/
function &calculate_width_and_height(&$width, &$height, $origWidth,
$origHeight) {
if ((int)$origWidth === 0 || (int)$origHeight === 0) return
array($width, $height); // TO AVOID DIVISION BY ZERO
if ((int)$origWidth !== (int)$width && (int)$origHeight !==
(int)$height) return array($width, $height); // NO NEED TO CALCULATE
SINCE CHANGING ALL DIMENSIONS
if ((int)$origWidth !== (int)$width) $height = $width * ($origHeight
/ $origWidth);
if ((int)$origHeight !== (int)$height) $width = $height * ($origWidth
/ $origHeight);
return array(floor($width), floor($height));
}

Phil

Apr 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tjerk | last post: by
9 posts views Thread by Karl Burrows | last post: by
5 posts views Thread by Csaba Gabor | last post: by
15 posts views Thread by Anand Ganesh | last post: by
6 posts views Thread by abdullah1983 | last post: by
4 posts views Thread by tshad | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.