Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. <?php
  2. function get_relative_size ($src_val,$src_opposite,$target_val,$target_opposite) {
  3. if (!$target_opposite) {return $target_val;}
  4. return round($src_val/($src_opposite/$target_opposite));
  5. }
  6.  
  7. function get_orientation ($w,$h) {
  8. return ($w==$h?0:($w>$h?-1:1));
  9. }
  10.  
  11. function resize ($w=0,$h=0,$ow=0,$oh=0,$mode='max',$enlarge=false) {
  12. $rw = NAN;
  13. $rh = NAN;
  14. $w = intval($w);
  15. $h = intval($h);
  16. if ($w==0&&$h==0) {return [$ow,$oh];}
  17.  
  18. $new_orientation = get_orientation($w,$h);
  19. $src_orientation = get_orientation($ow,$oh);
  20.  
  21. //$mode = 'max'; // min|max|stretch
  22.  
  23. if ($mode=='stretch') {
  24. return [$w,$h];
  25. }
  26.  
  27. //$enlarge = true;
  28.  
  29. $rw = get_relative_size($ow,$oh,$w,$h);
  30. $rh = get_relative_size($oh,$ow,$h,$w);
  31.  
  32. $case = (!$h?'width':(!$w?'height':'both'));
  33.  
  34. switch ($case) {
  35. case 'width':$rw = $w;break;
  36. case 'height':$rh = $h;break;
  37. default:
  38. if (($mode=='max'&&$rw<$w)||($mode=='min'&&$rw>$w)) {
  39. $rh = $h;
  40. } else {
  41. $rw = $w;
  42. }
  43.  
  44. }
  45.  
  46. if (!$enlarge) {
  47. if ($rw>$ow||$rh>$oh) {
  48. $rw = $ow;
  49. $rh = $oh;
  50. }
  51. }
  52.  
  53. return [$rw,$rh];
  54. }
  55.  
  56. // enlarge
  57. $enlarges = [false,true];
  58.  
  59. // modes
  60. $modes = ['max','min','stretch'];
  61.  
  62. // resize to
  63. $tests = [
  64. [50,0],
  65. [0,50],
  66. [50,50],
  67. [50,150],
  68. [150,50],
  69. ];
  70.  
  71. // src sizes
  72. $sizes = [
  73. [40,40],
  74. [20,40],
  75. [40,20],
  76.  
  77. [150,150],
  78. [150,250],
  79. [250,150],
  80.  
  81. [200,200],
  82. [200,300],
  83. [300,200],
  84. ];
  85.  
  86. foreach ($modes as $mode) {
  87. foreach ($enlarges as $enlarge) {
  88. $id = $mode.'_'.($enlarge?'up':'d');
  89. 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>';
  90. print '<div id="'.$id.'">';
  91. foreach ($tests as $test) {
  92. foreach ($sizes as $size) {
  93. $r = call_user_func_array('resize',array_merge($test,$size,[$mode],[$enlarge]));
  94. print '<div style="float:left;margin:1em;">';
  95. print "<b>resize:".implode('x',$test).'['.get_orientation($test[0],$test[1]).']</b><br />';
  96. print 'src:'.implode('x',$size).'['.get_orientation($size[0],$size[1]).'] >> <b>'.implode('x',$r).'['.get_orientation($r[0],$r[1]).']</b><br />';
  97.  
  98. print '<div style="position:relative;min-height:300px;min-width:300px;">';
  99. print '<div style="background-color:rgba(255,255,0,1);width:'.$size[0].'px;height:'.$size[1].'px;">';
  100. print '<div style="background-color:rgba(0,255,255,0.4);width:'.$r[0].'px;height:'.$r[1].'px;">';
  101. print '</div></div>';
  102. print '<div style="position:absolute;left:0;top:0;border:1px solid red;width:'.($test[0]-2).'px;height:'.($test[1]-2).'px;"></div>';
  103. print '</div>';
  104. print '</div>';
  105. }
  106. print '<hr style="clear:both" />';
  107. }
  108. print '</div>';
  109. }
  110. }
  111.  
  112. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement