首頁  >  文章  >  後端開發  >  php 根據請求產生縮圖儲存到Linux圖片伺服器的程式碼

php 根據請求產生縮圖儲存到Linux圖片伺服器的程式碼

WBOY
WBOY原創
2016-07-25 08:59:19833瀏覽
  1. /**
  2. * 縮圖 Linux圖片伺服器
  3. * edit 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. resizeImg($picID."_0".$picTypes,$picWidth,$picWidth,$picID."_".$picWidth.$picTypes);
  21. //輸出
  22. outputImg($picID."_".$picWidth.$picTypes);
  23. }else
  24. {
  25. //如果大圖不存在
  26. resizeImg('noDefaultImage.gif',$picWidthth,$picWidth, $picWidth,noDefaultImage."_".$picWidth.".gif");
  27. //輸出
  28. outputImg($picID."_".$picWidth.$picTypes);
  29. }
  30. }
  31. }
  32. //判斷檔案是否存在大圖
  33. if(file_exists($picID."_0".$picTypes))
  34. {
  35. $img_file = $picID."_0" .$picTypes;
  36. outputImg($img_file);
  37. }
  38. else
  39. {
  40. //如果不存在圖片
  41. $img_file = 'noDefaultImage. //如果不存在圖片
  42. $img_file = 'noDefaultImage.gif'; ($img_file);
  43. }
  44. }
  45. //輸出圖片
  46. function outputImg($img_file)
  47. {
  48. $fp = fopen($img_file, 'rb');
  49. $content = fread($fp, filesize($img_file)); //二進位資料
  50. fclose($fp);
  51. header('Content-Type: image/gif');
  52. echocho> header('Content-Type: image/gif');
  53. echocho $content;
  54. }
  55. /**
  56. * 產生縮圖
  57. * $srcName----為原圖片路徑
  58. * $newWidth,$newHeight----分別縮圖的最大寬,高
  59. * $newName-- --為縮圖檔案名稱(含路徑)
  60. * @param string $srcName
  61. * @param int $newWidth
  62. * @param int $newHeight
  63. * @param string $newName
  64. * return viod
  65. */
  66. function resizeImg($srcName,$newWidth,$newHeight,$newName="")
  67. {
  68. if($newName =="")
  69. {
  70. $nameArr=explode('.',$srcName);
  71. $expName=array_pop($nameArr);
  72. $expName=$expName;
  73. array_push ($nameArr,$expName);
  74. $newName = implode('.',$nameArr);
  75. }
  76. $info = "";
  77. $data = getimagesize($srcName,$info );
  78. switch ($data[2])
  79. {
  80. case 1:
  81. if(!function_exists("imagecreatefromgif")){
  82. echo "你的GD庫不能使用GIF格式的圖片,請使用Jpeg或PNG格式!回傳";
  83. exit();
  84. }
  85. $im = ImageCreateFromGIF($srcName);
  86. break;
  87. case 2:
  88. if(!function_exists("jpage)) { echo "你的GD庫不能使用jpeg格式的圖片,請使用其它格式的圖片!首選項";
  89. exit();
  90. }
  91. $im = ImageCreateFromJpeg($srcName); 🎜>
  92. }
  93. $srcW=ImageSY($im);
  94. if🎜> $srcW=ImageSY($im);
  95. if $ newWidthH $ftoW = $ newWidth ; $ftoH=$newHeight;
  96. $ftoW=$ftoH*($srcW/$srcH) 🎜> if(function_exists($srcW/$srcH) 🎜> if(function_exists("color))
  97. { if(function_exists('jpimageage '); )) ImageJpeg($ni,$newName);
  98. }
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn