Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在调整大小时设置最大高度_Php_Javascript_Jquery - Fatal编程技术网

Php 在调整大小时设置最大高度

Php 在调整大小时设置最大高度,php,javascript,jquery,Php,Javascript,Jquery,我有一个脚本,它使用此函数在加载和调整大小时调整图像大小: /** * Calculates the display width of an image depending on its display height when it is resized. * @param displayHeight the resized height of the image * @param originalHeight the original height of the image * @pa

我有一个脚本,它使用此函数在加载和调整大小时调整图像大小:

/**
 * Calculates the display width of an image depending on its display height when it is resized.
 * @param displayHeight the resized height of the image
 * @param originalHeight the original height of the image
 * @param originalWidth the original width of the image
 * @return the display width
 */
function getDisplayWidth(displayHeight, originalHeight, originalWidth){
    var ratio = originalHeight/displayHeight,
        res = Math.round(originalWidth/ratio) || 1000;
    return res;
}
。。但我不希望图像高度超过800px,事实上它甚至可以固定为800×530px大小。我试图将一个固定值返回到
res
,但它似乎不起作用


谢谢

您只需要在函数中添加if语句

/**
 * Calculates the display width of an image depending on its display height when it is resized.
 * @param displayHeight the resized height of the image
 * @param originalHeight the original height of the image
 * @param originalWidth the original width of the image
 * @return the display width
 */
function getDisplayWidth(displayHeight, originalHeight, originalWidth){
    if (displayHeight > 800) displayHeight = 800;
    var ratio = originalHeight/displayHeight,
        res = Math.round(originalWidth/ratio) || 1000;
    return res;
}
设置宽度时,它将自动将高度设置为正确的宽高比值。还可以通过将变量传递给getDisplayWidth来获取高度,然后当函数返回时,该变量将具有由“最大高度”条件定义的高度