首页 > 语言 > PHP > 正文

php 生成缩略图代码

2024-09-04 11:44:21
字体:大 中 小
来源:转载
供稿:网友

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表