Просмотр файла resize_img.php

Размер файла: 2.34Kb
  1. <?php
  2. require('require/configuration.php');
  3. if (!isset($_GET['img']))
  4. {
  5. exit;
  6. }
  7. #################
  8. #################
  9. $image='datapic/'.$_GET['img'];
  10. if(!file_exists($image)){
  11. exit;
  12. }
  13. $ext=strtolower(substr($_GET['img'],strrpos($_GET['img'],'.')+1));
  14. if($ext=='jpg'){
  15. $ext='jpeg';
  16. }
  17. switch($ext)
  18. {
  19. case('jpeg'):
  20. $size=getimagesize($image);
  21. $width=$size[0];
  22. $height=$size[1];
  23.  
  24. $x_ratio=$max_width/$width;
  25. $y_ratio=$max_height/$height;
  26.  
  27. if(($width<=$max_width)&&($height<=$max_height)){
  28. $tn_width=$width;
  29. $tn_height=$height;
  30. }elseif(($x_ratio*$height)<$max_height){
  31. $tn_height=ceil($x_ratio*$height);
  32. $tn_width=$max_width;
  33. }else{
  34. $tn_width=ceil($y_ratio*$width);
  35. $tn_height=$max_height;
  36. }
  37.  
  38. $src=imagecreatefromjpeg($image);
  39. $dst=imagecreatetruecolor($tn_width,$tn_height);
  40. imagecopyresized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height);
  41. header('Content-type: image/jpeg');
  42. imagejpeg($dst, null, 100);
  43. imagedestroy($src);
  44. imagedestroy($dst);
  45. break;
  46. #################
  47. #################
  48. case('gif'):
  49. $size=getimagesize($image);
  50. $width=$size[0];
  51. $height=$size[1];
  52.  
  53. $x_ratio=$max_width/$width;
  54. $y_ratio=$max_height/$height;
  55. if(($width<=$max_width)&&($height<=$max_height)){
  56. $tn_width=$width;
  57. $tn_height=$height;
  58. }elseif(($x_ratio*$height)<$max_height){
  59. $tn_height=ceil($x_ratio*$height);
  60. $tn_width=$max_width;
  61. }else{$tn_width=ceil($y_ratio*$width);
  62. $tn_height=$max_height;
  63. }
  64. $src=imagecreatefromgif($image);
  65. $dst=imagecreatetruecolor($tn_width,$tn_height);
  66. imagecopyresized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height);
  67. header('Content-type: image/gif');
  68. imagegif($dst);
  69. imagedestroy($src);
  70. imagedestroy($dst);
  71. break;
  72. #################
  73. #################
  74. case('png'):
  75. $size=getimagesize($image);
  76. $width=$size[0];
  77. $height=$size[1];
  78.  
  79. $x_ratio=$max_width/$width;
  80. $y_ratio=$max_height/$height;
  81.  
  82. if(($width<=$max_width)&&($height<=$max_height)){
  83. $tn_width=$width;
  84. $tn_height=$height;
  85. }elseif(($x_ratio*$height)<$max_height){
  86. $tn_height=ceil($x_ratio*$height);
  87. $tn_width=$max_width;
  88. }else{
  89. $tn_width=ceil($y_ratio*$width);
  90. $tn_height=$max_height;
  91. }
  92. $src=imagecreatefrompng($image);
  93. $dst=imagecreatetruecolor($tn_width,$tn_height);
  94. imagecopyresized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height);
  95. imagecolortransparent($dst, '0');
  96. header('Content-type: image/png');
  97. imagepng($dst);
  98. imagedestroy($src);
  99. imagedestroy($dst);
  100. break;
  101. }
  102. ?>