<?

// Show the source code
show_source(__FILE__); exit;

function 
createThumb($img$w 100){
    
// Get the extension
    
if (eregi("(gif|jpg|jpeg|png)$"$img$extension)) {
        
// Store the extension in $type
        
$type strtolower($extension[0]);

        
// Catch JPEG extensions
        
if ($type == "jpeg"$type "jpg";

        
// Determine which function to call
        
switch($type) {
            case 
"jpg" $image = @imagecreatefromjpeg($img); break;
            case 
"gif" $image = @imagecreatefromgif($img); break;
            case 
"png" $image = @imagecreatefrompng($img); break;
        }
    } else {
        return 
false;
    }

    
// Image resource creation failed, return false
    
if (!$image) return false;

    
// Calculate the thumbnail's dimensions
    
$currentWidth imagesx($image);
    
$currentHeight imagesy($image);
    
$ratio $currentWidth $currentHeight;
    
$thumbWidth $w;
    
$thumbHeight ceil($thumbWidth/$ratio);
    
    
// When the imagegif function is unavailable, create a JPG instead
    // by changing $type to 'jpg'
    
if (!function_exists("imagegif") && $type == "gif"$type "jpg";
    
    
// Determine the thumbnail's name
    
$thumbName substr($img0strrpos($img"."))."_thumb.".$type;
    
$thumb imagecreatetruecolor ($thumbWidth$thumbHeight);

    
imagecopyresized ($thumb$image0000$thumbWidth$thumbHeight$currentWidth$currentHeight);
    
    
// Determine which function to use
    
switch($type) {
        case 
"gif" imagegif($thumb$thumbName); break;
        case 
"jpg" imagejpeg($thumb$thumbName); break;
        case 
"png" imagepng($thumb$thumbName); break;
    }

    
// Success, return the thumbnail's name
    
return $thumbName;
}

// Handle the uploaded file
if (isset($_FILES['file'])) {
    if (!
$_FILES['file']['error']) {
        
// Replace whitespaces with underscores
        
$name str_replace(" ""_"$_FILES['file']['name']);
        
$path "../temp/";
        
$img $path.$name;

        
// Copy the uploaded file to the destinated location
        
move_uploaded_file($_FILES['file']['tmp_name'], $img);
        
        
// Set the permissions of the uploaded image
        
chmod($img0644);

        
// Call the function
        
if (!$thumb createThumb($img)) {
            echo 
"An error occured during thumbnail creation";
        } else {
            
// Set the permissions of the generated thumbnail
            
chmod($thumb0644);
        }
        
    } else {
        echo 
"An error occured during uploading";
    }
}

?>
<html>
<head>
    <title> New Document </title>
</head>

<body>
    <form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit">
    </form>
    <?
        
// Display the contents of the output dir
        
$d opendir("../temp");

        while (
$f readdir($d)) {
            if (!
eregi("(gif|jpg|jpeg|png)$"$f)) continue;
            echo 
"<img src='../temp/$f'><br />";
        }
    
?>
</body>
</html>