Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function get_relative_size ($src_val,$src_opposite,$target_val,$target_opposite) {
- if (!$target_opposite) {return $target_val;}
- return round($src_val/($src_opposite/$target_opposite));
- }
- function get_orientation ($w,$h) {
- return ($w==$h?0:($w>$h?-1:1));
- }
- function resize ($w=0,$h=0,$ow=0,$oh=0,$mode='max',$enlarge=false) {
- $rw = NAN;
- $rh = NAN;
- $w = intval($w);
- $h = intval($h);
- if ($w==0&&$h==0) {return [$ow,$oh];}
- $new_orientation = get_orientation($w,$h);
- $src_orientation = get_orientation($ow,$oh);
- //$mode = 'max'; // min|max|stretch
- if ($mode=='stretch') {
- return [$w,$h];
- }
- //$enlarge = true;
- $rw = get_relative_size($ow,$oh,$w,$h);
- $rh = get_relative_size($oh,$ow,$h,$w);
- $case = (!$h?'width':(!$w?'height':'both'));
- switch ($case) {
- case 'width':$rw = $w;break;
- case 'height':$rh = $h;break;
- default:
- if (($mode=='max'&&$rw<$w)||($mode=='min'&&$rw>$w)) {
- $rh = $h;
- } else {
- $rw = $w;
- }
- }
- if (!$enlarge) {
- if ($rw>$ow||$rh>$oh) {
- $rw = $ow;
- $rh = $oh;
- }
- }
- return [$rw,$rh];
- }
- // enlarge
- $enlarges = [false,true];
- // modes
- $modes = ['max','min','stretch'];
- // resize to
- $tests = [
- [50,0],
- [0,50],
- [50,50],
- [50,150],
- [150,50],
- ];
- // src sizes
- $sizes = [
- [40,40],
- [20,40],
- [40,20],
- [150,150],
- [150,250],
- [250,150],
- [200,200],
- [200,300],
- [300,200],
- ];
- foreach ($modes as $mode) {
- foreach ($enlarges as $enlarge) {
- $id = $mode.'_'.($enlarge?'up':'d');
- print '<div style="clear:both;padding:1em;border:1px solid blue;" onclick="var image=document.getElementById(\''.$id.'\');image.style.display=(image.style.display==\'none\')?\'block\':\'none\';">'.$mode.' '.($enlarge?'^':'').'</div>';
- print '<div id="'.$id.'">';
- foreach ($tests as $test) {
- foreach ($sizes as $size) {
- $r = call_user_func_array('resize',array_merge($test,$size,[$mode],[$enlarge]));
- print '<div style="float:left;margin:1em;">';
- print "<b>resize:".implode('x',$test).'['.get_orientation($test[0],$test[1]).']</b><br />';
- print 'src:'.implode('x',$size).'['.get_orientation($size[0],$size[1]).'] >> <b>'.implode('x',$r).'['.get_orientation($r[0],$r[1]).']</b><br />';
- print '<div style="position:relative;min-height:300px;min-width:300px;">';
- print '<div style="background-color:rgba(255,255,0,1);width:'.$size[0].'px;height:'.$size[1].'px;">';
- print '<div style="background-color:rgba(0,255,255,0.4);width:'.$r[0].'px;height:'.$r[1].'px;">';
- print '</div></div>';
- print '<div style="position:absolute;left:0;top:0;border:1px solid red;width:'.($test[0]-2).'px;height:'.($test[1]-2).'px;"></div>';
- print '</div>';
- print '</div>';
- }
- print '<hr style="clear:both" />';
- }
- print '</div>';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement