PHP重置根据图片显示大小及位置重置图片高宽和位置,溢出隐藏
实现功能:
- 重置图片高宽和位置,溢出隐藏
- 保证在规定窗口满窗显示
- 直接在图片style上使用输出
代码实现:
<?php /* 重置图片高宽和位置,溢出隐藏 保证在规定窗口满窗显示 直接在图片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; } ?>
转载请注明:半叶寒羽
» php根据高宽比及显示窗口大小重置图片样式