Blog » 脚本收藏 » AutoIndex 的缩略图显示函数

AutoIndex 的缩略图显示函数

作者: Justin Hagstrom
来源: AutoIndex 1.5.4

这里面有个获取扩展名的函数,还不错

<?php
 
//显示缩略图核心函数
function display_thumbnail($file, $thumbnail_height)
{
    
global $html_heading;
    
if (!@is_file($file))
    
{
        
header('HTTP/1.0 404 Not Found');
        
die("$html_heading<p>File not found: <em>".htmlentities($file).'</em></p>');
    
}
    
switch (ext($file))
    
{
        
case 'gif':
            
$src = @imagecreatefromgif($file);
            
break;
        
case 'jpeg':
        
case 'jpg':
        
case 'jpe':
            
$src = @imagecreatefromjpeg($file);
            
break;
        
case 'png':
            
$src = @imagecreatefrompng($file);
            
break;
        
default:
            
die("$html_heading<p>Unsupported file extension.</p>");
    
}
    
if ($src === false)
    
{
        
die("$html_heading<p>Unsupported image type.</p>");
    
}
    
    
header('Content-Type: image/jpeg');
    
header('Cache-Control: public, max-age=3600, must-revalidate');
    
header('Expires: '.gmdate('D, d M Y H:i:s', time()+3600).' GMT');
    
$src_height = imagesy($src);
    
if ($src_height <= $thumbnail_height)
    
{
        
imagejpeg($src, '', 100);
    
}
    
else
    
{
        
$src_width = imagesx($src);
        
$thumb_width = $thumbnail_height * ($src_width / $src_height);
        
$thumb = imagecreatetruecolor($thumb_width, $thumbnail_height);
        
imagecopyresampled($thumb, $src, 0, 0, 0, 0, $thumb_width,
            
$thumbnail_height, $src_width, $src_height);
        
imagejpeg($thumb, '', 100);
        
imagedestroy($thumb);
    
}
    
imagedestroy($src);
    
die();
}
 
//获取文件扩展名
function ext($fn)
//return the lowercase file extension of $fn, not including the leading dot
{
    
$fn = get_basename($fn);
    
return (strpos($fn, '.') ? strtolower(substr(strrchr($fn, '.'), 1)) : '');
}
 
//获取完整文件名
function get_basename($fn)
//returns everything after the slash, or the original string if there is no slash
{
    
return basename(str_replace('\\', '/', $fn));
}
 
//调用方式 参数(图片名,缩略图最大高度)
display_thumbnail('newwebpick.com.jpg', '200');
 
?>

21andy.com相关文章

本文地址: http://www.21andy.com/blog/20070112/521.html

发表评论