calculate image height or width

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    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_widt h_and_height(&$ width, &$height, $origWidth,
    $origHeight) {
    print_r("width = $width and origWidth = $origWidth and height =
    $height and origHeight = $origHeight<P>" );
    if ((int)$origWidt h === 0 || (int)$origHeigh t === 0) return array();
    // TO AVOID DIVISION BY ZERO
    if ((int)$origWidt h !== (int)$width && (int)$origHeigh t !==
    (int)$height) return array($width, $height); // NO NEED TO CALCULATE
    SINCE CHANGING ALL DIMENSIONS
    if ((int)$origWidt h !== (int)$width) $height = $height / ($width /
    $origWidth);
    if ((int)$origHeig ht !== (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

  • comp.lang.php

    #2
    Re: calculate image height or width

    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_widt h_and_height(&$ width, &$height, $origWidth,
    $origHeight) {
    if ((int)$origWidt h === 0 || (int)$origHeigh t === 0) return
    array($width, $height); // TO AVOID DIVISION BY ZERO
    if ((int)$origWidt h !== (int)$width && (int)$origHeigh t !==
    (int)$height) return array($width, $height); // NO NEED TO CALCULATE
    SINCE CHANGING ALL DIMENSIONS
    if ((int)$origWidt h !== (int)$width) $height = $width * ($origHeight
    / $origWidth);
    if ((int)$origHeig ht !== (int)$height) $width = $height * ($origWidth
    / $origHeight);
    return array(floor($wi dth), floor($height)) ;
    }

    Phil

    Comment

    Working...