php 根据请求生成缩略图片保存到Linux图片服务器的代码

WBOY
Lepaskan: 2016-07-25 08:59:19
asal
826 orang telah melayarinya
  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',$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.gif';
  42. outputImg($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. echo $content;
  53. }
  54. /**
  55. * 生成缩略图
  56. * $srcName----为原图片路径
  57. * $newWidth,$newHeight----分别缩略图的最大宽,高
  58. * $newName----为缩略图文件名(含路径)
  59. * @param string $srcName
  60. * @param int $newWidth
  61. * @param int $newHeight
  62. * @param string $newName
  63. * return viod
  64. */
  65. function resizeImg($srcName,$newWidth,$newHeight,$newName="")
  66. {
  67. if($newName=="")
  68. {
  69. $nameArr=explode('.',$srcName);
  70. $expName=array_pop($nameArr);
  71. $expName=$expName;
  72. array_push($nameArr,$expName);
  73. $newName = implode('.',$nameArr);
  74. }
  75. $info = "";
  76. $data = getimagesize($srcName,$info);
  77. switch ($data[2])
  78. {
  79. case 1:
  80. if(!function_exists("imagecreatefromgif")){
  81. echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回";
  82. exit();
  83. }
  84. $im = ImageCreateFromGIF($srcName);
  85. break;
  86. case 2:
  87. if(!function_exists("imagecreatefromjpeg")){
  88. echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回";
  89. exit();
  90. }
  91. $im = ImageCreateFromJpeg($srcName);
  92. break;
  93. case 3:
  94. $im = ImageCreateFromPNG($srcName);
  95. break;
  96. }
  97. $srcW=ImageSX($im);
  98. $srcH=ImageSY($im);
  99. $newWidthH=$newWidth/$newHeight;
  100. $srcWH=$srcW/$srcH;
  101. if($newWidthH $ftoW=$newWidth;
  102. $ftoH=$ftoW*($srcH/$srcW);
  103. }
  104. else{
  105. $ftoH=$newHeight;
  106. $ftoW=$ftoH*($srcW/$srcH);
  107. }
  108. if($srcW>$newWidth||$srcH>$newHeight)
  109. {
  110. if(function_exists("imagecreatetruecolor"))
  111. {
  112. @$ni = ImageCreateTrueColor($ftoW,$ftoH);
  113. if($ni) ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  114. else
  115. {
  116. $ni=ImageCreate($ftoW,$ftoH);
  117. ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  118. }
  119. }
  120. else
  121. {
  122. $ni=ImageCreate($ftoW,$ftoH);
  123. ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
  124. }
  125. if(function_exists('imagejpeg')) ImageJpeg($ni,$newName);
  126. else ImagePNG($ni,$newName);
  127. ImageDestroy($ni);
  128. }
  129. ImageDestroy($im);
  130. }
  131. ?>
复制代码


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!