function ImageCreateFromStringReplacement(&$RawImageData, $DieOnErrors = false)
 {
     // there are serious bugs in the non-bundled versions of GD which may cause
     // PHP to segfault when calling ImageCreateFromString() - avoid if at all possible
     // when not using a bundled version of GD2
     if (!phpthumb_functions::gd_version()) {
         if ($DieOnErrors) {
             if (!headers_sent()) {
                 // base64-encoded error image in GIF format
                 $ERROR_NOGD = 'R0lGODlhIAAgALMAAAAAABQUFCQkJDY2NkZGRldXV2ZmZnJycoaGhpSUlKWlpbe3t8XFxdXV1eTk5P7+/iwAAAAAIAAgAAAE/vDJSau9WILtTAACUinDNijZtAHfCojS4W5H+qxD8xibIDE9h0OwWaRWDIljJSkUJYsN4bihMB8th3IToAKs1VtYM75cyV8sZ8vygtOE5yMKmGbO4jRdICQCjHdlZzwzNW4qZSQmKDaNjhUMBX4BBAlmMywFSRWEmAI6b5gAlhNxokGhooAIK5o/pi9vEw4Lfj4OLTAUpj6IabMtCwlSFw0DCKBoFqwAB04AjI54PyZ+yY3TD0ss2YcVmN/gvpcu4TOyFivWqYJlbAHPpOntvxNAACcmGHjZzAZqzSzcq5fNjxFmAFw9iFRunD1epU6tsIPmFCAJnWYE0FURk7wJDA0MTKpEzoWAAskiAAA7';
                 header('Content-Type: image/gif');
                 echo base64_decode($ERROR_NOGD);
             } else {
                 echo '*** ERROR: No PHP-GD support available ***';
             }
             exit;
         } else {
             $this->DebugMessage('ImageCreateFromStringReplacement() failed: gd_version says "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__);
             return false;
         }
     }
     if (phpthumb_functions::gd_is_bundled()) {
         $this->DebugMessage('ImageCreateFromStringReplacement() calling built-in ImageCreateFromString()', __FILE__, __LINE__);
         return @ImageCreateFromString($RawImageData);
     }
     if ($this->issafemode) {
         $this->DebugMessage('ImageCreateFromStringReplacement() failed: cannot create temp file in SAFE_MODE', __FILE__, __LINE__);
         return false;
     }
     switch (substr($RawImageData, 0, 3)) {
         case 'GIF':
             $ICFSreplacementFunctionName = 'ImageCreateFromGIF';
             break;
         case "ÿØÿ":
             $ICFSreplacementFunctionName = 'ImageCreateFromJPEG';
             break;
         case "‰" . 'PN':
             $ICFSreplacementFunctionName = 'ImageCreateFromPNG';
             break;
         default:
             $this->DebugMessage('ImageCreateFromStringReplacement() failed: unknown fileformat signature "' . phpthumb_functions::HexCharDisplay(substr($RawImageData, 0, 3)) . '"', __FILE__, __LINE__);
             return false;
             break;
     }
     if ($tempnam = $this->phpThumb_tempnam()) {
         if ($fp_tempnam = @fopen($tempnam, 'wb')) {
             fwrite($fp_tempnam, $RawImageData);
             fclose($fp_tempnam);
             if ($ICFSreplacementFunctionName == 'ImageCreateFromGIF' && !function_exists($ICFSreplacementFunctionName)) {
                 // Need to create from GIF file, but ImageCreateFromGIF does not exist
                 ob_start();
                 if (!@(include_once dirname(__FILE__) . '/phpthumb.gif.php')) {
                     $ErrorMessage = 'Failed to include required file "' . dirname(__FILE__) . '/phpthumb.gif.php" in ' . __FILE__ . ' on line ' . __LINE__;
                     $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
                 }
                 ob_end_clean();
                 // gif_loadFileToGDimageResource() cannot read from raw data, write to file first
                 if ($tempfilename = $this->phpThumb_tempnam()) {
                     if ($fp_tempfile = @fopen($tempfilename, 'wb')) {
                         fwrite($fp_tempfile, $RawImageData);
                         fclose($fp_tempfile);
                         $gdimg_source = gif_loadFileToGDimageResource($tempfilename);
                         $this->DebugMessage('gif_loadFileToGDimageResource(' . $tempfilename . ') completed', __FILE__, __LINE__);
                         $this->DebugMessage('deleting "' . $tempfilename . '"', __FILE__, __LINE__);
                         unlink($tempfilename);
                         return $gdimg_source;
                         //							break;
                     } else {
                         $ErrorMessage = 'Failed to open tempfile in ' . __FILE__ . ' on line ' . __LINE__;
                         $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
                     }
                 } else {
                     $ErrorMessage = 'Failed to open generate tempfile name in ' . __FILE__ . ' on line ' . __LINE__;
                     $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
                 }
             } elseif (function_exists($ICFSreplacementFunctionName) && ($gdimg_source = @$ICFSreplacementFunctionName($tempnam))) {
                 // great
                 $this->DebugMessage($ICFSreplacementFunctionName . '(' . $tempnam . ') succeeded', __FILE__, __LINE__);
                 $this->DebugMessage('deleting "' . $tempnam . '"', __FILE__, __LINE__);
                 unlink($tempnam);
                 return $gdimg_source;
             } else {
                 // GD functions not available, or failed to create image
                 $this->DebugMessage($ICFSreplacementFunctionName . '(' . $tempnam . ') ' . (function_exists($ICFSreplacementFunctionName) ? 'failed' : 'does not exist'), __FILE__, __LINE__);
                 if (isset($_GET['phpThumbDebug'])) {
                     $this->phpThumbDebug();
                 }
             }
         } else {
             $ErrorMessage = 'Failed to fopen(' . $tempnam . ', "wb") in ' . __FILE__ . ' on line ' . __LINE__ . "\n" . 'You may need to set $PHPTHUMB_CONFIG[temp_directory] in phpThumb.config.php';
             if ($this->issafemode) {
                 $ErrorMessage = 'ImageCreateFromStringReplacement() failed in ' . __FILE__ . ' on line ' . __LINE__ . ': cannot create temp file in SAFE_MODE';
             }
             $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
         }
         $this->DebugMessage('deleting "' . $tempnam . '"', __FILE__, __LINE__);
         @unlink($tempnam);
     } else {
         $ErrorMessage = 'Failed to generate phpThumb_tempnam() in ' . __FILE__ . ' on line ' . __LINE__ . "\n" . 'You may need to set $PHPTHUMB_CONFIG[temp_directory] in phpThumb.config.php';
         if ($this->issafemode) {
             $ErrorMessage = 'ImageCreateFromStringReplacement() failed in ' . __FILE__ . ' on line ' . __LINE__ . ': cannot create temp file in SAFE_MODE';
         }
     }
     if ($DieOnErrors && $ErrorMessage) {
         return $this->ErrorImage($ErrorMessage);
     }
     return false;
 }
 function SourceImageToGD()
 {
     if (is_resource($this->gdimg_source)) {
         $this->DebugMessage('skipping SourceImageToGD() because $this->gdimg_source is already a resource', __FILE__, __LINE__);
         return true;
     }
     $this->DebugMessage('starting SourceImageToGD()', __FILE__, __LINE__);
     while (true) {
         if (!$this->config_use_exif_thumbnail_for_speed) {
             $this->DebugMessage('Not using EXIF thumbnail data because $this->config_use_exif_thumbnail_for_speed is FALSE', __FILE__, __LINE__);
             break;
         }
         if (!$this->exif_thumbnail_data) {
             $this->DebugMessage('Not using EXIF thumbnail data because $this->exif_thumbnail_data is empty', __FILE__, __LINE__);
             break;
         }
         if ($this->thumbnailCropX != 0 || $this->thumbnailCropY != 0) {
             $this->DebugMessage('Not using EXIF thumbnail data because source cropping is enabled (' . $this->thumbnailCropX . ',' . $this->thumbnailCropY . ')', __FILE__, __LINE__);
             break;
         }
         if ($this->w > $this->exif_thumbnail_width || $this->h > $this->exif_thumbnail_height) {
             $this->DebugMessage('Not using EXIF thumbnail data because EXIF thumbnail is too small (' . $this->exif_thumbnail_width . 'x' . $this->exif_thumbnail_height . ' vs ' . $this->w . 'x' . $this->h . ')', __FILE__, __LINE__);
             break;
         }
         $source_ar = $this->source_width / $this->source_height;
         $exif_ar = $this->exif_thumbnail_width / $this->exif_thumbnail_height;
         if (number_format($source_ar, 2) != number_format($exif_ar, 2)) {
             $this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar (' . $source_ar . ' != ' . $exif_ar . ')', __FILE__, __LINE__);
         }
         // EXIF thumbnail exists, and is equal to or larger than destination thumbnail, and will be use as source image
         $this->DebugMessage('Trying to use EXIF thumbnail as source image', __FILE__, __LINE__);
         if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
             $this->DebugMessage('Successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
             $this->gdimg_source = $gdimg_exif_temp;
             $this->source_width = $this->exif_thumbnail_width;
             $this->source_height = $this->exif_thumbnail_height;
             $this->thumbnailCropW = $this->source_width;
             $this->thumbnailCropH = $this->source_height;
             return true;
         } else {
             $this->DebugMessage('$this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false) failed', __FILE__, __LINE__);
         }
         break;
     }
     if (!$this->gdimg_source) {
         // try to create GD image source directly via GD, if possible,
         // rather than buffering to memory and creating with ImageCreateFromString
         $ImageCreateWasAttempted = false;
         $ImageCreateFromFunction = array(1 => 'ImageCreateFromGIF', 2 => 'ImageCreateFromJPEG', 3 => 'ImageCreateFromPNG', 15 => 'ImageCreateFromWBMP');
         switch (@$this->getimagesizeinfo[2]) {
             case 1:
                 // GIF
             // GIF
             case 2:
                 // JPEG
             // JPEG
             case 3:
                 // PNG
             // PNG
             case 15:
                 // WBMP
                 if ($this->md5s && $this->md5s != phpthumb_functions::md5_file_safe($this->sourceFilename)) {
                     return $this->ErrorImage('$this->md5s != md5($this->rawImageData)' . "\n" . '"' . $this->md5s . '" != ' . "\n" . '"' . phpthumb_functions::md5_file_safe($this->sourceFilename) . '"');
                 }
                 $ImageCreateFromFunctionName = $ImageCreateFromFunction[$this->getimagesizeinfo[2]];
                 if (function_exists($ImageCreateFromFunctionName)) {
                     $this->DebugMessage('Calling ' . $ImageCreateFromFunctionName . '(' . $this->sourceFilename . ')', __FILE__, __LINE__);
                     $ImageCreateWasAttempted = true;
                     $this->gdimg_source = @$ImageCreateFromFunctionName($this->sourceFilename);
                     switch ($this->getimagesizeinfo[2]) {
                         case 1:
                         case 3:
                             // GIF or PNG input file may have transparency
                             $this->is_alpha = true;
                             break;
                     }
                 } else {
                     $this->DebugMessage('NOT calling ' . $ImageCreateFromFunctionName . '(' . $this->sourceFilename . ') because !function_exists(' . $ImageCreateFromFunctionName . ')', __FILE__, __LINE__);
                 }
                 break;
             case 4:
                 // SWF
             // SWF
             case 5:
                 // PSD
             // PSD
             case 6:
                 // BMP
             // BMP
             case 7:
                 // TIFF (LE)
             // TIFF (LE)
             case 8:
                 // TIFF (BE)
             // TIFF (BE)
             case 9:
                 // JPC
             // JPC
             case 10:
                 // JP2
             // JP2
             case 11:
                 // JPX
             // JPX
             case 12:
                 // JB2
             // JB2
             case 13:
                 // SWC
             // SWC
             case 14:
                 // IFF
             // IFF
             case 16:
                 // XBM
                 $this->DebugMessage('No built-in image creation function for image type "' . @$this->getimagesizeinfo[2] . '" ($this->getimagesizeinfo[2])', __FILE__, __LINE__);
                 break;
             case '':
                 // no source file, source image was probably set by setSourceData()
                 break;
             default:
                 $this->DebugMessage('Unknown value for $this->getimagesizeinfo[2]: "' . @$this->getimagesizeinfo[2] . '"', __FILE__, __LINE__);
                 break;
         }
         if (!$this->gdimg_source) {
             // cannot create from filename, attempt to create source image with ImageCreateFromString, if possible
             if ($ImageCreateWasAttempted) {
                 $this->DebugMessage(@$ImageCreateFromFunctionName . '() was attempted but FAILED', __FILE__, __LINE__);
             }
             if (!$this->rawImageData) {
                 $this->DebugMessage('Populating $this->rawImageData and attempting ImageCreateFromStringReplacement()', __FILE__, __LINE__);
                 if ($fp = @fopen($this->sourceFilename, 'rb')) {
                     $this->rawImageData = '';
                     $filesize = filesize($this->sourceFilename);
                     $blocksize = 16384;
                     $blockreads = ceil($filesize / $blocksize);
                     for ($i = 0; $i < $blockreads; $i++) {
                         $this->rawImageData .= fread($fp, $blocksize);
                     }
                     fclose($fp);
                 } else {
                     return $this->ErrorImage('cannot fopen("' . $this->sourceFilename . '") on line ' . __LINE__ . ' of ' . __FILE__);
                 }
             }
             if ($this->md5s && $this->md5s != md5($this->rawImageData)) {
                 return $this->ErrorImage('$this->md5s != md5($this->rawImageData)' . "\n" . '"' . $this->md5s . '" != ' . "\n" . '"' . md5($this->rawImageData) . '"');
             }
             $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->rawImageData, true);
         }
         if (!$this->gdimg_source) {
             $this->DebugMessage('$this->gdimg_source is still empty', __FILE__, __LINE__);
             if ($this->ImageMagickThumbnailToGD()) {
                 // excellent, we have a thumbnailed source image
                 $this->DebugMessage('ImageMagickThumbnailToGD() succeeded', __FILE__, __LINE__);
             } else {
                 $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
                 $imageHeader = '';
                 $gd_info = phpthumb_functions::gd_info();
                 $GDreadSupport = false;
                 switch (substr($this->rawImageData, 0, 3)) {
                     case 'GIF':
                         $imageHeader = 'Content-Type: image/gif';
                         $GDreadSupport = (bool) @$gd_info['GIF Read Support'];
                         break;
                     case "ÿØÿ":
                         $imageHeader = 'Content-Type: image/jpeg';
                         $GDreadSupport = (bool) @$gd_info['JPG Support'];
                         break;
                     case "‰" . 'PN':
                         $imageHeader = 'Content-Type: image/png';
                         $GDreadSupport = (bool) @$gd_info['PNG Support'];
                         break;
                 }
                 if ($imageHeader) {
                     // cannot create image for whatever reason (maybe ImageCreateFromJPEG et al are not available?)
                     // and ImageMagick is not available either, no choice but to output original (not resized/modified) data and exit
                     if ($this->config_error_die_on_source_failure && !$this->phpThumbDebug) {
                         $this->ErrorImage('All attempts to create GD image source failed (' . ($GDreadSupport ? 'source image probably corrupt' : 'GD does not have read support for "' . $imageHeader . '"') . '), cannot generate thumbnail');
                     } else {
                         //$this->DebugMessage('All attempts to create GD image source failed ('.($GDreadSupport ? 'source image probably corrupt' : 'GD does not have read support for "'.$imageHeader.'"').'), outputing raw image', __FILE__, __LINE__);
                         //if (!$this->phpThumbDebug) {
                         //	header($imageHeader);
                         //	echo $this->rawImageData;
                         //	exit;
                         //}
                         return false;
                     }
                 }
                 switch (substr($this->rawImageData, 0, 2)) {
                     case 'BM':
                         ob_start();
                         if (!@(include_once dirname(__FILE__) . '/phpthumb.bmp.php')) {
                             ob_end_clean();
                             if ($this->phpThumbDebug) {
                                 $this->DebugMessage('include_once(' . dirname(__FILE__) . '/phpthumb.bmp.php) failed', __FILE__, __LINE__);
                                 return false;
                             }
                             return $this->ErrorImage('include_once(' . dirname(__FILE__) . '/phpthumb.bmp.php) failed');
                         }
                         ob_end_clean();
                         $phpthumb_bmp = new phpthumb_bmp();
                         if ($this->gdimg_source = $phpthumb_bmp->phpthumb_bmp2gd($this->rawImageData, phpthumb_functions::gd_version() >= 2.0)) {
                             $this->DebugMessage('$phpthumb_bmp->phpthumb_bmp2gd() succeeded', __FILE__, __LINE__);
                             break;
                         } elseif ($this->phpThumbDebug) {
                             $this->DebugMessage('phpthumb_bmp2db failed', __FILE__, __LINE__);
                             return false;
                         }
                         return $this->ErrorImage('ImageMagick is unavailable and phpThumb() does not support BMP source images without it');
                         break;
                 }
                 switch (substr($this->rawImageData, 0, 4)) {
                     case 'II' . "*":
                     case 'MM' . "*":
                         if ($this->phpThumbDebug) {
                             $this->DebugMessage('ImageMagick is unavailable and phpThumb() does not support TIFF source images without it', __FILE__, __LINE__);
                             return false;
                         }
                         return $this->ErrorImage('ImageMagick is unavailable and phpThumb() does not support TIFF source images without it');
                         break;
                     case "×ÍÆš":
                         if ($this->phpThumbDebug) {
                             $this->DebugMessage('ImageMagick is unavailable and phpThumb() does not support WMF source images without it', __FILE__, __LINE__);
                             return false;
                         }
                         return $this->ErrorImage('ImageMagick is unavailable and phpThumb() does not support WMF source images without it');
                         break;
                 }
                 if (!$this->gdimg_source) {
                     if ($this->phpThumbDebug) {
                         $this->DebugMessage('Unknown image type identified by "' . substr($this->rawImageData, 0, 4) . '" (' . phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)) . ') in SourceImageToGD()', __FILE__, __LINE__);
                         return false;
                     }
                     return $this->ErrorImage('Unknown image type identified by "' . substr($this->rawImageData, 0, 4) . '" (' . phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)) . ') in SourceImageToGD()');
                 }
             }
         }
     }
     if (!$this->gdimg_source) {
         if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
             $this->DebugMessage('All other attempts failed, but successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
             $this->gdimg_source = $gdimg_exif_temp;
             // 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->aoe = true;
             return true;
         }
         return false;
     }
     $this->source_width = ImageSX($this->gdimg_source);
     $this->source_height = ImageSY($this->gdimg_source);
     return true;
 }
 function ImageCreateFromStringReplacement(&$RawImageData, $DieOnErrors = false)
 {
     // there are serious bugs in the non-bundled versions of GD which may cause
     // PHP to segfault when calling ImageCreateFromString() - avoid if at all possible
     // when not using a bundled version of GD2
     $gd_info = phpthumb_functions::gd_info();
     if (strpos($gd_info['GD Version'], 'bundled') !== false) {
         return @ImageCreateFromString($RawImageData);
     }
     switch (substr($RawImageData, 0, 3)) {
         case 'GIF':
             $ICFSreplacementFunctionName = 'ImageCreateFromGIF';
             break;
         case "ÿØÿ":
             $ICFSreplacementFunctionName = 'ImageCreateFromJPEG';
             break;
         case "‰" . 'PN':
             $ICFSreplacementFunctionName = 'ImageCreateFromPNG';
             break;
         default:
             $this->ErrorImage('Unknown image type identified by "' . substr($this->rawImageData, 0, 3) . '" (' . phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 3)) . ') in ImageCreateFromStringReplacement()');
             break;
     }
     if ($tempnam = tempnam($this->config_temp_directory, 'pThumb')) {
         if ($fp_tempnam = @fopen($tempnam, 'wb')) {
             fwrite($fp_tempnam, $RawImageData);
             fclose($fp_tempnam);
             if ($ICFSreplacementFunctionName == 'ImageCreateFromGIF' && !function_exists($ICFSreplacementFunctionName)) {
                 // Need to create from GIF file, but ImageCreateFromGIF does not exist
                 if (@(include_once 'phpthumb.gif.php')) {
                     // gif_loadFileToGDimageResource() cannot read from raw data, write to file first
                     if ($tempfilename = tempnam($this->config_temp_directory, 'pThumb')) {
                         if ($fp_tempfile = @fopen($tempfilename, 'wb')) {
                             fwrite($fp_tempfile, $RawImageData);
                             fclose($fp_tempfile);
                             $gdimg_source = gif_loadFileToGDimageResource($tempfilename);
                             unlink($tempfilename);
                             return $gdimg_source;
                             break;
                         } else {
                             $ErrorMessage = 'Failed to open tempfile in ' . __FILE__ . ' on line ' . __LINE__;
                         }
                     } else {
                         $ErrorMessage = 'Failed to open generate tempfile name in ' . __FILE__ . ' on line ' . __LINE__;
                     }
                 } else {
                     $ErrorMessage = 'Failed to include required file "phpthumb.gif.php" in ' . __FILE__ . ' on line ' . __LINE__;
                 }
             } elseif (function_exists($ICFSreplacementFunctionName) && ($gdimg_source = $ICFSreplacementFunctionName($tempnam))) {
                 // great
                 unlink($tempnam);
                 return $gdimg_source;
             } else {
                 // GD functions not available
                 // base64-encoded error image in GIF format
                 $ERROR_NOGD = 'R0lGODlhIAAgALMAAAAAABQUFCQkJDY2NkZGRldXV2ZmZnJycoaGhpSUlKWlpbe3t8XFxdXV1eTk5P7+/iwAAAAAIAAgAAAE/vDJSau9WILtTAACUinDNijZtAHfCojS4W5H+qxD8xibIDE9h0OwWaRWDIljJSkUJYsN4bihMB8th3IToAKs1VtYM75cyV8sZ8vygtOE5yMKmGbO4jRdICQCjHdlZzwzNW4qZSQmKDaNjhUMBX4BBAlmMywFSRWEmAI6b5gAlhNxokGhooAIK5o/pi9vEw4Lfj4OLTAUpj6IabMtCwlSFw0DCKBoFqwAB04AjI54PyZ+yY3TD0ss2YcVmN/gvpcu4TOyFivWqYJlbAHPpOntvxNAACcmGHjZzAZqzSzcq5fNjxFmAFw9iFRunD1epU6tsIPmFCAJnWYE0FURk7wJDA0MTKpEzoWAAskiAAA7';
                 header('Content-type: image/gif');
                 echo base64_decode($ERROR_NOGD);
                 exit;
             }
         } else {
             $ErrorMessage = 'Failed to fopen(' . $tempnam . ', "wb") in ' . __FILE__ . ' on line ' . __LINE__ . "\n" . 'You may need to set $PHPTHUMB_CONFIG[temp_directory] in phpthumb.config.php';
         }
         unlink($tempnam);
     } else {
         $ErrorMessage = 'Failed to generate tempnam() in ' . __FILE__ . ' on line ' . __LINE__ . "\n" . 'You may need to set $PHPTHUMB_CONFIG[temp_directory] in phpthumb.config.php';
     }
     if ($DieOnErrors && !empty($ErrorMessage)) {
         die($ErrorMessage);
     }
     return false;
 }
 function SourceImageToGD()
 {
     if (is_resource($this->gdimg_source)) {
         $this->DebugMessage('skipping SourceImageToGD() because $this->gdimg_source is already a resource', __FILE__, __LINE__);
         return true;
     }
     $this->DebugMessage('starting SourceImageToGD()', __FILE__, __LINE__);
     if ($this->config_use_exif_thumbnail_for_speed && !empty($this->exif_thumbnail_data)) {
         if ($this->exif_thumbnail_width >= $this->thumbnail_image_width && $this->exif_thumbnail_height >= $this->thumbnail_image_height && $this->thumbnailCropX == 0 && $this->thumbnailCropY == 0 && $this->thumbnailCropW > 0 && $this->thumbnailCropH > 0 && $this->source_width >= $this->thumbnailCropW && $this->source_height >= $this->thumbnailCropH) {
             // EXIF thumbnail exists, and is equal to or larger than destination thumbnail, and will be use as source image
             // Only benefit here is greater speed, not lower memory usage
             $this->DebugMessage('Trying to use EXIF thumbnail as source image', __FILE__, __LINE__);
             if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
                 $this->DebugMessage('Successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
                 $this->gdimg_source = $gdimg_exif_temp;
                 $this->source_width = $this->exif_thumbnail_width;
                 $this->source_height = $this->exif_thumbnail_height;
                 $this->thumbnailCropW = $this->source_width;
                 $this->thumbnailCropH = $this->source_height;
                 return true;
             } else {
                 $this->DebugMessage('$this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false) failed', __FILE__, __LINE__);
             }
         } else {
             $this->DebugMessage('Not using EXIF thumbnail data because EXIF thumbnail is too small', __FILE__, __LINE__);
         }
     } else {
         if (!$this->config_use_exif_thumbnail_for_speed) {
             $this->DebugMessage('Not using EXIF thumbnail data because $this->config_use_exif_thumbnail_for_speed is FALSE', __FILE__, __LINE__);
         } elseif ($this->getimagesizeinfo[2] == 2) {
             $this->DebugMessage('Not using EXIF thumbnail data because EXIF thumbnail is unavailable', __FILE__, __LINE__);
         } elseif (is_array($this->getimagesizeinfo)) {
             $this->DebugMessage('Not using EXIF thumbnail data because source image is not JPEG, therefore no EXIF thumbnail available', __FILE__, __LINE__);
             //} else {
             //	$this->DebugMessage('Not using EXIF thumbnail data because GetImageSize failed on source image', __FILE__, __LINE__);
         }
     }
     if (empty($this->gdimg_source)) {
         // try to create GD image source directly via GD, if possible,
         // rather than buffering to memory and creating with ImageCreateFromString
         $ImageCreateWasAttempted = false;
         if (!empty($this->rawImageData)) {
             // fine
         } elseif ($this->iswindows && (substr($this->sourceFilename, 0, 2) == '//' || substr($this->sourceFilename, 0, 2) == '\\\\')) {
             // Windows \\share\filename.ext
         } elseif (eregi('^(f|ht)tp[s]?://', $this->sourceFilename)) {
             // URL
         } elseif (!file_exists($this->sourceFilename)) {
             return $this->ErrorImage('"' . $this->sourceFilename . '" does not exist');
         } elseif (!is_file($this->sourceFilename)) {
             return $this->ErrorImage('"' . $this->sourceFilename . '" is not a file');
         }
         $ImageCreateFromFunction = array(1 => 'ImageCreateFromGIF', 2 => 'ImageCreateFromJPEG', 3 => 'ImageCreateFromPNG', 15 => 'ImageCreateFromWBMP');
         switch (@$this->getimagesizeinfo[2]) {
             case 1:
                 // GIF
             // GIF
             case 2:
                 // JPEG
             // JPEG
             case 3:
                 // PNG
             // PNG
             case 15:
                 // WBMP
                 $ImageCreateFromFunctionName = $ImageCreateFromFunction[$this->getimagesizeinfo[2]];
                 if (function_exists($ImageCreateFromFunctionName)) {
                     $this->DebugMessage('Calling ' . $ImageCreateFromFunctionName . '(' . $this->sourceFilename . ')', __FILE__, __LINE__);
                     $ImageCreateWasAttempted = true;
                     $this->gdimg_source = @$ImageCreateFromFunctionName($this->sourceFilename);
                     switch ($this->getimagesizeinfo[2]) {
                         case 1:
                         case 3:
                             // GIF or PNG input file may have transparency
                             $this->is_alpha = true;
                             break;
                     }
                 } else {
                     $this->DebugMessage('NOT calling ' . $ImageCreateFromFunctionName . '(' . $this->sourceFilename . ') because !function_exists(' . $ImageCreateFromFunctionName . ')', __FILE__, __LINE__);
                 }
                 break;
             case 4:
                 // SWF
             // SWF
             case 5:
                 // PSD
             // PSD
             case 6:
                 // BMP
             // BMP
             case 7:
                 // TIFF (LE)
             // TIFF (LE)
             case 8:
                 // TIFF (BE)
             // TIFF (BE)
             case 9:
                 // JPC
             // JPC
             case 10:
                 // JP2
             // JP2
             case 11:
                 // JPX
             // JPX
             case 12:
                 // JB2
             // JB2
             case 13:
                 // SWC
             // SWC
             case 14:
                 // IFF
             // IFF
             case 16:
                 // XBM
                 $this->DebugMessage('No built-in image creation function for image type "' . @$this->getimagesizeinfo[2] . '" ($this->getimagesizeinfo[2])', __FILE__, __LINE__);
                 break;
             case '':
                 // no source file, source image was probably set by setSourceData()
                 break;
             default:
                 $this->DebugMessage('Unknown value for $this->getimagesizeinfo[2]: "' . @$this->getimagesizeinfo[2] . '"', __FILE__, __LINE__);
                 break;
         }
         if (empty($this->gdimg_source)) {
             // cannot create from filename, attempt to create source image with ImageCreateFromString, if possible
             if ($ImageCreateWasAttempted) {
                 $this->DebugMessage(@$ImageCreateFromFunctionName . '() was attempted but FAILED', __FILE__, __LINE__);
             }
             if (empty($this->rawImageData)) {
                 $this->DebugMessage('Populating $this->rawImageData and attempting ImageCreateFromStringReplacement()', __FILE__, __LINE__);
                 if ($fp = @fopen($this->sourceFilename, 'rb')) {
                     $this->rawImageData = '';
                     $filesize = filesize($this->sourceFilename);
                     $blocksize = 16384;
                     $blockreads = ceil($filesize / $blocksize);
                     for ($i = 0; $i < $blockreads; $i++) {
                         $this->rawImageData .= fread($fp, $blocksize);
                     }
                     fclose($fp);
                 } else {
                     return $this->ErrorImage('cannot fopen("' . $this->sourceFilename . '") on line ' . __LINE__ . ' of ' . __FILE__);
                 }
             }
             $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->rawImageData, true);
         }
         if (empty($this->gdimg_source)) {
             $this->DebugMessage('$this->gdimg_source is still empty', __FILE__, __LINE__);
             if ($this->ImageMagickThumbnailToGD()) {
                 // excellent, we have a thumbnailed source image
                 $this->DebugMessage('ImageMagickThumbnailToGD() succeeded', __FILE__, __LINE__);
             } else {
                 $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
                 $imageHeader = '';
                 switch (substr($this->rawImageData, 0, 3)) {
                     case 'GIF':
                         $imageHeader = 'Content-type: image/gif';
                         break;
                     case "ÿØÿ":
                         $imageHeader = 'Content-type: image/jpeg';
                         break;
                     case "‰" . 'PN':
                         $imageHeader = 'Content-type: image/png';
                         break;
                 }
                 if ($imageHeader) {
                     // cannot create image for whatever reason (maybe ImageCreateFromJPEG et al are not available?)
                     // and ImageMagick is not available either, no choice but to output original (not resized/modified) data and exit
                     if ($this->config_error_die_on_source_failure) {
                         $this->ErrorImage('All attempts to create GD image source failed (source image probably corrupt), cannot generate thumbnail');
                     } else {
                         $this->DebugMessage('All attempts to create GD image source failed, outputing raw image', __FILE__, __LINE__);
                         header($imageHeader);
                         echo $this->rawImageData;
                         exit;
                     }
                 }
                 switch (substr($this->rawImageData, 0, 2)) {
                     case 'BM':
                         if (@(include_once 'phpthumb.bmp.php')) {
                             $phpthumb_bmp = new phpthumb_bmp();
                             if ($this->gdimg_source = $phpthumb_bmp->phpthumb_bmp2gd($this->rawImageData, phpthumb_functions::gd_version() >= 2.0)) {
                                 $this->DebugMessage('$phpthumb_bmp->phpthumb_bmp2gd() succeeded', __FILE__, __LINE__);
                                 break;
                             } else {
                                 return $this->ErrorImage('phpthumb_bmp2db failed');
                             }
                         } else {
                             return $this->ErrorImage('include_once(phpthumb.bmp.php) failed');
                         }
                         return $this->ErrorImage('ImageMagick is unavailable and phpThumb() does not support BMP source images without it');
                         break;
                 }
                 switch (substr($this->rawImageData, 0, 4)) {
                     case 'II' . "*":
                     case 'MM' . "*":
                         return $this->ErrorImage('ImageMagick is unavailable and phpThumb() does not support TIFF source images without it');
                         break;
                     case "×ÍÆš":
                         return $this->ErrorImage('ImageMagick is unavailable and phpThumb() does not support WMF source images without it');
                         break;
                 }
                 if (empty($this->gdimg_source)) {
                     return $this->ErrorImage('Unknown image type identified by "' . substr($this->rawImageData, 0, 4) . '" (' . phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)) . ') in SourceImageToGD()');
                 }
             }
         }
     }
     $this->source_width = ImageSX($this->gdimg_source);
     $this->source_height = ImageSY($this->gdimg_source);
     return true;
 }