php 生成缩略图代码

这是一款经典实用的生成小图的php代码,有专业素语来讲就是php 生成缩略图代码,实例代码如下:

  1. # 显示图形及连接
  2. function showdir ($adirectory, $i)
  3. {
  4. global $browsedir;
  5. $start = $i;
  6. # 更改 $maxcols 及 $maximages 可改变每一页显示的小图的行数与列数。
  7. $maxcols = 2;
  8. $maximages = 6;
  9. $maximages = $i + ($maximages - 3);
  10. # 更改 $imagemaxwidth 及 $imagemaxheight 可改变显示小图的宽度与高度。
  11. $imagemaxwidth = 100;
  12. $imagemaxheight = 100;
  13. # 计算高度与宽度的比例。
  14. $imagemaxratio = $imagemaxwidth / $imagemaxheight;
  15. $ndirectory = sizeof ($adirectory);
  16. echo (table_start);
  17. for ($i; $i<=$maximages;)
  18. {
  19. echo (row_start);
  20. for ($icols=1; $icols<=$maxcols; $icols++)
  21. {
  22. echo (col_start);
  23. $imagefilename = $adirectory[++$i];
  24. if (strlen($imagefilename)>0)
  25. {
  26. $imagepath = $browsedir."/".$imagefilename;
  27. $imagesize = getimagesize ($imagepath);
  28. if ($imagesize)
  29. {
  30. $imagewidth = $imagesize[0];
  31. $imageheight = $imagesize[1];
  32. $imageratio = $imagewidth / $imageheight;
  33. if ($imageratio > $imagemaxratio)
  34. {
  35. $imageoutputwidth = $imagemaxwidth;
  36. $imageoutputheight = ceil ($imagemaxwidth/$imagewidth*$imageheight);
  37. }
  38. else if ($imageratio < $imagemaxratio)
  39. {
  40. $imageoutputheight = $imagemaxheight;
  41. $imageoutputwidth = ceil ($imagemaxheight/$imageheight*$imagewidth);
  42. } else
  43. {
  44. $imageoutputwidth = $imagemaxwidth;
  45. $imageoutputheight = $imagemaxheight;
  46. }
  47. # 显示图形
  48. echo (a_start.$imagepath.a_close);
  49. echo (img_start.$imagepath.img_width.$imageoutputwidth.img_height.$imageoutputheight.img_end);
  50. echo (line_break.$adirectory[$i]);
  51. echo (a_end);
  52. }
  53. echo (col_end);
  54. }
  55. }
  56. echo (row_end);
  57. }
  58. echo (table_end);
  59. pagemenu ($browsedir, $ndirectory, $start);
  60. }
  61. function pagemenu ($browsedir, $ndirectory, $pg) {
  62. echo "<br><center><font size="1" color="#000033">page:";
  63. $pg_num = 1;
  64. //开源代码phpfensi.com
  65. for ($img_num = 0; $img_num <= $ndirectory;) {
  66. if ($pg == $img_num) {
  67. echo "<span class="menulink_1"><a href="thumb.php?browsedir=$browsedir&start=$img_num"> *$pg_num</a> <span>";
  68. } else {
  69. echo "<span class="menulink_2"><a href="thumb.php?browsedir=$browsedir&start=$img_num"> $pg_num</a> <span>";
  70. }
  71. # 建立其他页次的连接, 每页显示四张图, 故页数 $pg_num 每加 1 , $img_num 就加 4 。
  72. $pg_num = $pg_num + 1;
  73. $img_num = $img_num + 4;
  74. }
  75. echo "</font> ";
  76. }
  77. function dirtoarray ($browsedir, $extensions)
  78. {
  79. $nextensions = sizeof ($extensions);
  80. $idirectory = 0;
  81. $directory = dir($browsedir);
  82. while ($entry = $directory->read())
  83. {
  84. for ($i=1; $i<=$nextensions; $i++)
  85. {
  86. $compare = stristr ($entry, $extensions[$i]);
  87. if (strlen($compare) == strlen($extensions[$i]))
  88. {
  89. $adirectory[++$idirectory] = $entry;
  90. break;
  91. }
  92. }
  93. }
  94. $directory->close();
  95. return $adirectory;
  96. }
  97. #主程序
  98. #变量 $browsedir 为图形文件放置的位置。
  99. $browsedir="./images";
  100. # 允许浏览的图形文件扩展名, 放置於数组中, 可自行增加。
  101. $extensions[1] = "jpeg";
  102. $extensions[2] = "jpg";
  103. $extensions[3] = "gif";
  104. $extensions[4] = "png";
  105. showdir (dirtoarray ($browsedir, $extensions), $start);
  106. define ("line_break", "<br>");
  107. define ("table_start", "<table width=600> ");
  108. define ("table_end", "</table> ");
  109. define ("row_start", " <tr> ");
  110. define ("row_end", " </tr> ");
  111. define ("col_start", " <td align=center> ");
  112. define ("col_end", " </td> ");
  113. define ("img_start", "<img src=");
  114. define ("img_end", ">");
  115. define ("img_width", " width=");
  116. define ("img_height", " height=");
  117. define ("a_start", '<a href="');
  118. define ("a_close", '">');
  119. define ("a_end", "</a>");