php 获取窗口尺寸,php根据高宽比及显示窗口大小重置图片样式

PHP重置根据图片显示大小及位置重置图片高宽和位置,溢出隐藏

实现功能:

重置图片高宽和位置,溢出隐藏

保证在规定窗口满窗显示

直接在图片style上使用输出

代码实现:

/*

重置图片高宽和位置,溢出隐藏

保证在规定窗口满窗显示

直接在图片style上使用输出

参数:

$ratio : 需要定义图片的宽高比

$w : 外框的宽度

$h : 外框的高度

$type : 返回值类型,1.组合好的css字串,2.图片新的高宽值数组

返回值:

例:width:200px;height:521px;margin-top:-260px;

*/

function resizeImgStyle($ratio,$w=200,$h=160,$type=1){

$width = $w != 0 ? $w : 200;

$height = $h != 0 ? $h : 200;

$frameRatio= $w/$h;//外框大小比例

$offestX = 0;

$offestY = 0;

$newImgWidth = 0;

$newImgHeight = 0;

if($frameRatio>=$ratio){

//宽一点 最小边高 取高

$newImgWidth = $width;

$newImgHeight = $width/$ratio;

$offestX = 0;

$offestY = ($newImgHeight-$height)/2;

}else{

//窄一点 最小边宽 取宽

$newImgHeight = $height;

$newImgWidth = $height*$ratio;

$offestX = ($newImgWidth-$width)/2;

$offestY = 0;

}

if($type==2){

// 返回数组

$style = array('img_width'=>(int)$newImgWidth,'img_height'=>(int)$newImgHeight);

}else{

// 判断

$offsetStyle = '';

$offsetStyle .= $offestY>0 ? 'margin-top:-'.(int)$offestY .'px;' : '' ;

$offsetStyle .= $offestX>0 ? 'margin-left:-'.(int)$offestY.'px;' : '' ;

$style = 'width:'.(int)$newImgWidth.'px;'.'height:'.(int)$newImgHeight.'px;'.$offsetStyle;

}

return $style;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值