ImgRed.com source code (PHP using MySQL, some parts edited)

Save the below code to a PHP file called ImgRed.php (for example).
Full-sized images will be accessed as ImgRed.php?Image=URL
Thumbnails will be accessed as ImgRed.php?Thumb=URL

You will need to create a MySQL table called 'Cached' with five columns:

You will also need these graphics (or your own substitutions).

Replace all sections in red with the correct data for your setup.
<?
function GetMIMEType ( $File )
{
    $File = escapeshellarg( $File );
    return exec('file -bi ' . $File);
}
function SQLConnect ( &$SQL )
{
    $SQL = @mysql_connect( 'DATABASE SERVER',
                           'USERNAME',
                           'PASSWORD' )
        or die( 'Error connecting to database.' );
    mysql_select_db( 'DATABASE NAME', $SQL );
}
// IMAGE_PATH is the folder where all cached images should be stored.
// For example, 'cached_images/'
// Note: it must end in a slash (/)
$IMAGE_PATH = 'ROOT IMAGE PATH';
$New = true;
if( $_GET['Image'] )
    $Source = 'Image';
else if( $_GET['Thumb'] )
    $Source = 'Thumb';
$URL = $_GET[$Source];
if( $URL )
{
    $Array = explode('/', $URL);
    if( strpos($Array[2], ':') !== false )
    {
        // A port was specified, so show NoPort.gif instead
        $Filename = 'gfx/NoPort.gif';
        $Name = 'NoPort.gif';
        $New = false;
    }
    if( $New )
    {    
        SQLConnect( $SQL );
        $Results = @mysql_query( 'SELECT ID,Filename FROM Cached WHERE URL=\'' . addslashes($URL) . '\' LIMIT 1', $SQL );
        if( $Results )
        {
            $Row = @mysql_fetch_array( $Results );
            if( $Row )
            {
                $ID = $Row[0];
                $Name = $Source . '_' . $Row[1];
                $Filename = $IMAGE_PATH . $Name;
                if( !file_exists($Filename) && $Source == 'Thumb' )
                {
                    // The thumbnail was requested but it doesn't exist; the image must have been too small for one.
                    $Name = 'Image_' . $Row[1];
                    $Filename = $IMAGE_PATH . $Name;
                }
                if( file_exists($Filename) )
                {
                    $New = false;
                    if( $Source == 'Image' )
                        mysql_query( 'UPDATE Cached SET NumViews=NumViews+1 WHERE ID=' . $ID . ' LIMIT 1', $SQL );
                    else
                        mysql_query( 'UPDATE Cached SET NumThumbViews=NumThumbViews+1 WHERE ID=' . $ID . ' LIMIT 1', $SQL );
                }
            }
        }
    }
    if( $New )
    {
        $Contents = @file_get_contents($URL);
        if( !$Contents )
        {
            // We can't load the image for some reason
            $Filename = 'gfx/CannotRead.gif';
            $Name = 'CannotRead.gif';
            $New = false;
        }
    }
    if( $New )
    {
        $Size = strlen($Contents);
        if( $Size > 2097152 )
        {
            // The image is greater than two megabytes
            $Filename = 'gfx/TooLarge.gif';
            $Name = 'TooLarge.gif';
            $New = false;
        }
    }
    if( $New )
    {
        $Filename = tempnam( $IMAGE_PATH, 'Image_' );
        $File = fopen( $Filename, 'wb' );
        fwrite( $File, $Contents );
        fclose( $File );
        $Name = substr($Filename, 6 + strlen($IMAGE_PATH));
        $TName = $IMAGE_PATH . 'Thumb_' . $Name;
        $MIME = GetMIMEType($Filename);
        if( $MIME == 'image/gif' )
            $Image = ImageCreateFromGIF( $Filename );
        else if( $MIME == 'image/png' )
            $Image = ImageCreateFromPNG( $Filename );
        else if( $MIME == 'image/jpeg' )
            $Image = ImageCreateFromJPEG( $Filename );
        else
        {
            // The file is not a PNG, GIF, or JPEG
            unlink( $Filename );
            $Filename = 'gfx/BadFormat.gif';
            $Name = 'BadFormat.gif';
            $New = false;
        }
    }
    if( $New )
    {
        $Dimensions = GetImageSize( $Filename );
        $W = $Dimensions[0];
        $H = $Dimensions[1];
        if( $W > 320 && $H > 320 )
        {
            // The image is large enough to warrant a thumbnail
            if( $W > $H )
            {
                $TW = 320;
                $TH = (int)($H * 320 / $W);
            }
            else
            {
                $TW = (int)($W * 320 / $H);
                $TH = 320;
            }
            $Thumb = ImageCreateTrueColor( $TW, $TH );
            ImageCopyResampled( $Thumb, $Image,
                                0, 0,
                                0, 0,
                                $TW, $TH,
                                $W, $H );
            if( $MIME == 'image/gif' || $MIME == 'image/png' )
                ImagePNG( $Thumb, $TName );
            else
                ImageJPEG( $Thumb, $TName, 75 );
            ImageDestroy( $Thumb );
        }
        else if( $Source == 'Thumb' )
            $Source = 'Image';
        if( ImageIsTrueColor($Image) && $W >= 480 && $H >= 480 )
        {
            // I put a tiny watermark on the bottom-right corner of large images.
            $Watermark = ImageCreateFromPNG( 'gfx/Watermark.png' );
            for( $Y = 0; $Y < 24; $Y ++ )
            for( $X = 0; $X < 96; $X ++ )
            {
                $Color = ImageColorAt( $Watermark, $X, $Y );
                $A_S = ($Color >> 24) / 127;
                $R_S = ($Color >> 16) & 0xFF;
                $G_S = ($Color >> 8) & 0xFF;
                $B_S = $Color & 0xFF;
                $Color = ImageColorAt( $Image, $X + $W - 96, $Y + $H - 24 );
                $R_D = ($Color >> 16) & 0xFF;
                $G_D = ($Color >> 8) & 0xFF;
                $B_D = $Color & 0xFF;
                $R = (int)($A_S * $R_D) + (int)((1 - $A_S) * $R_S);
                $G = (int)($A_S * $G_D) + (int)((1 - $A_S) * $G_S);
                $B = (int)($A_S * $B_D) + (int)((1 - $A_S) * $B_S);
                $Color = ImageColorAllocate( $Image, $R, $G, $B );
                ImageSetPixel( $Image, $X + $W - 96, $Y + $H - 24, $Color );
            }
            ImageDestroy( $Watermark );
            if( $MIME == 'image/gif' )
                ImageGIF( $Image, $Filename );
            else if( $MIME == 'image/png' )
                ImagePNG( $Image, $Filename );
            else
                ImageJPEG( $Image, $Filename, 75 );
        }
        ImageDestroy( $Image );
        // Add an entry for this image into our database
        if( $Source == 'Image' )
            mysql_query( 'INSERT INTO Cached VALUES ( 0, \'' . addslashes($URL) . '\', \'' . $Name . '\', 1, 0 )', $SQL );
        else
            mysql_query( 'INSERT INTO Cached VALUES ( 0, \'' . addslashes($URL) . '\', \'' . $Name . '\', 0, 1 )', $SQL );
        $Name = $Source . '_' . $Name;
        $Filename = $IMAGE_PATH . $Name;
    }
    if( $SQL )
        mysql_close( $SQL );
    $Size = filesize($Filename);
    $MIME = GetMIMEType($Filename);
    $Time = filemtime($Filename);
    // Send the appropriate headers for the image
    header( 'Content-Length: ' . $Size );
    header( 'Content-Type: ' . $MIME );
    header( 'Date: ' . date('r') );
    header( 'Expires: ' . date('r', time() + (365*86400)) );
    header( 'Last-Modified: ' . date('r', $Time) );
    header( 'Content-Disposition: inline; filename="' . $Name . '"' );
    // Now send the image itself
    readfile( $Filename );
    exit;
}
?>