[795] | 1 | <?php |
---|
| 2 | /*********************************************************************** |
---|
| 3 | ** Title.........: Thumbnail generator, with cache. |
---|
| 4 | ** Version.......: 1.0 |
---|
| 5 | ** Author........: Xiang Wei ZHUO <wei@zhuo.org> |
---|
| 6 | ** Filename......: thumbs.php |
---|
| 7 | ** Last changed..: 1 Mar 2003 |
---|
| 8 | ** Notes.........: Configuration in config.inc.php |
---|
| 9 | |
---|
| 10 | - if the thumbnail does not exists or the source |
---|
| 11 | file is newer, create a new thumbnail. |
---|
| 12 | |
---|
| 13 | ***********************************************************************/ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | include 'config.inc.php'; |
---|
| 17 | require_once '../ImageEditor/Transform.php'; |
---|
| 18 | |
---|
| 19 | $img = $BASE_DIR.urldecode($_GET['img']); |
---|
| 20 | |
---|
| 21 | if(is_file($img)) { |
---|
| 22 | make_thumbs(urldecode($_GET['img'])); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | function make_thumbs($img) |
---|
| 27 | { |
---|
| 28 | global $BASE_DIR, $BASE_URL; |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | $path_info = pathinfo($img); |
---|
| 32 | $path = $path_info['dirname']."/"; |
---|
| 33 | $img_file = $path_info['basename']; |
---|
| 34 | |
---|
| 35 | $thumb = $path.'.'.$img_file; |
---|
| 36 | |
---|
| 37 | $img_info = getimagesize($BASE_DIR.$path.$img_file); |
---|
| 38 | $w = $img_info[0]; $h = $img_info[1]; |
---|
| 39 | |
---|
| 40 | $nw = 96; $nh = 96; |
---|
| 41 | |
---|
| 42 | if($w <= $nw && $h <= $nh) { |
---|
| 43 | header('Location: '.$BASE_URL.$path.$img_file); |
---|
| 44 | exit(); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | if(is_file($BASE_DIR.$thumb)) { |
---|
| 48 | |
---|
| 49 | $t_mtime = filemtime($BASE_DIR.$thumb); |
---|
| 50 | $o_mtime = filemtime($BASE_DIR.$img); |
---|
| 51 | |
---|
| 52 | if($t_mtime > $o_mtime) { |
---|
| 53 | //echo $BASE_URL.$path.'.'.$img_file; |
---|
| 54 | header('Location: '.$BASE_URL.$path.'.'.$img_file); |
---|
| 55 | exit(); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | $img_thumbs = Image_Transform::factory(IMAGE_CLASS); |
---|
| 60 | $img_thumbs->load($BASE_DIR.$path.$img_file); |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | if ($w > $h) |
---|
| 64 | $nh = unpercent(percent($nw, $w), $h); |
---|
| 65 | else if ($h > $w) |
---|
| 66 | $nw = unpercent(percent($nh, $h), $w); |
---|
| 67 | |
---|
| 68 | $img_thumbs->resize($nw, $nh); |
---|
| 69 | |
---|
| 70 | $img_thumbs->save($BASE_DIR.$thumb); |
---|
| 71 | $img_thumbs->free(); |
---|
| 72 | |
---|
| 73 | chmod($BASE_DIR.$thumb, 0666); |
---|
| 74 | |
---|
| 75 | if(is_file($BASE_DIR.$thumb)) { |
---|
| 76 | //echo "Made:".$BASE_URL.$path.'.'.$img_file; |
---|
| 77 | header('Location: '.$BASE_URL.$path.'.'.$img_file); |
---|
| 78 | exit(); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | function percent($p, $w) |
---|
| 82 | { |
---|
| 83 | return (real)(100 * ($p / $w)); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | function unpercent($percent, $whole) |
---|
| 87 | { |
---|
| 88 | return (real)(($percent * $whole) / 100); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | ?> |
---|