ホームページ  >  記事  >  バックエンド開発  >  リクエストに基づいてサムネイル画像を生成し、Linux 画像サーバーに保存する PHP コード

リクエストに基づいてサムネイル画像を生成し、Linux 画像サーバーに保存する PHP コード

WBOY
WBOYオリジナル
2016-07-25 08:59:19833ブラウズ
  1. /**
  2. * サムネイル画像 Linux 画像サーバー
  3. * bbs.it-home.org を編集
  4. */
  5. $picID=$_GET['imgID'];
  6. $picTypes=".".$_GET['imgType'];
  7. $picWidth= $_GET['ImgWidth'];
  8. if($picID!="")
  9. {
  10. //リクエストは小さい画像です
  11. if($picWidth>0){
  12. //小さい画像が存在する場合
  13. if( file_exists($ picID."_".$picWidth.$picTypes))
  14. {
  15. OutputImg($picID."_".$picWidth.$picTypes)
  16. }else
  17. {
  18. if(file_exists($picID."_0) ".$ picTypes)){
  19. //サムネイルが存在しない場合は、直接サムネイルを生成します
  20. sizeImg($picID."_0".$picTypes,$picWidth,$picWidth,$picID."_".$picWidth. $picTypes);
  21. //出力
  22. OutputImg($picID."_".$picWidth.$picTypes);
  23. }else
  24. {
  25. //大きい画像が存在しない場合
  26. sizeImg('noDefaultImage.gif',$) picWidth,$picWidth,noDefaultImage. "_".$picWidth.".gif");
  27. //出力
  28. outputImg($picID."_".$picWidth.$picTypes);ファイル内に大きな画像が存在するかどうかを確認します
  29. if(file_exists($picID."_0".$picTypes))
  30. {
  31. $img_file = $picID."_0".$picTypes
  32. OutputImg($img_file) }
  33. else
  34. {
  35. //画像が存在しない場合
  36. $img_file = 'noDefaultImage.gif';
  37. OutputImg($img_file);
  38. }
  39. }
  40. // 画像を出力する
  41. function OutputImg($img_file)
  42. {
  43. $ fp = fopen($img_file, 'rb');
  44. $content = fread($fp, filesize($img_file)) //バイナリデータ
  45. fclose($fp); ');
  46. echo $content;
  47. }
  48. / **
  49. * サムネイルを生成します
  50. * $srcName---- は元の画像パスです
  51. * $newWidth、$newHeight---- はそれぞれサムネイルの最大幅と高さです
  52. * $newName---- はサムネイルファイルです名前 (パスを含む)
  53. * @param string $srcName
  54. * @param int $newWidth
  55. * @param int $newHeight
  56. * @param string $newName
  57. * return viod
  58. */
  59. 関数 raiseImg($srcName,$newWidth,$newHeight,$newName="")
  60. {
  61. if($newName=="")
  62. {
  63. $nameArr=explode('.', $srcName);
  64. $expName=$expName;
  65. $newName = implode(' .',$nameArr);
  66. }
  67. $info = "";
  68. $data = getimagesize($srcName,$info);
  69. {
  70. case 1:
  71. if(!function_exists() "imagecreatefromgif")){
  72. echo "あなたの GD ライブラリでは GIF 形式の画像を使用できません。Jpeg または PNG 形式を使用してください。 return ";
  73. exit();
  74. }
  75. $im = ImageCreateFromGIF($srcName);
  76. Break;
  77. ケース 2:
  78. if(!function_exists("imagecreatefromjpeg")){
  79. echo "GD ライブラリでは jpeg 形式を使用できません写真は他の形式で使用してください。戻り";
  80. exit();
  81. }
  82. $im = ImageCreateFromJpeg($srcName);
  83. ブレーク;
  84. ケース 3:
  85. $im = ImageCreateFromPNG($srcName);
  86. ブレーク;
  87. }
  88. $srcW=ImageSX($im );
  89. $srcH=$newWidth/$newHeight;
  90. if($newWidthH $ftoW=$newWidth;
  91. $ftoH=$ftoW*($srcH/$srcW);
  92. }
  93. else{
  94. $ftoH=$ftoH*($srcW/$srcH);
  95. if($srcW> $newWidth||$srcH>$newHeight)
  96. {
  97. if(function_exists("imagecreatetruecolor"))
  98. {
  99. @$ni = ImageCreateTrueColor($ftoW,$ftoH)
  100. if($ni) ImageCopyResampled($ni,$); im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  101. else
  102. {
  103. $ni=ImageCreate($ftoW,$ftoH);
  104. ImageCopyResize($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  105. }
  106. }
  107. else
  108. {
  109. $ni=ImageCreate($ftoW,$ftoH);
  110. ImageCopyResize($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  111. }
  112. if(function_exists('imagejpeg')) ImageJpeg($ni,$newName);
  113. else ImagePNG($ni,$newName);
  114. ImageDestroy($ni);
  115. }
  116. ImageDestroy($im);
  117. }
  118. ?>
  119. 复制代
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。