-
-
- RewriteEngine on
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteRule ^(.*)$ index.php?thumb=$1 [L,QSA]
复制代码
新建缩略图生成脚本:
新建 yoursite.com/thumbs/index.php
-
- $thumb = $_GET['thumb'];
- if (!$thumb) {
- exit;
- }
- //
- $thumb_array = explode('.',$thumb);
- $image = '../';
- foreach($thumb_array as $k=>$thumb_part){
- if ($k != count($thumb_array)-2) {
- $image .= $thumb_part . '.';
- }
- }
- $image = substr($image,0,-1);
- list($width,$height) = explode('x',$thumb_array[count($thumb_array)-2]);
- //
- if (file_exists($image)) {
- require('../phpthumb/phpthumb.class.php');
- $phpThumb = new phpThumb();
- $phpThumb->setSourceFilename($image);
- $phpThumb->setParameter('w',$width);
- $phpThumb->setParameter('h',$height);
- //$phpThumb->setParameter('far','C'); // scale outside
- //$phpThumb->setParameter('bg','FFFFFF'); // scale outside
- if ($phpThumb->GenerateThumbnail()) {
- mkdir(dirname($thumb),0777,true);
- if ($phpThumb->RenderToFile($thumb)) {
- header('Location: /thumbs/'.$thumb);
- exit;
- }
- }
- }
复制代码
测试缩略图生成:
上传一张图片到 yoursite.com/images/myimage.jpg
打开浏览器,访问 yoursite.com/thumbs/images/myimage.100×100.jpg
检查 thumbs 目录,应该有个缩略图在那,下次访问就不用调 PHP 啦。
官方网站 http://phpthumb.gxdlabs.com/
|