<?php
function resampleImage($src$dst$new_width 150$new_height 100)
{
    list(
$width$height$type) = getimagesize($src); // Get image properties

    
if (!$src imagecreatefromstring(file_get_contents($src))) // Attempt to create the source image
        
return false;

    if (
$width $new_width || $height $new_height// Does it need resized?
    
{
        if (
$width $height)
            
$new_width = ($new_height $height) * $width;
        else
            
$new_height = ($new_width $width) * $height;
    }
    else
    {
        
$new_width $width;
        
$new_height $height;
    }
    if (!
$img imagecreatetruecolor($new_width$new_height)) // Create the new base image
        
return false;
    if (!
imagecopyresampled($img$src0000$new_width$new_height$width$height)) // Resample on to the new base image
        
return false;

    
ob_start();

    switch (
$type)
    {
        case 
1:
            if (!
imagegif($img))
                return 
false;
            break;
        case 
2:
            if (!
imagejpeg($img))
                return 
false;
            break;
        case 
3:
            if (!
imagepng($img))
                return 
false;
            break;
        default:
            return 
false;
    }        

    
$filedata ob_get_clean();

    if (!
$fp fopen($dst'w'))
        return 
false;
    if (!
fwrite($fp$filedata))
        return 
false;
    
fclose($fp);

    return 
true;
}
?>