/lib/php/imgresize.php

https://github.com/matoyan/ConbuMusubi · PHP · 124 lines · 84 code · 20 blank · 20 comment · 13 complexity · 065ebe36230689235ea2df8b3155bfca MD5 · raw file

  1. <?php
  2. # 画像サイズの変換
  3. # サイズが分かる場合保留xsize, ysize
  4. # サイズが分からない場合maxsize
  5. #
  6. # /tmp/yr_imgcache ディレクトリにキャッシュを作成
  7. # ファイルがなければ元ファイルを変換してキャッシュ作成
  8. # 基本的に同じファイル名はつかないはずなのでキャッシュは作らない
  9. #
  10. # キャッシュファイルの命名規則
  11. # maxsize_xxx_dir1-dir2-...dirx-filename.filetype
  12. #
  13. # fname: xoops_homedirからのパス
  14. # maxsize: 縦もしくは横の最大長
  15. # 以下対応未
  16. # x_maxsize: 横の最大長
  17. # y_maxsize: 縦の最大長
  18. # xsize: 横長
  19. # ysize: 縦長
  20. error_reporting(E_ERROR);
  21. $cachedir="/home2/mountrec/public_html/musubi/thumb/";
  22. if(empty($_GET['fname'])){
  23. exit();
  24. }
  25. $fname = "/home2/mountrec/public_html/musubi/data/", $_GET['fname']);
  26. if(!empty($_GET['maxheight'])){
  27. $maxheight=intval($_GET['maxheight']);
  28. }else{
  29. $maxheight=0;
  30. }
  31. if($maxheight>0){
  32. $h="h_".$maxheight."_";
  33. }else{
  34. $h="";
  35. }
  36. if(!empty($_GET['maxsize']) and intval($_GET['maxsize'])>0 and !empty($fname)){
  37. $maxsize=intval($_GET['maxsize']);
  38. $fname=$xoops_homedir.$fname;
  39. $cachename=$cachedir."maxsize_".$maxsize."_".$h.$_GET['fname'];
  40. }else{
  41. exit();
  42. }
  43. if(is_file($cachename)){
  44. print_img($cachename);
  45. exit();
  46. }
  47. list($width, $height, $type, $attr) = getimagesize($fname);
  48. // ファイル有無チェック
  49. if(!is_file($fname)){
  50. exit();
  51. }
  52. // リサイズして保存
  53. $ftype=strrchr($fname, "."); // 拡張子から
  54. if(strcasecmp($ftype, ".png")==0){
  55. $src_id = imagecreatefrompng($fname);
  56. }elseif(strcasecmp($ftype, ".jpg")==0 or strcasecmp($ftype, ".jpeg")==0){
  57. $src_id = imagecreatefromjpeg($fname);
  58. }elseif(strcasecmp($ftype, ".gif")==0){
  59. $src_id = imagecreatefromgif($fname);
  60. }
  61. if($width >= $height){
  62. $th_width = $maxsize;
  63. $th_height = $height * $th_width / $width;
  64. } else {
  65. $th_height = $maxsize;
  66. $th_width = $width * $th_height / $height;
  67. }
  68. if($maxheight>0){
  69. if($th_height>$maxheight){
  70. $th_width = $th_width * $maxheight / $th_height;
  71. $th_height = $maxheight;
  72. }
  73. }
  74. $th_width=intval($th_width);
  75. $th_height=intval($th_height);
  76. $dst_id = imagecreatetruecolor($th_width, $th_height);
  77. imagecopyresampled($dst_id, $src_id, 0, 0, 0, 0, $th_width, $th_height, $width, $height);
  78. if(strcasecmp($ftype, ".png")==0){
  79. imagepng($dst_id, $cachename);
  80. }elseif(strcasecmp($ftype, ".jpg")==0 or strcasecmp($ftype, ".jpeg")==0){
  81. imagejpeg($dst_id, $cachename, 100);
  82. }elseif(strcasecmp($ftype, ".gif")==0){
  83. imagegif($dst_id, $cachename);
  84. }
  85. imagedestroy($src_id);
  86. imagedestroy($dst_id);
  87. print_img($cachename);
  88. exit();
  89. function print_img($imgname){
  90. $ftype=strrchr($imgname, ".");
  91. $type="Content-type: image/";
  92. if(strcasecmp($ftype, ".png")==0){
  93. $type.="png\n\n";
  94. }elseif(strcasecmp($ftype, ".jpg")==0 or strcasecmp($ftype, ".jpeg")==0){
  95. $type.="jpeg\n\n";
  96. }elseif(strcasecmp($ftype, ".gif")==0){
  97. $type.="gif\n\n";
  98. }else{
  99. exit();
  100. }
  101. header($type);
  102. readfile($imgname);
  103. }
  104. ?>