PHP实现缩略图的生成

对于网站前端上传的图片,在后台处理时有必要对其进行缩放以生成大小统一的缩略图。在PHP中,可以很方便的使用GD库来完成这一任务。下面的CImage类的静态方法CreateThumbnail()函数可以接受原图像文件名称,缩略图宽高,生成的缩略图文件名称等参数来生成保持原图纵横比的缩略图。函数基本上体现了PHP生成缩略图的方法,功能上还可以作进一步的扩充,如根据缩略图文件名称来生成指定格式的缩略图。

完整代码如下(Win7+XAMPP2.5及Linuxdev 2.6.18-128+apache测试通过):

 1 //by MoreWindows (http://blog.csdn.net/MoreWindows )
 2 class CImage
 3 {
 4     /**
 5      * 生成保持原图纵横比的缩略图,支持.png .jpg .gif
 6      * 缩略图类型统一为.png格式
 7      * $srcFile     原图像文件名称
 8      * $toW         缩略图宽
 9      * $toH         缩略图高
10      * $toFile      缩略图文件名称,为空覆盖原图像文件
11      * @return bool    
12     */
13     public static function CreateThumbnail($srcFile, $toW, $toH, $toFile="") 
14     {
15         if ($toFile == "")
16         { 
17                $toFile = $srcFile; 
18         }
19         $info = "";
20         //返回含有4个单元的数组,0-宽,1-高,2-图像类型,3-宽高的文本描述。
21         //失败返回false并产生警告。
22         $data = getimagesize($srcFile, $info);
23         if (!$data)
24             return false;
25         
26         //将文件载入到资源变量im中
27         switch ($data[2]) //1-GIF,2-JPG,3-PNG
28         {
29         case 1:
30             if(!function_exists("imagecreatefromgif"))
31             {
32                 echo "the GD can't support .gif, please use .jpeg or .png! <a href='javascript:history.back();'>back</a>";
33                 exit();
34             }
35             $im = imagecreatefromgif($srcFile);
36             break;
37             
38         case 2:
39             if(!function_exists("imagecreatefromjpeg"))
40             {
41                 echo "the GD can't support .jpeg, please use other picture! <a href='javascript:history.back();'>back</a>";
42                 exit();
43             }
44             $im = imagecreatefromjpeg($srcFile);
45             break;
46               
47         case 3:
48             $im = imagecreatefrompng($srcFile);    
49             break;
50         }
51         
52         //计算缩略图的宽高
53         $srcW = imagesx($im);
54         $srcH = imagesy($im);
55         $toWH = $toW / $toH;
56         $srcWH = $srcW / $srcH;
57         if ($toWH <= $srcWH) 
58         {
59             $ftoW = $toW;
60             $ftoH = (int)($ftoW * ($srcH / $srcW));
61         }
62         else 
63         {
64             $ftoH = $toH;
65             $ftoW = (int)($ftoH * ($srcW / $srcH));
66         }
67         
68         if (function_exists("imagecreatetruecolor")) 
69         {
70             $ni = imagecreatetruecolor($ftoW, $ftoH); //新建一个真彩色图像
71             if ($ni) 
72             {
73                 //重采样拷贝部分图像并调整大小 可保持较好的清晰度
74                 imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
75             } 
76             else 
77             {
78                 //拷贝部分图像并调整大小
79                 $ni = imagecreate($ftoW, $ftoH);
80                 imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
81             }
82         }
83         else 
84         {
85             $ni = imagecreate($ftoW, $ftoH);
86             imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
87         }
88 
89         //保存到文件 统一为.png格式
90         imagepng($ni, $toFile); //以 PNG 格式将图像输出到浏览器或文件
91         ImageDestroy($ni);
92         ImageDestroy($im);
93         return true;
94     }
95 }

原文链接