function SendSaveAsFileHeaderIfNeeded()
{
    if (!empty($_GET['down'])) {
        $downloadfilename = ereg_replace('[/\\:\\*\\?"<>|]', '_', $_GET['down']);
        if (phpthumb_functions::version_compare_replacement(phpversion(), '4.1.0', '>=')) {
            $downloadfilename = trim($downloadfilename, '.');
        }
        if (!empty($downloadfilename)) {
            header('Content-Disposition: attachment; filename="' . $downloadfilename . '"');
        }
    }
    return true;
}
$PHPTHUMB_CONFIG['temp_directory'] = $PHPTHUMB_CONFIG['cache_directory'];
// set to same as cache directory
// NOTE: "max_source_pixels" only affects GD-resized thumbnails. If you have ImageMagick
//       installed it will bypass most of these limits
// maximum number of pixels in source image to attempt to process entire image in GD mode.
// If this is zero then no limit on source image dimensions.
// If this is nonzero then this is the maximum number of pixels the source image
// can have to be processed normally, otherwise the embedded EXIF thumbnail will
// be used (if available) or an "image too large" notice will be displayed.
// This is to be used for large source images (> 1600x1200) and low PHP memory
// limits. If PHP runs out of memory the script will usually just die with no output.
// To calculate this number, multiply the dimensions of the largest image
// you can process with your memory limitation (e.g. 1600 * 1200 = 1920000)
// As a general guideline, this number will be about 20% of your PHP memory
// configuration, so 8M = 1,677,722; 16M = 3,355,443; 32M = 6,710,886; etc.
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=') && !defined('memory_get_usage') && !@ini_get('memory_limit')) {
    // memory_get_usage() will only be defined if your PHP is compiled with the --enable-memory-limit configuration option.
    $PHPTHUMB_CONFIG['max_source_pixels'] = 0;
    // no memory limit
} else {
    // calculate default max_source_pixels as 1/6 of memory limit configuration
    $PHPTHUMB_CONFIG['max_source_pixels'] = round(max(intval(ini_get('memory_limit')), intval(get_cfg_var('memory_limit'))) * 1048576 / 6);
    //$PHPTHUMB_CONFIG['max_source_pixels'] = 0;       // no memory limit
    //$PHPTHUMB_CONFIG['max_source_pixels'] = 1920000; // allow 1600x1200 images (2Mpx), no larger (about 12MB memory required)
    //$PHPTHUMB_CONFIG['max_source_pixels'] = 2795000; // 16MB memory limit
    //$PHPTHUMB_CONFIG['max_source_pixels'] = 3871488; // allow 2272x1704 images (4Mpx), no larger (about 24MB memory required)
}
// ImageMagick configuration
$PHPTHUMB_CONFIG['prefer_imagemagick'] = true;
// If true, use ImageMagick to resize thumbnails if possible, since it is usually faster than GD functions; if false only use ImageMagick if PHP memory limit is too low.
$PHPTHUMB_CONFIG['imagemagick_use_thumbnail'] = true;
Exemple #3
0
 public function Smooth(&$gdimg, $amount = 6)
 {
     $amount = min(25, max(0, $amount));
     if ($amount == 0) {
         return true;
     }
     if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
         if (ImageFilter($gdimg, IMG_FILTER_SMOOTH, $amount)) {
             return true;
         }
         $this->DebugMessage('FAILED: ImageFilter($gdimg, IMG_FILTER_SMOOTH, ' . $amount . ')', __FILE__, __LINE__);
         // fall through and try it the hard way
     }
     // currently not implemented "the hard way"
     $this->DebugMessage('FAILED: phpthumb_filters::Smooth($gdimg, ' . $amount . ') [function not implemented]', __FILE__, __LINE__);
     return false;
 }
function SendSaveAsFileHeaderIfNeeded()
{
    if (headers_sent()) {
        return false;
    }
    global $phpThumb;
    if (@$_GET['down']) {
        $downloadfilename = ereg_replace('[/\\:\\*\\?"<>|]', '_', $_GET['down']);
        if (phpthumb_functions::version_compare_replacement(phpversion(), '4.1.0', '>=')) {
            $downloadfilename = trim($downloadfilename, '.');
        }
        if (@$downloadfilename) {
            $phpThumb->DebugMessage('SendSaveAsFileHeaderIfNeeded() sending header: Content-Disposition: attachment; filename="' . $downloadfilename . '"', __FILE__, __LINE__);
            header('Content-Disposition: attachment; filename="' . $downloadfilename . '"');
            return true;
        }
    }
    $phpThumb->DebugMessage('SendSaveAsFileHeaderIfNeeded() sending header: Content-Disposition: inline', __FILE__, __LINE__);
    header('Content-Disposition: inline');
    return true;
}
 function SanitizeFilename($filename)
 {
     $filename = ereg_replace('[^' . preg_quote(' !#$%^()+,-.;<>=@[]_{}') . 'a-zA-Z0-9]', '_', $filename);
     if (phpthumb_functions::version_compare_replacement(phpversion(), '4.1.0', '>=')) {
         $filename = trim($filename, '.');
     }
     return $filename;
 }
 function ErrorImage($text, $width = 0, $height = 0, $forcedisplay = false)
 {
     $width = $width ? $width : $this->config_error_image_width;
     $height = $height ? $height : $this->config_error_image_height;
     $text = 'phpThumb() v' . $this->phpthumb_version . "\n" . 'http://phpthumb.sourceforge.net' . "\n\n" . ($this->config_disable_debug ? 'Error messages disabled' : $text);
     $this->FatalError($text);
     $this->DebugMessage($text, __FILE__, __LINE__);
     $this->purgeTempFiles();
     if ($this->phpThumbDebug && !$forcedisplay) {
         return false;
     }
     if (!$this->config_error_die_on_error && !$forcedisplay) {
         return false;
     }
     if ($this->config_error_silent_die_on_error) {
         exit;
     }
     if ($this->err || $this->config_error_message_image_default) {
         // Show generic custom error image instead of error message
         // for use on production sites where you don't want debug messages
         if ($this->err == 'showerror' || $this->phpThumbDebug) {
             // fall through and actually show error message even if default error image is set
         } else {
             header('Location: ' . ($this->err ? $this->err : $this->config_error_message_image_default));
             exit;
         }
     }
     $this->setOutputFormat();
     if (!$this->thumbnailFormat || phpthumb_functions::gd_version() < 1) {
         $this->thumbnailFormat = 'text';
     }
     if (@$this->thumbnailFormat == 'text') {
         // bypass all GD functions and output text error message
         if (!headers_sent()) {
             header('Content-type: text/plain');
             echo $text;
         } else {
             echo '<pre>' . htmlspecialchars($text) . '</pre>';
         }
         exit;
     }
     $FontWidth = ImageFontWidth($this->config_error_fontsize);
     $FontHeight = ImageFontHeight($this->config_error_fontsize);
     $LinesOfText = explode("\n", @wordwrap($text, floor($width / $FontWidth), "\n", true));
     $height = max($height, count($LinesOfText) * $FontHeight);
     $headers_file = '';
     $headers_line = '';
     if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.0', '>=') && headers_sent($headers_file, $headers_line)) {
         echo "\n" . '**Headers already sent in file "' . $headers_file . '" on line "' . $headers_line . '", dumping error message as text:**<br><pre>' . "\n\n" . $text . "\n" . '</pre>';
     } elseif (headers_sent()) {
         echo "\n" . '**Headers already sent, dumping error message as text:**<br><pre>' . "\n\n" . $text . "\n" . '</pre>';
     } elseif ($gdimg_error = ImageCreate($width, $height)) {
         $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_bgcolor, true);
         $text_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_textcolor, true);
         ImageFilledRectangle($gdimg_error, 0, 0, $width, $height, $background_color);
         $lineYoffset = 0;
         foreach ($LinesOfText as $line) {
             ImageString($gdimg_error, $this->config_error_fontsize, 2, $lineYoffset, $line, $text_color);
             $lineYoffset += $FontHeight;
         }
         if (function_exists('ImageTypes')) {
             $imagetypes = ImageTypes();
             if ($imagetypes & IMG_PNG) {
                 header('Content-Type: image/png');
                 ImagePNG($gdimg_error);
             } elseif ($imagetypes & IMG_GIF) {
                 header('Content-Type: image/gif');
                 ImageGIF($gdimg_error);
             } elseif ($imagetypes & IMG_JPG) {
                 header('Content-Type: image/jpeg');
                 ImageJPEG($gdimg_error);
             } elseif ($imagetypes & IMG_WBMP) {
                 header('Content-Type: image/vnd.wap.wbmp');
                 ImageWBMP($gdimg_error);
             }
         }
         ImageDestroy($gdimg_error);
     }
     if (!headers_sent()) {
         echo "\n" . '**Failed to send graphical error image, dumping error message as text:**<br>' . "\n\n" . $text;
     }
     exit;
     return true;
 }
 function applyUnsharpMask(&$img, $amount, $radius, $threshold)
 {
     // $img is an image that is already created within php using
     // imgcreatetruecolor. No url! $img must be a truecolor image.
     // Attempt to calibrate the parameters to Photoshop:
     $amount = min($amount, 500);
     $amount = $amount * 0.016;
     if ($amount == 0) {
         return true;
     }
     $radius = min($radius, 50);
     $radius = $radius * 2;
     $threshold = min($threshold, 255);
     $radius = abs(round($radius));
     // Only integers make sense.
     if ($radius == 0) {
         return true;
     }
     $w = ImageSX($img);
     $h = ImageSY($img);
     $imgCanvas = ImageCreateTrueColor($w, $h);
     $imgCanvas2 = ImageCreateTrueColor($w, $h);
     ImageCopy($imgCanvas, $img, 0, 0, 0, 0, $w, $h);
     ImageCopy($imgCanvas2, $img, 0, 0, 0, 0, $w, $h);
     $builtinFilterSucceeded = false;
     if ($radius == 1) {
         if (phpthumb_functions::version_compare_replacement(phpversion(), '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
             if (ImageFilter($imgCanvas, IMG_FILTER_GAUSSIAN_BLUR) && ImageFilter($imgCanvas2, IMG_FILTER_GAUSSIAN_BLUR)) {
                 $builtinFilterSucceeded = true;
             }
         }
     }
     if (!$builtinFilterSucceeded) {
         $imgBlur = ImageCreateTrueColor($w, $h);
         $imgBlur2 = ImageCreateTrueColor($w, $h);
         ///////////////////////////
         //
         // Gaussian blur matrix:
         //	1  2  1
         //	2  4  2
         //	1  2  1
         //
         ///////////////////////////
         // Move copies of the image around one pixel at the time and merge them with weight
         // according to the matrix. The same matrix is simply repeated for higher radii.
         for ($i = 0; $i < $radius; $i++) {
             ImageCopy($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1);
             // up left
             ImageCopyMerge($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50);
             // down right
             ImageCopyMerge($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333);
             // down left
             ImageCopyMerge($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25);
             // up right
             ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333);
             // left
             ImageCopyMerge($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25);
             // right
             ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20);
             // up
             ImageCopyMerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667);
             // down
             ImageCopyMerge($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50);
             // center
             ImageCopy($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);
             // During the loop above the blurred copy darkens, possibly due to a roundoff
             // error. Therefore the sharp picture has to go through the same loop to
             // produce a similar image for comparison. This is not a good thing, as processing
             // time increases heavily.
             ImageCopy($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h);
             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50);
             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333);
             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25);
             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 33.33333);
             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 25);
             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 20);
             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 16.666667);
             ImageCopyMerge($imgBlur2, $imgCanvas2, 0, 0, 0, 0, $w, $h, 50);
             ImageCopy($imgCanvas2, $imgBlur2, 0, 0, 0, 0, $w, $h);
         }
         ImageDestroy($imgBlur);
         ImageDestroy($imgBlur2);
     }
     // Calculate the difference between the blurred pixels and the original
     // and set the pixels
     for ($x = 0; $x < $w; $x++) {
         // each row
         for ($y = 0; $y < $h; $y++) {
             // each pixel
             $rgbOrig = ImageColorAt($imgCanvas2, $x, $y);
             $rOrig = $rgbOrig >> 16 & 0xff;
             $gOrig = $rgbOrig >> 8 & 0xff;
             $bOrig = $rgbOrig & 0xff;
             $rgbBlur = ImageColorAt($imgCanvas, $x, $y);
             $rBlur = $rgbBlur >> 16 & 0xff;
             $gBlur = $rgbBlur >> 8 & 0xff;
             $bBlur = $rgbBlur & 0xff;
             // When the masked pixels differ less from the original
             // than the threshold specifies, they are set to their original value.
             $rNew = abs($rOrig - $rBlur) >= $threshold ? max(0, min(255, $amount * ($rOrig - $rBlur) + $rOrig)) : $rOrig;
             $gNew = abs($gOrig - $gBlur) >= $threshold ? max(0, min(255, $amount * ($gOrig - $gBlur) + $gOrig)) : $gOrig;
             $bNew = abs($bOrig - $bBlur) >= $threshold ? max(0, min(255, $amount * ($bOrig - $bBlur) + $bOrig)) : $bOrig;
             if ($rOrig != $rNew || $gOrig != $gNew || $bOrig != $bNew) {
                 $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);
                 ImageSetPixel($img, $x, $y, $pixCol);
             }
         }
     }
     ImageDestroy($imgCanvas);
     ImageDestroy($imgCanvas2);
     return true;
 }
    echo 'orange';
} elseif ($php_sapi_name == 'cgi') {
    echo 'yellow';
} elseif ($php_sapi_name == 'apache') {
    echo 'lime';
} else {
    echo 'green';
}
echo '">' . $php_sapi_name . '</th>';
echo '<td>SAPI mode preferred to CGI mode. FCGI mode has unconfirmed strange behavior (notably more than one space in "wmt" filter text causes errors). If not working in "apache" (SAPI) mode, <i>apache_lookup_uri()</i> will not work.</td></tr>';
echo '<tr><th>Server Software</th><th colspan="2" bgcolor="';
$server_software = getenv('SERVER_SOFTWARE');
if (!$server_software) {
    echo 'red';
} elseif (eregi('^Apache/([0-9\\.]+)', $server_software, $matches)) {
    if (phpthumb_functions::version_compare_replacement($matches[1], '2.0.0', '>=')) {
        echo 'lightgreen';
    } else {
        echo 'lime';
    }
} else {
    echo 'darkgreen';
}
echo '">' . $server_software . '</th>';
echo '<td>Apache v1.x has the fewest compatability problems. IIS has numerous annoyances. Apache v2.x is broken when lookup up <i>/~user/filename.jpg</i> style relative filenames using <i>apache_lookup_uri()</i>.</td></tr>';
echo '<tr><th>curl_version:</th><th colspan="2" bgcolor="';
$curl_version = function_exists('curl_version') ? curl_version() : '';
if (is_array($curl_version)) {
    $curl_version = @$curl_version['version'];
}
if ($curl_version) {
 function ExtractEXIFgetImageSize()
 {
     if ($this->getimagesizeinfo = @GetImageSize($this->sourceFilename)) {
         $this->source_width = $this->getimagesizeinfo[0];
         $this->source_height = $this->getimagesizeinfo[1];
         if (function_exists('exif_thumbnail') && $this->getimagesizeinfo[2] == 2) {
             // Extract EXIF info from JPEGs
             $this->exif_thumbnail_width = '';
             $this->exif_thumbnail_height = '';
             $this->exif_thumbnail_type = '';
             // The parameters width, height and imagetype are available since PHP v4.3.0
             if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.0', '>=')) {
                 $this->exif_thumbnail_data = @exif_thumbnail($this->sourceFilename, $this->exif_thumbnail_width, $this->exif_thumbnail_height, $this->exif_thumbnail_type);
             } else {
                 // older versions of exif_thumbnail output an error message but NOT return false on failure
                 ob_start();
                 $this->exif_thumbnail_data = exif_thumbnail($this->sourceFilename);
                 $exit_thumbnail_error = ob_get_contents();
                 ob_end_clean();
                 if (empty($exit_thumbnail_error) && !empty($this->exif_thumbnail_data)) {
                     if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
                         $this->exif_thumbnail_width = ImageSX($gdimg_exif_temp);
                         $this->exif_thumbnail_height = ImageSY($gdimg_exif_temp);
                         $this->exif_thumbnail_type = 2;
                         // (2 == JPEG) before PHP v4.3.0 only JPEG format EXIF thumbnails are returned
                         unset($gdimg_exif_temp);
                     } else {
                         $this->ErrorImage('Failed - $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data) in ' . __FILE__ . ' on line ' . __LINE__);
                     }
                 }
             }
         }
         // see if EXIF thumbnail can be used directly with no processing
         if (!empty($this->exif_thumbnail_data)) {
             while (true) {
                 if (!empty($this->xto)) {
                     if (isset($this->w) && $this->w != $this->exif_thumbnail_width) {
                         break;
                     }
                     if (isset($this->h) && $this->h != $this->exif_thumbnail_height) {
                         break;
                     }
                     $CannotBeSetParameters = array('sx', 'sy', 'sh', 'sw', 'bw', 'bg', 'bc', 'usa', 'usr', 'ust', 'wmf', 'wmp', 'wmm', 'wma');
                     foreach ($CannotBeSetParameters as $parameter) {
                         if (!empty($this->{$parameter})) {
                             break 2;
                         }
                     }
                 }
                 if (!empty($this->xto) && empty($this->phpThumbDebug)) {
                     // write cached file
                     $ImageTypesLookup = array(2 => 'jpeg');
                     // EXIF thumbnails are (currently?) only availble from JPEG source images
                     if (is_dir($this->config_cache_directory) && is_writable($this->config_cache_directory) && isset($ImageTypesLookup[$this->exif_thumbnail_type])) {
                         $cache_filename = $this->GenerateCachedFilename($ImageTypesLookup[$this->exif_thumbnail_type]);
                         if (is_writable($cache_filename)) {
                             if ($fp_cached = @fopen($cache_filename, 'wb')) {
                                 fwrite($fp_cached, $this->exif_thumbnail_data);
                                 fclose($fp_cached);
                             }
                         }
                     }
                     if ($mime_type = ImageTypeToMIMEtype($this->exif_thumbnail_type)) {
                         header('Content-type: ' . $mime_type);
                         echo $this->exif_thumbnail_data;
                         exit;
                     } else {
                         $this->ErrorImage('ImageTypeToMIMEtype(' . $this->exif_thumbnail_type . ') failed in ' . __FILE__ . ' on line ' . __LINE__);
                     }
                 }
                 break;
             }
         }
         if ($this->config_max_source_pixels > 0 && $this->source_width * $this->source_height > $this->config_max_source_pixels) {
             // Source image is larger than would fit in available PHP memory
             if (!empty($this->exif_thumbnail_data)) {
                 // EXIF thumbnail exists, and will be use as source image
                 $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data);
                 $this->source_width = $this->exif_thumbnail_width;
                 $this->source_height = $this->exif_thumbnail_height;
                 // override allow-enlarging setting if EXIF thumbnail is the only source available
                 // otherwise thumbnails larger than the EXIF thumbnail will be created at EXIF size
                 $this->config_output_allow_enlarging = true;
             } else {
                 // EXIF thumbnail is unavailable
                 // if ImageMagick is installed, use it to thumbnail,
                 // otherwise no choice but to abort
                 if ($this->ImageMagickThumbnailToGD()) {
                     // excellent, we have a thumbnail
                 } else {
                     $this->ErrorImage('Source image is more than ' . sprintf('%1.1f', $this->config_max_source_pixels / 1000000) . ' megapixels - insufficient memory.' . "\n" . 'EXIF thumbnail unavailable.');
                 }
             }
         }
     }
     return true;
 }
 function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment = '*', $opacity = 50, $margin = 5)
 {
     if (is_resource($gdimg_dest) && is_resource($img_watermark)) {
         $watermark_source_x = 0;
         $watermark_source_y = 0;
         $img_source_width = ImageSX($gdimg_dest);
         $img_source_height = ImageSY($gdimg_dest);
         $watermark_source_width = ImageSX($img_watermark);
         $watermark_source_height = ImageSY($img_watermark);
         $watermark_opacity_percent = max(0, min(100, $opacity));
         if ($margin < 1) {
             $watermark_margin_percent = 1 - $margin;
         } else {
             $watermark_margin_percent = (100 - max(0, min(100, $margin))) / 100;
         }
         $watermark_margin_x = round((1 - $watermark_margin_percent) * $img_source_width);
         $watermark_margin_y = round((1 - $watermark_margin_percent) * $img_source_height);
         switch ($alignment) {
             case '*':
                 if ($gdimg_tiledwatermark = phpthumb_functions::ImageCreateFunction($img_source_width, $img_source_height)) {
                     ImageAlphaBlending($gdimg_tiledwatermark, false);
                     if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
                         ImageSaveAlpha($gdimg_tiledwatermark, true);
                     }
                     $text_color_transparent = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_tiledwatermark, 255, 0, 255, 127);
                     ImageFill($gdimg_tiledwatermark, 0, 0, $text_color_transparent);
                     // set the tiled image transparent color to whatever the untiled image transparency index is
                     //						ImageColorTransparent($gdimg_tiledwatermark, ImageColorTransparent($img_watermark));
                     // a "cleaner" way of doing it, but can't handle the margin feature :(
                     //						ImageSetTile($gdimg_tiledwatermark, $img_watermark);
                     //						ImageFill($gdimg_tiledwatermark, 0, 0, IMG_COLOR_TILED);
                     //						break;
                     //						ImageFill($gdimg_tiledwatermark, 0, 0, ImageColorTransparent($gdimg_tiledwatermark));
                     // tile the image as many times as can fit
                     for ($x = $watermark_margin_x; $x < $img_source_width + $watermark_source_width; $x += round($watermark_source_width + (1 - $watermark_margin_percent) * $img_source_width)) {
                         for ($y = $watermark_margin_y; $y < $img_source_height + $watermark_source_height; $y += round($watermark_source_height + (1 - $watermark_margin_percent) * $img_source_height)) {
                             ImageCopy($gdimg_tiledwatermark, $img_watermark, $x, $y, 0, 0, min($watermark_source_width, $img_source_width - $x - (1 - $watermark_margin_percent) * $img_source_width), min($watermark_source_height, $img_source_height - $y - (1 - $watermark_margin_percent) * $img_source_height));
                         }
                     }
                     $watermark_source_width = ImageSX($gdimg_tiledwatermark);
                     $watermark_source_height = ImageSY($gdimg_tiledwatermark);
                     $watermark_destination_x = 0;
                     $watermark_destination_y = 0;
                     ImageDestroy($img_watermark);
                     $img_watermark = $gdimg_tiledwatermark;
                 }
                 break;
             case 'T':
                 $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2 + $watermark_margin_x);
                 $watermark_destination_y = $watermark_margin_y;
                 break;
             case 'B':
                 $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2 + $watermark_margin_x);
                 $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
                 break;
             case 'L':
                 $watermark_destination_x = $watermark_margin_x;
                 $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2 + $watermark_margin_y);
                 break;
             case 'R':
                 $watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent);
                 $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2 + $watermark_margin_y);
                 break;
             case 'C':
                 $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2);
                 $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2);
                 break;
             case 'TL':
                 $watermark_destination_x = $watermark_margin_x;
                 $watermark_destination_y = $watermark_margin_y;
                 break;
             case 'TR':
                 $watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent);
                 $watermark_destination_y = $watermark_margin_y;
                 break;
             case 'BL':
                 $watermark_destination_x = $watermark_margin_x;
                 $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
                 break;
             case 'BR':
             default:
                 $watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent);
                 $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
                 break;
         }
         ImageAlphaBlending($gdimg_dest, false);
         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
             ImageSaveAlpha($gdimg_dest, true);
             ImageSaveAlpha($img_watermark, true);
         }
         phpthumb_functions::ImageCopyRespectAlpha($gdimg_dest, $img_watermark, $watermark_destination_x, $watermark_destination_y, 0, 0, $watermark_source_width, $watermark_source_height, $watermark_opacity_percent);
         return true;
     }
     return false;
 }
 function filedate_remote($remotefile, $timeout = 10)
 {
     $date = false;
     $url = parse_url($remotefile);
     if ($fp = @fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $errstr, $timeout)) {
         fwrite($fp, 'HEAD ' . @$url['path'] . @$url['query'] . ' HTTP/1.0' . "\r\n" . 'Host: ' . @$url['host'] . "\r\n\r\n");
         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.0', '>=')) {
             stream_set_timeout($fp, $timeout);
         }
         while (!feof($fp)) {
             $headerline = fgets($fp, 4096);
             if (eregi('^Last-Modified: (.*)', $headerline, $matches)) {
                 $date = strtotime($matches[1]) - date('Z');
                 break;
             }
         }
         fclose($fp);
     }
     return $date;
 }
 function ExtractEXIFgetImageSize()
 {
     if (is_resource($this->gdimg_source)) {
         $this->source_width = ImageSX($this->gdimg_source);
         $this->source_height = ImageSY($this->gdimg_source);
     } elseif ($this->rawImageData && !$this->sourceFilename) {
         $this->DebugMessage('bypassing EXIF and GetImageSize sections because $this->rawImageData is set and $this->sourceFilename is not', __FILE__, __LINE__);
     } elseif ($this->getimagesizeinfo = @GetImageSize($this->sourceFilename)) {
         $this->source_width = $this->getimagesizeinfo[0];
         $this->source_height = $this->getimagesizeinfo[1];
         if (function_exists('exif_thumbnail') && $this->getimagesizeinfo[2] == 2) {
             // Extract EXIF info from JPEGs
             $this->exif_thumbnail_width = '';
             $this->exif_thumbnail_height = '';
             $this->exif_thumbnail_type = '';
             // The parameters width, height and imagetype are available since PHP v4.3.0
             if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.0', '>=')) {
                 $this->exif_thumbnail_data = @exif_thumbnail($this->sourceFilename, $this->exif_thumbnail_width, $this->exif_thumbnail_height, $this->exif_thumbnail_type);
             } else {
                 // older versions of exif_thumbnail output an error message but NOT return false on failure
                 ob_start();
                 $this->exif_thumbnail_data = exif_thumbnail($this->sourceFilename);
                 $exit_thumbnail_error = ob_get_contents();
                 ob_end_clean();
                 if (empty($exit_thumbnail_error) && !empty($this->exif_thumbnail_data)) {
                     if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
                         $this->exif_thumbnail_width = ImageSX($gdimg_exif_temp);
                         $this->exif_thumbnail_height = ImageSY($gdimg_exif_temp);
                         $this->exif_thumbnail_type = 2;
                         // (2 == JPEG) before PHP v4.3.0 only JPEG format EXIF thumbnails are returned
                         unset($gdimg_exif_temp);
                     } else {
                         return $this->ErrorImage('Failed - $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data) in ' . __FILE__ . ' on line ' . __LINE__);
                     }
                 }
             }
         } elseif (!function_exists('exif_thumbnail')) {
             $this->DebugMessage('exif_thumbnail() does not exist, cannot extract EXIF thumbnail', __FILE__, __LINE__);
         }
         // see if EXIF thumbnail can be used directly with no processing
         if (!empty($this->exif_thumbnail_data)) {
             while (true) {
                 if (empty($this->xto)) {
                     if (isset($this->w) && $this->w != $this->exif_thumbnail_width) {
                         break;
                     }
                     if (isset($this->h) && $this->h != $this->exif_thumbnail_height) {
                         break;
                     }
                     $CannotBeSetParameters = array('sx', 'sy', 'sh', 'sw', 'far', 'bg', 'bc', 'fltr', 'phpThumbDebug');
                     foreach ($CannotBeSetParameters as $parameter) {
                         if (!empty($this->{$parameter})) {
                             break 2;
                         }
                     }
                 }
                 // EXIF thumbnail can be used directly for these parameters - write cached file
                 $ImageTypesLookup = array(2 => 'jpeg');
                 // EXIF thumbnails are (currently?) only availble from JPEG source images
                 if (is_dir($this->config_cache_directory) && is_writable($this->config_cache_directory) && isset($ImageTypesLookup[$this->exif_thumbnail_type])) {
                     if ($fp_cached = @fopen($this->cache_filename, 'wb')) {
                         fwrite($fp_cached, $this->exif_thumbnail_data);
                         fclose($fp_cached);
                     } else {
                         $this->DebugMessage('failed to fopen $this->cache_filename (' . $this->cache_filename . ')', __FILE__, __LINE__);
                     }
                 } else {
                     $this->DebugMessage('!is_dir($this->config_cache_directory), or !is_writable($this->config_cache_directory) (' . $this->cache_filename . '); or !isset($ImageTypesLookup[' . $this->exif_thumbnail_type . '])', __FILE__, __LINE__);
                 }
                 if ($mime_type = phpthumb_functions::ImageTypeToMIMEtype($this->exif_thumbnail_type)) {
                     header('Content-type: ' . $mime_type);
                     echo $this->exif_thumbnail_data;
                     exit;
                 } else {
                     return $this->ErrorImage('phpthumb_functions::ImageTypeToMIMEtype(' . $this->exif_thumbnail_type . ') failed in ' . __FILE__ . ' on line ' . __LINE__);
                 }
                 break;
             }
         }
         if ($this->config_max_source_pixels > 0 && $this->source_width * $this->source_height > $this->config_max_source_pixels) {
             // Source image is larger than would fit in available PHP memory.
             // If ImageMagick is installed, use it to generate the thumbnail.
             // Else, if an EXIF thumbnail is available, use that as the source image.
             // Otherwise, no choice but to fail with an error message
             $this->DebugMessage('image is ' . $this->source_width . 'x' . $this->source_height . ' and therefore contains more pixels (' . $this->source_width * $this->source_height . ') than $this->config_max_source_pixels setting (' . $this->config_max_source_pixels . ')', __FILE__, __LINE__);
             if ($this->ImageMagickThumbnailToGD()) {
                 // excellent, we have a thumbnailed source image
             } elseif (!empty($this->exif_thumbnail_data)) {
                 $this->DebugMessage('ImageMagickThumbnailToGD() failed, but $this->exif_thumbnail_data is usable', __FILE__, __LINE__);
                 // EXIF thumbnail exists, and will be use as source image
                 $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data);
                 $this->source_width = $this->exif_thumbnail_width;
                 $this->source_height = $this->exif_thumbnail_height;
                 // override allow-enlarging setting if EXIF thumbnail is the only source available
                 // otherwise thumbnails larger than the EXIF thumbnail will be created at EXIF size
                 $this->config_output_allow_enlarging = true;
             } else {
                 return $this->ErrorImage('Source image is (' . $this->source_width . 'x' . $this->source_height . ') which equals ' . sprintf('%1.1f', $this->source_width * $this->source_height) . ' megapixels, which is more than the allowed ' . sprintf('%1.1f', $this->config_max_source_pixels / 1000000) . ' megapixels -- insufficient memory.' . "\n" . 'EXIF thumbnail unavailable.');
             }
         }
     } else {
         $this->DebugMessage('GetImageSize("' . $this->sourceFilename . '") failed', __FILE__, __LINE__);
     }
     return true;
 }
 function ImageColorAllocateAlphaSafe(&$gdimg_hexcolorallocate, $R, $G, $B, $alpha = false)
 {
     if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=') && $alpha !== false) {
         return ImageColorAllocateAlpha($gdimg_hexcolorallocate, $R, $G, $B, $alpha);
     } else {
         return ImageColorAllocate($gdimg_hexcolorallocate, $R, $G, $B);
     }
 }