1<?php
2//////////////////////////////////////////////////////////////
3//   phpThumb() by James Heinrich <info@silisoftware.com>   //
4//        available at http://phpthumb.sourceforge.net      //
5//         and/or https://github.com/JamesHeinrich/phpThumb //
6//////////////////////////////////////////////////////////////
7///                                                         //
8// See: phpthumb.readme.txt for usage instructions          //
9//                                                         ///
10//////////////////////////////////////////////////////////////
11
12if (!class_exists('phpthumb_functions', false)) {
13	ob_start();
14	if (!include_once __DIR__ . '/phpthumb.functions.php') {
15		ob_end_flush();
16		die('failed to include_once("' . __DIR__ . '/phpthumb.functions.php")');
17	}
18	ob_end_clean();
19}
20
21// make sure all image type constants are defined, even in older PHP versions that don't natively support them
22$predefined_IMG_constants = array(
23	'IMG_GIF'           =>   1,
24	'IMG_JPG'           =>   2, // not a typo, both IMG_JPG and IMG_JPEG have a value of "2"
25	'IMG_JPEG'          =>   2, // not a typo, both IMG_JPG and IMG_JPEG have a value of "2"
26	'IMG_PNG'           =>   4,
27	'IMG_WBMP'          =>   8,
28	'IMG_XPM'           =>  16,
29	'IMG_WEBP'          =>  32, // PHP 7.0.10
30	'IMG_BMP'           =>  64, // PHP 7.2.0
31	'IMG_WEBP_LOSSLESS' => 101, // PHP 8.1.0
32	'IMG_AVIF'          => 256, // PHP 8.1.0
33);
34$predefined_IMAGETYPE_constants = array(
35	'IMAGETYPE_GIF'       =>  1,
36	'IMAGETYPE_JPEG'      =>  2,
37	'IMAGETYPE_PNG'       =>  3,
38	'IMAGETYPE_SWF'       =>  4,
39	'IMAGETYPE_PSD'       =>  5,
40	'IMAGETYPE_BMP'       =>  6,
41	'IMAGETYPE_TIFF_II'   =>  7,
42	'IMAGETYPE_TIFF_MM'   =>  8,
43	'IMAGETYPE_JPC'       =>  9,
44	'IMAGETYPE_JP2'       => 10,
45	'IMAGETYPE_JPX'       => 11,
46	'IMAGETYPE_JB2'       => 12,
47	'IMAGETYPE_SWC'       => 13,
48	'IMAGETYPE_IFF'       => 14,
49	'IMAGETYPE_WBMP'      => 15,
50	'IMAGETYPE_XBM'       => 16,
51	'IMAGETYPE_ICO'       => 17,
52	'IMAGETYPE_WEBP'      => 18, // PHP 7.0.10
53	'IMAGETYPE_AVIF'      => 19, // PHP 8.1.0
54);
55foreach ($predefined_IMG_constants as $PHP_constant_name => $PHP_constant_value) {
56	if (!defined($PHP_constant_name)) {
57		define($PHP_constant_name, $PHP_constant_value);
58	}
59}
60foreach ($predefined_IMAGETYPE_constants as $PHP_constant_name => $PHP_constant_value) {
61	if (!defined($PHP_constant_name)) {
62		define($PHP_constant_name, $PHP_constant_value);
63	}
64}
65unset($predefined_IMG_constants, $predefined_IMAGETYPE_constants, $PHP_constant_name, $PHP_constant_value);
66
67
68class phpthumb {
69
70	// public:
71	// START PARAMETERS (for object mode and phpThumb.php)
72	// See phpthumb.readme.txt for descriptions of what each of these values are
73	public $src  = null;     // SouRCe filename
74	public $new  = null;     // NEW image (phpThumb.php only)
75	public $w    = null;     // Width
76	public $h    = null;     // Height
77	public $wp   = null;     // Width  (Portrait Images Only)
78	public $hp   = null;     // Height (Portrait Images Only)
79	public $wl   = null;     // Width  (Landscape Images Only)
80	public $hl   = null;     // Height (Landscape Images Only)
81	public $ws   = null;     // Width  (Square Images Only)
82	public $hs   = null;     // Height (Square Images Only)
83	public $f    = null;     // output image Format
84	public $q    = 75;       // jpeg output Quality
85	public $sx   = null;     // Source crop top-left X position
86	public $sy   = null;     // Source crop top-left Y position
87	public $sw   = null;     // Source crop Width
88	public $sh   = null;     // Source crop Height
89	public $zc   = null;     // Zoom Crop
90	public $ica  = null;     // Image Crop Auto
91	public $bc   = null;     // Border Color
92	public $bg   = null;     // BackGround color
93	public $fltr = array();  // FiLTeRs
94	public $goto = null;     // GO TO url after processing
95	public $err  = null;     // default ERRor image filename
96	public $xto  = null;     // extract eXif Thumbnail Only
97	public $ra   = null;     // Rotate by Angle
98	public $ar   = null;     // Auto Rotate
99	public $aoe  = null;     // Allow Output Enlargement
100	public $far  = null;     // Fixed Aspect Ratio
101	public $iar  = null;     // Ignore Aspect Ratio
102	public $maxb = null;     // MAXimum Bytes
103	public $down = null;     // DOWNload thumbnail filename
104	public $md5s = null;     // MD5 hash of Source image
105	public $sfn  = 0;        // Source Frame Number
106	public $dpi  = 150;      // Dots Per Inch for vector source formats
107	public $sia  = null;     // Save Image As filename
108
109	public $file = null;     // >>>deprecated, DO NOT USE, will be removed in future versions<<<
110
111	public $phpThumbDebug = null;
112	// END PARAMETERS
113
114
115	// public:
116	// START CONFIGURATION OPTIONS (for object mode only)
117	// See phpThumb.config.php for descriptions of what each of these settings do
118
119	// * Directory Configuration
120	public $config_cache_directory                      = null;
121	public $config_cache_directory_depth                = 0;
122	public $config_cache_disable_warning                = true;
123	public $config_cache_source_enabled                 = false;
124	public $config_cache_source_directory               = null;
125	public $config_temp_directory                       = null;
126	public $config_document_root                        = null;
127
128	// * Default output configuration:
129	public $config_output_format                        = 'jpeg';
130	public $config_output_maxwidth                      = 0;
131	public $config_output_maxheight                     = 0;
132	public $config_output_interlace                     = true;
133
134	// * Error message configuration
135	public $config_error_image_width                    = 400;
136	public $config_error_image_height                   = 100;
137	public $config_error_message_image_default          = '';
138	public $config_error_bgcolor                        = 'CCCCFF';
139	public $config_error_textcolor                      = 'FF0000';
140	public $config_error_fontsize                       = 1;
141	public $config_error_die_on_error                   = false;
142	public $config_error_silent_die_on_error            = false;
143	public $config_error_die_on_source_failure          = true;
144
145	// * Anti-Hotlink Configuration:
146	public $config_nohotlink_enabled                    = true;
147	public $config_nohotlink_valid_domains              = array();
148	public $config_nohotlink_erase_image                = true;
149	public $config_nohotlink_text_message               = 'Off-server thumbnailing is not allowed';
150	// * Off-server Linking Configuration:
151	public $config_nooffsitelink_enabled                = false;
152	public $config_nooffsitelink_valid_domains          = array();
153	public $config_nooffsitelink_require_refer          = false;
154	public $config_nooffsitelink_erase_image            = true;
155	public $config_nooffsitelink_watermark_src          = '';
156	public $config_nooffsitelink_text_message           = 'Off-server linking is not allowed';
157
158	// * Border & Background default colors
159	public $config_border_hexcolor                      = '000000';
160	public $config_background_hexcolor                  = 'FFFFFF';
161
162	// * TrueType Fonts
163	public $config_ttf_directory                        = './fonts';
164
165	public $config_max_source_pixels                    = null;
166	public $config_use_exif_thumbnail_for_speed         = false;
167	public $config_allow_local_http_src                 = false;
168
169	public $config_imagemagick_path                     = null;
170	public $config_prefer_imagemagick                   = true;
171	public $config_imagemagick_use_thumbnail            = true;
172
173	public $config_cache_maxage                         = null;
174	public $config_cache_maxsize                        = null;
175	public $config_cache_maxfiles                       = null;
176	public $config_cache_source_filemtime_ignore_local  = false;
177	public $config_cache_source_filemtime_ignore_remote = true;
178	public $config_cache_default_only_suffix            = false;
179	public $config_cache_force_passthru                 = true;
180	public $config_cache_prefix                         = '';    // default value set in the constructor below
181
182	// * MySQL
183	public $config_mysql_extension                      = null;
184	public $config_mysql_query                          = null;
185	public $config_mysql_hostname                       = null;
186	public $config_mysql_username                       = null;
187	public $config_mysql_password                       = null;
188	public $config_mysql_database                       = null;
189
190	// * Security
191	public $config_high_security_enabled                = true;
192	public $config_high_security_password               = null;
193	public $config_high_security_url_separator          = '&';
194	public $config_disable_debug                        = true;
195	public $config_allow_src_above_docroot              = false;
196	public $config_allow_src_above_phpthumb             = true;
197	public $config_auto_allow_symlinks                  = true;    // allow symlink target directories without explicitly whitelisting them
198	public $config_additional_allowed_dirs              = array(); // additional directories to allow source images to be read from
199	public $config_file_create_mask                     = 0755;
200	public $config_dir_create_mask                      = 0755;
201
202	// * HTTP fopen
203	public $config_http_fopen_timeout                   = 10;
204	public $config_http_follow_redirect                 = true;
205
206	// * Compatability
207	public $config_disable_pathinfo_parsing             = false;
208	public $config_disable_imagecopyresampled           = false;
209	public $config_disable_onlycreateable_passthru      = false;
210	public $config_disable_realpath                     = false;
211
212	public $config_http_user_agent                      = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7';
213
214	// END CONFIGURATION OPTIONS
215
216
217	// public: error messages (read-only; persistant)
218	public $debugmessages = array();
219	public $debugtiming   = array();
220	public $fatalerror    = null;
221
222
223	// private: (should not be modified directly)
224	public $thumbnailQuality = 75;
225	public $thumbnailFormat  = null;
226
227	public $sourceFilename   = null;
228	public $rawImageData     = null;
229	public $IMresizedData    = null;
230	public $outputImageData  = null;
231
232	public $useRawIMoutput   = false;
233
234	public $gdimg_output     = null;
235	public $gdimg_source     = null;
236
237	public $getimagesizeinfo = null;
238
239	public $source_width  = null;
240	public $source_height = null;
241
242	public $thumbnailCropX = null;
243	public $thumbnailCropY = null;
244	public $thumbnailCropW = null;
245	public $thumbnailCropH = null;
246
247	public $exif_thumbnail_width  = null;
248	public $exif_thumbnail_height = null;
249	public $exif_thumbnail_type   = null;
250	public $exif_thumbnail_data   = null;
251	public $exif_raw_data         = null;
252
253	public $thumbnail_width        = null;
254	public $thumbnail_height       = null;
255	public $thumbnail_image_width  = null;
256	public $thumbnail_image_height = null;
257
258	public $tempFilesToDelete = array();
259	public $cache_filename    = null;
260
261	public $AlphaCapableFormats = array( 'png', 'ico', 'gif', 'webp', 'avif');
262	public $is_alpha = false;
263
264	public $iswindows        = null;
265	public $issafemode       = null;
266	public $php_memory_limit = null;
267
268	public $phpthumb_version = '1.7.19-202210110924';
269
270	//////////////////////////////////////////////////////////////////////
271
272	// public: constructor
273	public function __construct() {
274		$this->phpThumb();
275	}
276
277	public function phpThumb() {
278		$this->DebugTimingMessage('phpThumb() constructor', __FILE__, __LINE__);
279		$this->DebugMessage('phpThumb() v'.$this->phpthumb_version, __FILE__, __LINE__);
280
281		foreach (array(ini_get('memory_limit'), get_cfg_var('memory_limit')) as $php_config_memory_limit) {
282			if (!empty($php_config_memory_limit)) {
283				if (strtoupper($php_config_memory_limit[ strlen($php_config_memory_limit) - 1 ]) == 'G') { // PHP memory limit expressed in Gigabytes
284					$php_config_memory_limit = (int) substr($php_config_memory_limit, 0, -1) * 1073741824;
285				} elseif (strtoupper($php_config_memory_limit[ strlen($php_config_memory_limit) - 1 ]) == 'M') { // PHP memory limit expressed in Megabytes
286					$php_config_memory_limit = (int) substr($php_config_memory_limit, 0, -1) * 1048576;
287				}
288				$this->php_memory_limit = max($this->php_memory_limit, $php_config_memory_limit);
289			}
290		}
291		if ($this->php_memory_limit > 0) { // could be "-1" for "no limit"
292			$this->config_max_source_pixels = round($this->php_memory_limit * 0.20); // 20% of memory_limit
293		}
294
295		$this->iswindows  = (bool) (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
296		$this->issafemode = (bool) preg_match('#(1|ON)#i', ini_get('safe_mode'));
297		$this->config_document_root = (!empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT']   : $this->config_document_root);
298		$this->config_cache_prefix  = ( isset($_SERVER['SERVER_NAME'])   ? $_SERVER['SERVER_NAME'].'_' : '');
299
300		$this->purgeTempFiles(); // purge existing temp files if re-initializing object
301
302		$php_sapi_name = strtolower(function_exists('php_sapi_name') ? PHP_SAPI : '');
303		if ($php_sapi_name == 'cli') {
304			$this->config_allow_src_above_docroot = true;
305		}
306
307		if (!$this->config_disable_debug) {
308			// if debug mode is enabled, force phpThumbDebug output, do not allow normal thumbnails to be generated
309			$this->phpThumbDebug = (null === $this->phpThumbDebug ? 9 : max(1, (int) $this->phpThumbDebug));
310		}
311	}
312
313	public function __destruct() {
314		$this->purgeTempFiles();
315	}
316
317	// public:
318	public function purgeTempFiles() {
319		foreach ($this->tempFilesToDelete as $tempFileToDelete) {
320			if (file_exists($tempFileToDelete)) {
321				$this->DebugMessage('Deleting temp file "'.$tempFileToDelete.'"', __FILE__, __LINE__);
322				@unlink($tempFileToDelete);
323			}
324		}
325		$this->tempFilesToDelete = array();
326		return true;
327	}
328
329	// public:
330	public function setSourceFilename($sourceFilename) {
331		//$this->resetObject();
332		//$this->rawImageData   = null;
333		$this->sourceFilename = $sourceFilename;
334		$this->src            = $sourceFilename;
335		if (null === $this->config_output_format) {
336			$sourceFileExtension = strtolower(substr(strrchr($sourceFilename, '.'), 1));
337			if (preg_match('#^[a-z]{3,4}$#', $sourceFileExtension)) {
338				$this->config_output_format = $sourceFileExtension;
339				$this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->config_output_format to "'.$sourceFileExtension.'"', __FILE__, __LINE__);
340			} else {
341				$this->DebugMessage('setSourceFilename('.$sourceFilename.') did NOT set $this->config_output_format to "'.$sourceFileExtension.'" because it did not seem like an appropriate image format', __FILE__, __LINE__);
342			}
343		}
344		$this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->sourceFilename to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
345		return true;
346	}
347
348	// public:
349	public function setSourceData($rawImageData, $sourceFilename='') {
350		//$this->resetObject();
351		//$this->sourceFilename = null;
352		$this->rawImageData   = $rawImageData;
353		$this->DebugMessage('setSourceData() setting $this->rawImageData ('.strlen($this->rawImageData).' bytes; magic="'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).'))', __FILE__, __LINE__);
354		if ($this->config_cache_source_enabled) {
355			$sourceFilename = ($sourceFilename ? $sourceFilename : md5($rawImageData));
356			if (!is_dir($this->config_cache_source_directory)) {
357				$this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not a directory');
358			} elseif (!@is_writable($this->config_cache_source_directory)) {
359				$this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not writable');
360			}
361			$this->DebugMessage('setSourceData() attempting to save source image to "'.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).'"', __FILE__, __LINE__);
362			if ($fp = @fopen($this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename), 'wb')) {
363				fwrite($fp, $rawImageData);
364				fclose($fp);
365			} elseif (!$this->phpThumbDebug) {
366				$this->ErrorImage('setSourceData() failed to write to source cache ('.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).')');
367			}
368		}
369		return true;
370	}
371
372	// public:
373	public function setSourceImageResource($gdimg) {
374		//$this->resetObject();
375		$this->gdimg_source = $gdimg;
376		return true;
377	}
378
379	// public:
380	public function setParameter($param, $value) {
381		if ($param == 'src') {
382			$this->setSourceFilename($this->ResolveFilenameToAbsolute($value));
383		} elseif (@is_array($this->$param)) {
384			if (is_array($value)) {
385				foreach ($value as $arraykey => $arrayvalue) {
386					array_push($this->$param, $arrayvalue);
387				}
388			} else {
389				array_push($this->$param, $value);
390			}
391		} else {
392			$this->$param = $value;
393		}
394		return true;
395	}
396
397	// public:
398	public function getParameter($param) {
399		//if (property_exists('phpThumb', $param)) {
400			return $this->$param;
401		//}
402		//$this->DebugMessage('setParameter() attempting to get non-existant parameter "'.$param.'"', __FILE__, __LINE__);
403		//return false;
404	}
405
406
407	// public:
408	public function GenerateThumbnail() {
409
410		$this->setOutputFormat();
411			$this->phpThumbDebug('8a');
412		$this->ResolveSource();
413			$this->phpThumbDebug('8b');
414		$this->SetCacheFilename();
415			$this->phpThumbDebug('8c');
416		$this->ExtractEXIFgetImageSize();
417			$this->phpThumbDebug('8d');
418		if ($this->useRawIMoutput) {
419			$this->DebugMessage('Skipping rest of GenerateThumbnail() because ($this->useRawIMoutput == true)', __FILE__, __LINE__);
420			return true;
421		}
422			$this->phpThumbDebug('8e');
423		if (!$this->SourceImageToGD()) {
424			$this->DebugMessage('SourceImageToGD() failed', __FILE__, __LINE__);
425			return false;
426		}
427			$this->phpThumbDebug('8f');
428		$this->ImageCropAuto();
429			$this->phpThumbDebug('8h');
430		$this->Rotate();
431			$this->phpThumbDebug('8h');
432		$this->CreateGDoutput();
433			$this->phpThumbDebug('8i');
434
435		// default values, also applicable for far="C"
436		$destination_offset_x = round(($this->thumbnail_width  - $this->thumbnail_image_width)  / 2);
437		$destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
438		if (($this->far == 'L') || ($this->far == 'TL') || ($this->far == 'BL')) {
439			$destination_offset_x = 0;
440		}
441		if (($this->far == 'R') || ($this->far == 'TR') || ($this->far == 'BR')) {
442			$destination_offset_x =  round($this->thumbnail_width  - $this->thumbnail_image_width);
443		}
444		if (($this->far == 'T') || ($this->far == 'TL') || ($this->far == 'TR')) {
445			$destination_offset_y = 0;
446		}
447		if (($this->far == 'B') || ($this->far == 'BL') || ($this->far == 'BR')) {
448			$destination_offset_y =  round($this->thumbnail_height - $this->thumbnail_image_height);
449		}
450
451//		// copy/resize image to appropriate dimensions
452//		$borderThickness = 0;
453//		if (!empty($this->fltr)) {
454//			foreach ($this->fltr as $key => $value) {
455//				if (preg_match('#^bord\|([0-9]+)#', $value, $matches)) {
456//					$borderThickness = $matches[1];
457//					break;
458//				}
459//			}
460//		}
461//		if ($borderThickness > 0) {
462//			//$this->DebugMessage('Skipping ImageResizeFunction() because BorderThickness="'.$borderThickness.'"', __FILE__, __LINE__);
463//			$this->thumbnail_image_height /= 2;
464//		}
465		$this->ImageResizeFunction(
466			$this->gdimg_output,
467			$this->gdimg_source,
468			$destination_offset_x,
469			$destination_offset_y,
470			$this->thumbnailCropX,
471			$this->thumbnailCropY,
472			$this->thumbnail_image_width,
473			$this->thumbnail_image_height,
474			$this->thumbnailCropW,
475			$this->thumbnailCropH
476		);
477
478		$this->DebugMessage('memory_get_usage() after copy-resize = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
479		imagedestroy($this->gdimg_source);
480		$this->DebugMessage('memory_get_usage() after imagedestroy = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
481
482			$this->phpThumbDebug('8i');
483		$this->AntiOffsiteLinking();
484			$this->phpThumbDebug('8j');
485		$this->ApplyFilters();
486			$this->phpThumbDebug('8k');
487		$this->AlphaChannelFlatten();
488			$this->phpThumbDebug('8l');
489		$this->MaxFileSize();
490			$this->phpThumbDebug('8m');
491
492		$this->DebugMessage('GenerateThumbnail() completed successfully', __FILE__, __LINE__);
493		return true;
494	}
495
496
497	// public:
498	public function RenderOutput() {
499		if (!$this->useRawIMoutput && !(is_resource($this->gdimg_output) || (is_object($this->gdimg_source) && $this->gdimg_source instanceOf \GdImage))) {
500			$this->DebugMessage('RenderOutput() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
501			return false;
502		}
503		if (!$this->thumbnailFormat) {
504			$this->DebugMessage('RenderOutput() failed because $this->thumbnailFormat is empty', __FILE__, __LINE__);
505			return false;
506		}
507		if ($this->useRawIMoutput) {
508			$this->DebugMessage('RenderOutput copying $this->IMresizedData ('.strlen($this->IMresizedData).' bytes) to $this->outputImage', __FILE__, __LINE__);
509			$this->outputImageData = $this->IMresizedData;
510			return true;
511		}
512
513		$builtin_formats = array();
514		if (function_exists('imagetypes')) {
515			$imagetypes = imagetypes();
516			$builtin_formats['wbmp'] = (bool) ($imagetypes & IMG_WBMP);
517			$builtin_formats['jpg']  = (bool) ($imagetypes & IMG_JPG);
518			$builtin_formats['gif']  = (bool) ($imagetypes & IMG_GIF);
519			$builtin_formats['png']  = (bool) ($imagetypes & IMG_PNG);
520			$builtin_formats['webp'] = (bool) ($imagetypes & IMG_WEBP); // PHP 7.0.10
521			$builtin_formats['bmp']  = (bool) ($imagetypes & IMG_BMP);  // PHP 7.2.0
522			$builtin_formats['avif'] = (bool) ($imagetypes & IMG_AVIF); // PHP 8.1.0
523		}
524
525		$this->DebugMessage('imageinterlace($this->gdimg_output, '. (int) $this->config_output_interlace .')', __FILE__, __LINE__);
526		imageinterlace($this->gdimg_output, (int) $this->config_output_interlace);
527
528		$this->DebugMessage('RenderOutput() attempting image'.strtolower(@$this->thumbnailFormat).'($this->gdimg_output)', __FILE__, __LINE__);
529		ob_start();
530		switch ($this->thumbnailFormat) {
531			case 'wbmp':
532				if (empty($builtin_formats['wbmp'])) {
533					$this->DebugMessage('GD does not have required built-in support for WBMP output', __FILE__, __LINE__);
534					ob_end_clean();
535					return false;
536				}
537				imagewbmp($this->gdimg_output, null, $this->thumbnailQuality);
538				$this->outputImageData = ob_get_contents();
539				break;
540
541			case 'jpeg':
542			case 'jpg':  // should be "jpeg" not "jpg" but just in case...
543				if (empty($builtin_formats['jpg'])) {
544					$this->DebugMessage('GD does not have required built-in support for JPEG output', __FILE__, __LINE__);
545					ob_end_clean();
546					return false;
547				}
548				imagejpeg($this->gdimg_output, null, $this->thumbnailQuality);
549				$this->outputImageData = ob_get_contents();
550				break;
551
552			case 'png':
553				if (empty($builtin_formats['png'])) {
554					$this->DebugMessage('GD does not have required built-in support for PNG output', __FILE__, __LINE__);
555					ob_end_clean();
556					return false;
557				}
558				if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.1.2', '>=')) {
559					// https://github.com/JamesHeinrich/phpThumb/issues/24
560
561					/* http://php.net/manual/en/function.imagepng.php:
562					from php source (gd.h):
563					2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all,
564					:: 1 is FASTEST but produces larger files, 9 provides the best
565					:: compression (smallest files) but takes a long time to compress, and
566					:: -1 selects the default compiled into the zlib library.
567					Conclusion: Based on the Zlib manual (http://www.zlib.net/manual.html) the default compression level is set to 6.
568					*/
569					if (($this->thumbnailQuality >= -1) && ($this->thumbnailQuality <= 9)) {
570						$PNGquality = $this->thumbnailQuality;
571					} else {
572						$this->DebugMessage('Specified thumbnailQuality "'.$this->thumbnailQuality.'" is outside the accepted range (0-9, or -1). Using 6 as default value.', __FILE__, __LINE__);
573						$PNGquality = 6;
574					}
575					imagepng($this->gdimg_output, null, $PNGquality);
576				} else {
577					imagepng($this->gdimg_output);
578				}
579				$this->outputImageData = ob_get_contents();
580				break;
581
582			case 'gif':
583				if (empty($builtin_formats['gif'])) {
584					$this->DebugMessage('GD does not have required built-in support for GIF output', __FILE__, __LINE__);
585					ob_end_clean();
586					return false;
587				}
588				imagegif($this->gdimg_output);
589				$this->outputImageData = ob_get_contents();
590				break;
591
592			case 'webp':
593				if (empty($builtin_formats['webp'])) {
594					$this->DebugMessage('GD does not have required built-in support for WebP output', __FILE__, __LINE__);
595					ob_end_clean();
596					return false;
597				}
598				imagewebp($this->gdimg_output, null, $this->thumbnailQuality);
599				$this->outputImageData = ob_get_contents();
600				break;
601
602			case 'avif':
603				if (empty($builtin_formats['avif'])) {
604					$this->DebugMessage('GD does not have required built-in support for AVIF output', __FILE__, __LINE__);
605					ob_end_clean();
606					return false;
607				}
608				imageavif($this->gdimg_output, null, $this->thumbnailQuality);
609				$this->outputImageData = ob_get_contents();
610				break;
611
612			case 'bmp':
613				if (!empty($builtin_formats['bmp'])) {
614					imagebmp($this->gdimg_output);
615					$this->outputImageData = ob_get_contents();
616					break;
617				}
618				$this->DebugMessage('GD does not have required built-in support for BMP output', __FILE__, __LINE__);
619				if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
620					$this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
621					ob_end_clean();
622					return false;
623				}
624				$phpthumb_bmp = new phpthumb_bmp();
625				$this->outputImageData = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
626				unset($phpthumb_bmp);
627				break;
628
629			case 'ico':
630				if (!@include_once __DIR__ .'/phpthumb.ico.php' ) {
631					$this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
632					ob_end_clean();
633					return false;
634				}
635				$phpthumb_ico = new phpthumb_ico();
636				$arrayOfOutputImages = array($this->gdimg_output);
637				$this->outputImageData = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
638				unset($phpthumb_ico);
639				break;
640
641			default:
642				$this->DebugMessage('RenderOutput failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
643				ob_end_clean();
644				return false;
645		}
646		ob_end_clean();
647		if (!$this->outputImageData) {
648			$this->DebugMessage('RenderOutput() for "'.$this->thumbnailFormat.'" failed', __FILE__, __LINE__);
649			ob_end_clean();
650			return false;
651		}
652		$this->DebugMessage('RenderOutput() completing with $this->outputImageData = '.strlen($this->outputImageData).' bytes', __FILE__, __LINE__);
653		return true;
654	}
655
656
657	// public:
658	public function RenderToFile($filename) {
659		if (preg_match('#^[a-z0-9]+://#i', $filename)) {
660			$this->DebugMessage('RenderToFile() failed because $filename ('.$filename.') is a URL', __FILE__, __LINE__);
661			return false;
662		}
663		// render thumbnail to this file only, do not cache, do not output to browser
664		//$renderfilename = $this->ResolveFilenameToAbsolute(dirname($filename)).DIRECTORY_SEPARATOR.basename($filename);
665		$renderfilename = $filename;
666		if (($filename[0] != '/') && ($filename[0] != '\\') && ($filename[1] != ':')) {
667			$renderfilename = $this->ResolveFilenameToAbsolute($renderfilename);
668		}
669		if (!@is_writable(dirname($renderfilename))) {
670			$this->DebugMessage('RenderToFile() failed because "'.dirname($renderfilename).'/" is not writable', __FILE__, __LINE__);
671			return false;
672		}
673		if (@is_file($renderfilename) && !@is_writable($renderfilename)) {
674			$this->DebugMessage('RenderToFile() failed because "'.$renderfilename.'" is not writable', __FILE__, __LINE__);
675			return false;
676		}
677
678		if ($this->RenderOutput()) {
679			if (file_put_contents($renderfilename, $this->outputImageData)) {
680				@chmod($renderfilename, $this->getParameter('config_file_create_mask'));
681				$this->DebugMessage('RenderToFile('.$renderfilename.') succeeded', __FILE__, __LINE__);
682				return true;
683			}
684			if (!@file_exists($renderfilename)) {
685				$this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] did not appear to fail, but the output image does not exist either...', __FILE__, __LINE__);
686			}
687		} else {
688			$this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] failed', __FILE__, __LINE__);
689		}
690		return false;
691	}
692
693
694	// public:
695	public function OutputThumbnail() {
696		$this->purgeTempFiles();
697
698		if (!$this->useRawIMoutput && !(is_resource($this->gdimg_output) || (is_object($this->gdimg_source) && $this->gdimg_source instanceOf \GdImage))) {
699			$this->DebugMessage('OutputThumbnail() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
700			return false;
701		}
702		if (headers_sent()) {
703			return $this->ErrorImage('OutputThumbnail() failed - headers already sent');
704		}
705
706		$downloadfilename = phpthumb_functions::SanitizeFilename(is_string($this->sia) ? $this->sia : ($this->down ? $this->down : 'phpThumb_generated_thumbnail'.'.'.$this->thumbnailFormat));
707		$this->DebugMessage('Content-Disposition header filename set to "'.$downloadfilename.'"', __FILE__, __LINE__);
708		if ($downloadfilename) {
709			header('Content-Disposition: '.($this->down ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"');
710		} else {
711			$this->DebugMessage('failed to send Content-Disposition header because $downloadfilename is empty', __FILE__, __LINE__);
712		}
713
714		if ($this->useRawIMoutput) {
715
716			header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
717			echo $this->IMresizedData;
718
719		} else {
720
721			$this->DebugMessage('imageinterlace($this->gdimg_output, '. (int) $this->config_output_interlace .')', __FILE__, __LINE__);
722			imageinterlace($this->gdimg_output, (int) $this->config_output_interlace);
723			switch ($this->thumbnailFormat) {
724				case 'gif':
725				case 'jpeg':
726				case 'png':
727				case 'webp':
728				case 'avif':
729					$ImageOutFunction = 'image'.$this->thumbnailFormat;
730					if (!function_exists($ImageOutFunction)) {
731						$this->DebugMessage($ImageOutFunction.' is not available', __FILE__, __LINE__);
732						return false;
733					}
734					header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
735					if ($this->thumbnailFormat == 'gif') {
736						@$ImageOutFunction($this->gdimg_output);
737					} else {
738						@$ImageOutFunction($this->gdimg_output, null, $this->thumbnailQuality);
739					}
740					break;
741
742				case 'bmp':
743					if (function_exists('imagebmp')) {
744						header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
745						imagebmp($this->gdimg_output);
746						break;
747					}
748					if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
749						$this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
750						return false;
751					}
752					$phpthumb_bmp = new phpthumb_bmp();
753					if (is_object($phpthumb_bmp)) {
754						$bmp_data = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
755						unset($phpthumb_bmp);
756						if (!$bmp_data) {
757							$this->DebugMessage('$phpthumb_bmp->GD2BMPstring() failed', __FILE__, __LINE__);
758							return false;
759						}
760						header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
761						echo $bmp_data;
762					} else {
763						$this->DebugMessage('new phpthumb_bmp() failed', __FILE__, __LINE__);
764						return false;
765					}
766					break;
767
768				case 'ico':
769					if (!@include_once __DIR__ .'/phpthumb.ico.php' ) {
770						$this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
771						return false;
772					}
773					$phpthumb_ico = new phpthumb_ico();
774					if (is_object($phpthumb_ico)) {
775						$arrayOfOutputImages = array($this->gdimg_output);
776						$ico_data = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
777						unset($phpthumb_ico);
778						if (!$ico_data) {
779							$this->DebugMessage('$phpthumb_ico->GD2ICOstring() failed', __FILE__, __LINE__);
780							return false;
781						}
782						header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
783						echo $ico_data;
784					} else {
785						$this->DebugMessage('new phpthumb_ico() failed', __FILE__, __LINE__);
786						return false;
787					}
788					break;
789
790				default:
791					$this->DebugMessage('OutputThumbnail failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
792					return false;
793					break;
794			}
795
796		}
797		return true;
798	}
799
800
801	// public:
802	public function CleanUpCacheDirectory() {
803		$this->DebugMessage('CleanUpCacheDirectory() set to purge ('.(null === $this->config_cache_maxage ? 'NULL' : number_format($this->config_cache_maxage / 86400, 1)).' days; '.(null === $this->config_cache_maxsize ? 'NULL' : number_format($this->config_cache_maxsize / 1048576, 2)).' MB; '.(null === $this->config_cache_maxfiles ? 'NULL' : number_format($this->config_cache_maxfiles)).' files)', __FILE__, __LINE__);
804
805		if (!is_writable($this->config_cache_directory)) {
806			$this->DebugMessage('CleanUpCacheDirectory() skipped because "'.$this->config_cache_directory.'" is not writable', __FILE__, __LINE__);
807			return true;
808		}
809
810		// cache status of cache directory for 1 hour to avoid hammering the filesystem functions
811		$phpThumbCacheStats_filename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheStats.txt';
812		if (file_exists($phpThumbCacheStats_filename) && is_readable($phpThumbCacheStats_filename) && (filemtime($phpThumbCacheStats_filename) >= (time() - 3600))) {
813			$this->DebugMessage('CleanUpCacheDirectory() skipped because "'.$phpThumbCacheStats_filename.'" is recently modified', __FILE__, __LINE__);
814			return true;
815		}
816		if (!@touch($phpThumbCacheStats_filename)) {
817			$this->DebugMessage('touch('.$phpThumbCacheStats_filename.') failed', __FILE__, __LINE__);
818		}
819
820		$DeletedKeys = array();
821		$AllFilesInCacheDirectory = array();
822		if (($this->config_cache_maxage > 0) || ($this->config_cache_maxsize > 0) || ($this->config_cache_maxfiles > 0)) {
823			$CacheDirOldFilesAge  = array();
824			$CacheDirOldFilesSize = array();
825			$AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory);
826			foreach ($AllFilesInCacheDirectory as $fullfilename) {
827				if (preg_match('#'.preg_quote($this->config_cache_prefix).'#i', $fullfilename) && file_exists($fullfilename)) {
828					$CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename);
829					if ($CacheDirOldFilesAge[$fullfilename] == 0) {
830						$CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename);
831					}
832					$CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename);
833				}
834			}
835			if (empty($CacheDirOldFilesSize)) {
836				$this->DebugMessage('CleanUpCacheDirectory() skipped because $CacheDirOldFilesSize is empty (phpthumb_functions::GetAllFilesInSubfolders('.$this->config_cache_directory.') found no files)', __FILE__, __LINE__);
837				return true;
838			}
839			$DeletedKeys['zerobyte'] = array();
840			foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) {
841				// purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files)
842				$cutofftime = time() - 3600;
843				if (($filesize == 0) && ($CacheDirOldFilesAge[$fullfilename] < $cutofftime)) {
844					$this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
845					if (@unlink($fullfilename)) {
846						$DeletedKeys['zerobyte'][] = $fullfilename;
847						unset($CacheDirOldFilesSize[$fullfilename]);
848						unset($CacheDirOldFilesAge[$fullfilename]);
849					}
850				}
851			}
852			$this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['zerobyte']).' zero-byte files', __FILE__, __LINE__);
853			asort($CacheDirOldFilesAge);
854
855			if ($this->config_cache_maxfiles > 0) {
856				$TotalCachedFiles = count($CacheDirOldFilesAge);
857				$DeletedKeys['maxfiles'] = array();
858				foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
859					if ($TotalCachedFiles > $this->config_cache_maxfiles) {
860						$this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
861						if (@unlink($fullfilename)) {
862							$TotalCachedFiles--;
863							$DeletedKeys['maxfiles'][] = $fullfilename;
864						}
865					} else {
866						// there are few enough files to keep the rest
867						break;
868					}
869				}
870				$this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxfiles']).' files based on (config_cache_maxfiles='.$this->config_cache_maxfiles.')', __FILE__, __LINE__);
871				foreach ($DeletedKeys['maxfiles'] as $fullfilename) {
872					unset($CacheDirOldFilesAge[$fullfilename]);
873					unset($CacheDirOldFilesSize[$fullfilename]);
874				}
875			}
876
877			if ($this->config_cache_maxage > 0) {
878				$mindate = time() - $this->config_cache_maxage;
879				$DeletedKeys['maxage'] = array();
880				foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
881					if ($filedate > 0) {
882						if ($filedate < $mindate) {
883							$this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
884							if (@unlink($fullfilename)) {
885								$DeletedKeys['maxage'][] = $fullfilename;
886							}
887						} else {
888							// the rest of the files are new enough to keep
889							break;
890						}
891					}
892				}
893				$this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxage']).' files based on (config_cache_maxage='.$this->config_cache_maxage.')', __FILE__, __LINE__);
894				foreach ($DeletedKeys['maxage'] as $fullfilename) {
895					unset($CacheDirOldFilesAge[$fullfilename]);
896					unset($CacheDirOldFilesSize[$fullfilename]);
897				}
898			}
899
900			if ($this->config_cache_maxsize > 0) {
901				$TotalCachedFileSize = array_sum($CacheDirOldFilesSize);
902				$DeletedKeys['maxsize'] = array();
903				foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
904					if ($TotalCachedFileSize > $this->config_cache_maxsize) {
905						$this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
906						if (@unlink($fullfilename)) {
907							$TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename];
908							$DeletedKeys['maxsize'][] = $fullfilename;
909						}
910					} else {
911						// the total filesizes are small enough to keep the rest of the files
912						break;
913					}
914				}
915				$this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxsize']).' files based on (config_cache_maxsize='.$this->config_cache_maxsize.')', __FILE__, __LINE__);
916				foreach ($DeletedKeys['maxsize'] as $fullfilename) {
917					unset($CacheDirOldFilesAge[$fullfilename]);
918					unset($CacheDirOldFilesSize[$fullfilename]);
919				}
920			}
921
922		} else {
923			$this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__);
924		}
925		$totalpurged = 0;
926		foreach ($DeletedKeys as $key => $value) {
927			$totalpurged += count($value);
928		}
929		$this->DebugMessage('CleanUpCacheDirectory() purged '.$totalpurged.' files (from '.count($AllFilesInCacheDirectory).') based on config settings', __FILE__, __LINE__);
930		if ($totalpurged > 0) {
931			$empty_dirs = array();
932			foreach ($AllFilesInCacheDirectory as $fullfilename) {
933				if (is_dir($fullfilename)) {
934					$empty_dirs[$this->realPathSafe($fullfilename)] = 1;
935				} else {
936					unset($empty_dirs[$this->realPathSafe(dirname($fullfilename))]);
937				}
938			}
939			krsort($empty_dirs);
940			$totalpurgeddirs = 0;
941			foreach ($empty_dirs as $empty_dir => $dummy) {
942				if ($empty_dir == $this->config_cache_directory) {
943					// shouldn't happen, but just in case, don't let it delete actual cache directory
944					continue;
945				} elseif (@rmdir($empty_dir)) {
946					$totalpurgeddirs++;
947				} else {
948					$this->DebugMessage('failed to rmdir('.$empty_dir.')', __FILE__, __LINE__);
949				}
950			}
951			$this->DebugMessage('purged '.$totalpurgeddirs.' empty directories', __FILE__, __LINE__);
952		}
953		return true;
954	}
955
956	//////////////////////////////////////////////////////////////////////
957
958	// private: re-initializator (call between rendering multiple images with one object)
959	public function resetObject() {
960		$class_vars = get_class_vars(get_class($this));
961		foreach ($class_vars as $key => $value) {
962			// do not clobber debug or config info
963			if (!preg_match('#^(config_|debug|fatalerror)#i', $key)) {
964				$this->$key = $value;
965			}
966		}
967		$this->phpThumb(); // re-initialize some class variables
968		return true;
969	}
970
971	//////////////////////////////////////////////////////////////////////
972
973	public function ResolveSource() {
974		if (is_resource($this->gdimg_source) || (is_object($this->gdimg_source) && $this->gdimg_source instanceOf \GdImage)) {
975			$this->DebugMessage('ResolveSource() exiting because is_resource($this->gdimg_source)', __FILE__, __LINE__);
976			return true;
977		}
978		if ($this->rawImageData) {
979			$this->sourceFilename = null;
980			$this->DebugMessage('ResolveSource() exiting because $this->rawImageData is set ('.number_format(strlen($this->rawImageData)).' bytes)', __FILE__, __LINE__);
981			return true;
982		}
983		if ($this->sourceFilename) {
984			$this->sourceFilename = $this->ResolveFilenameToAbsolute($this->sourceFilename);
985			$this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
986		} elseif ($this->src) {
987			$this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
988			$this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'" from $this->src ('.$this->src.')', __FILE__, __LINE__);
989		} else {
990			return $this->ErrorImage('$this->sourceFilename and $this->src are both empty');
991		}
992		if ($this->iswindows && ((substr($this->sourceFilename, 0, 2) == '//') || (substr($this->sourceFilename, 0, 2) == '\\\\'))) {
993			// Windows \\share\filename.ext
994		} elseif (preg_match('#^[a-z0-9]+://#i', $this->sourceFilename, $protocol_matches)) {
995			if (preg_match('#^(f|ht)tps?\://#i', $this->sourceFilename)) {
996				// URL
997				if ($this->config_http_user_agent) {
998					ini_set('user_agent', $this->config_http_user_agent);
999				}
1000			} else {
1001				return $this->ErrorImage('only FTP and HTTP/HTTPS protocols are allowed, "'.$protocol_matches[1].'" is not');
1002		}
1003		} elseif (!@file_exists($this->sourceFilename)) {
1004			return $this->ErrorImage('"'.$this->sourceFilename.'" does not exist');
1005		} elseif (!@is_file($this->sourceFilename)) {
1006			return $this->ErrorImage('"'.$this->sourceFilename.'" is not a file');
1007		}
1008		return true;
1009	}
1010
1011
1012	public function setOutputFormat() {
1013		static $alreadyCalled = false;
1014		if ($this->thumbnailFormat && $alreadyCalled) {
1015			return true;
1016		}
1017		$alreadyCalled = true;
1018
1019		$AvailableImageOutputFormats = array();
1020		$AvailableImageOutputFormats[] = 'text';
1021		if (@is_readable( __DIR__ .'/phpthumb.ico.php')) {
1022			$AvailableImageOutputFormats[] = 'ico';
1023		}
1024		if (@is_readable( __DIR__ .'/phpthumb.bmp.php')) {
1025			$AvailableImageOutputFormats[] = 'bmp';
1026		}
1027
1028		$this->thumbnailFormat = 'ico';
1029
1030		// Set default output format based on what image types are available
1031		if (function_exists('imagetypes')) {
1032			$imagetypes = imagetypes();
1033			if ($imagetypes & IMG_WBMP) {
1034				$this->thumbnailFormat         = 'wbmp';
1035				$AvailableImageOutputFormats[] = 'wbmp';
1036			}
1037			if ($imagetypes & IMG_GIF) {
1038				$this->thumbnailFormat         = 'gif';
1039				$AvailableImageOutputFormats[] = 'gif';
1040			}
1041			if ($imagetypes & IMG_AVIF) {
1042				$this->thumbnailFormat         = 'avif';
1043				$AvailableImageOutputFormats[] = 'avif';
1044			}
1045			if ($imagetypes & IMG_WEBP) {
1046				$this->thumbnailFormat         = 'webp';
1047				$AvailableImageOutputFormats[] = 'webp';
1048			}
1049			if ($imagetypes & IMG_PNG) {
1050				$this->thumbnailFormat         = 'png';
1051				$AvailableImageOutputFormats[] = 'png';
1052			}
1053			if ($imagetypes & IMG_JPG) {
1054				$this->thumbnailFormat         = 'jpeg';
1055				$AvailableImageOutputFormats[] = 'jpeg';
1056			}
1057		} else {
1058			$this->DebugMessage('imagetypes() does not exist - GD support might not be enabled?',  __FILE__, __LINE__);
1059		}
1060		if ($this->ImageMagickVersion()) {
1061			$IMformats = array('jpeg', 'png', 'gif', 'bmp', 'ico', 'wbmp', 'webp', 'avif');
1062			$this->DebugMessage('Addding ImageMagick formats to $AvailableImageOutputFormats ('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
1063			foreach ($IMformats as $key => $format) {
1064				$AvailableImageOutputFormats[] = $format;
1065			}
1066		}
1067		$AvailableImageOutputFormats = array_unique($AvailableImageOutputFormats);
1068		$this->DebugMessage('$AvailableImageOutputFormats = array('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
1069
1070		$this->f = (!empty($this->f) ? $this->f : '');
1071		$this->f = preg_replace('#[^a-z]#', '', strtolower($this->f));
1072		if (strtolower($this->config_output_format) == 'jpg') {
1073			$this->config_output_format = 'jpeg';
1074		}
1075		if (strtolower($this->f) == 'jpg') {
1076			$this->f = 'jpeg';
1077		}
1078		if (phpthumb_functions::CaseInsensitiveInArray($this->config_output_format, $AvailableImageOutputFormats)) {
1079			// set output format to config default if that format is available
1080			$this->DebugMessage('$this->thumbnailFormat set to $this->config_output_format "'.strtolower($this->config_output_format).'"', __FILE__, __LINE__);
1081			$this->thumbnailFormat = strtolower($this->config_output_format);
1082		} elseif ($this->config_output_format) {
1083			$this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->config_output_format ('.strtolower($this->config_output_format).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
1084		}
1085		if ($this->f && phpthumb_functions::CaseInsensitiveInArray($this->f, $AvailableImageOutputFormats) ) {
1086			// override output format if $this->f is set and that format is available
1087			$this->DebugMessage('$this->thumbnailFormat set to $this->f "'.strtolower($this->f).'"', __FILE__, __LINE__);
1088			$this->thumbnailFormat = strtolower($this->f);
1089		} elseif ($this->f) {
1090			$this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->f ('.strtolower($this->f).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
1091		}
1092
1093		// for JPEG images, quality 1 (worst) to 99 (best)
1094		// quality < 25 is nasty, with not much size savings - not recommended
1095		// problems with 100 - invalid JPEG?
1096		$this->thumbnailQuality = max(1, min(99, ($this->q ? (int) $this->q : 75)));
1097		$this->DebugMessage('$this->thumbnailQuality set to "'.$this->thumbnailQuality.'"', __FILE__, __LINE__);
1098
1099		return true;
1100	}
1101
1102
1103	public function setCacheDirectory() {
1104		// resolve cache directory to absolute pathname
1105		$this->DebugMessage('setCacheDirectory() starting with config_cache_directory = "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
1106		if ($this->config_cache_directory && ($this->config_cache_directory[0] == '.')) {
1107			if (preg_match('#^(f|ht)tps?\://#i', $this->src)) {
1108				if (!$this->config_cache_disable_warning) {
1109					$this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') cannot be used for remote images. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php');
1110				}
1111			} elseif ($this->src) {
1112				// resolve relative cache directory to source image
1113				$this->config_cache_directory = dirname($this->ResolveFilenameToAbsolute($this->src)).DIRECTORY_SEPARATOR.$this->config_cache_directory;
1114			} else {
1115				// $this->new is probably set
1116			}
1117		}
1118		if (substr($this->config_cache_directory, -1) == '/') {
1119			$this->config_cache_directory = substr($this->config_cache_directory, 0, -1);
1120		}
1121		if ($this->iswindows) {
1122			$this->config_cache_directory = str_replace('/', DIRECTORY_SEPARATOR, $this->config_cache_directory);
1123		}
1124		if ($this->config_cache_directory) {
1125			$real_cache_path = $this->realPathSafe($this->config_cache_directory);
1126			if (!$real_cache_path) {
1127				$this->DebugMessage('$this->realPathSafe($this->config_cache_directory) failed for "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
1128				if (!is_dir($this->config_cache_directory)) {
1129					$this->DebugMessage('!is_dir('.$this->config_cache_directory.')', __FILE__, __LINE__);
1130				}
1131			}
1132			if ($real_cache_path) {
1133				$this->DebugMessage('setting config_cache_directory to $this->realPathSafe('.$this->config_cache_directory.') = "'.$real_cache_path.'"', __FILE__, __LINE__);
1134				$this->config_cache_directory = $real_cache_path;
1135			}
1136		}
1137		if (!is_dir($this->config_cache_directory)) {
1138			if (!$this->config_cache_disable_warning) {
1139				$this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') does not exist. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php');
1140			}
1141			$this->DebugMessage('$this->config_cache_directory ('.$this->config_cache_directory.') is not a directory', __FILE__, __LINE__);
1142			$this->config_cache_directory = null;
1143		} elseif (!@is_writable($this->config_cache_directory)) {
1144			$this->DebugMessage('$this->config_cache_directory is not writable ('.$this->config_cache_directory.')', __FILE__, __LINE__);
1145		}
1146
1147		$this->InitializeTempDirSetting();
1148		if (!@is_dir($this->config_temp_directory) && !@is_writable($this->config_temp_directory) && @is_dir($this->config_cache_directory) && @is_writable($this->config_cache_directory)) {
1149			$this->DebugMessage('setting $this->config_temp_directory = $this->config_cache_directory ('.$this->config_cache_directory.')', __FILE__, __LINE__);
1150			$this->config_temp_directory = $this->config_cache_directory;
1151		}
1152		return true;
1153	}
1154
1155	/* Takes the array of path segments up to now, and the next segment (maybe a modifier: empty, . or ..)
1156	   Applies it, adding or removing from $segments as a result. Returns nothing. */
1157	// http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
1158	public function applyPathSegment(&$segments, $segment) {
1159		if ($segment == '.') {
1160			return; // always remove
1161		}
1162		if ($segment == '') {
1163			$test = array_pop($segments);
1164			if (null === $test) {
1165				$segments[] = $segment; // keep the first empty block
1166			} elseif ($test == '') {
1167				$test = array_pop($segments);
1168				if (null === $test) {
1169					$segments[] = $test;
1170					$segments[] = $segment; // keep the second one too
1171				} else { // put both back and ignore segment
1172					$segments[] = $test;
1173					$segments[] = $test;
1174				}
1175			} else {
1176				$segments[] = $test; // ignore empty blocks
1177			}
1178		} else {
1179			if ($segment == '..') {
1180				$test = array_pop($segments);
1181				if (null === $test) {
1182					$segments[] = $segment;
1183				} elseif ($test == '..') {
1184					$segments[] = $test;
1185					$segments[] = $segment;
1186				} else {
1187					if ($test == '') {
1188						$segments[] = $test;
1189					} // else nothing, remove both
1190				}
1191			} else {
1192				$segments[] = $segment;
1193			}
1194		}
1195	}
1196
1197	/* Takes array of path components, normalizes it: removes empty slots and '.', collapses '..' and folder names.  Returns array. */
1198	// http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
1199	public function normalizePath($segments) {
1200		$parts = array();
1201		foreach ($segments as $segment) {
1202			$this->applyPathSegment($parts, $segment);
1203		}
1204		return $parts;
1205	}
1206
1207	/* True if the provided path points (without resolving symbolic links) into one of the allowed directories. */
1208	// http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
1209	public function matchPath($path, $allowed_dirs) {
1210		if (!empty($allowed_dirs)) {
1211			foreach ($allowed_dirs as $one_dir) {
1212				if (preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($one_dir))).'#', $path)) {
1213					return true;
1214				}
1215			}
1216		}
1217		return false;
1218	}
1219
1220	/* True if the provided path points inside one of open_basedirs (or if open_basedirs are disabled) */
1221	// http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
1222	public function isInOpenBasedir($path) {
1223		static $open_basedirs = null;
1224		if (null === $open_basedirs) {
1225			$ini_text = ini_get('open_basedir');
1226			$this->DebugMessage('open_basedir: "'.$ini_text.'"', __FILE__, __LINE__);
1227			$open_basedirs = array();
1228			if (strlen($ini_text) > 0) {
1229				foreach (preg_split('#[;:]#', $ini_text) as $key => $value) {
1230					$open_basedirs[$key] = $this->realPathSafe($value);
1231				}
1232			}
1233		}
1234		return (empty($open_basedirs) || $this->matchPath($path, $open_basedirs));
1235	}
1236
1237	/* Resolves all symlinks in $path, checking that each continuous part ends in an allowed zone. Returns null, if any component leads outside of allowed zone. */
1238	// http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
1239	public function resolvePath($path, $allowed_dirs) {
1240		$this->DebugMessage('resolvePath: '.$path.' (allowed_dirs: '.print_r($allowed_dirs, true).')', __FILE__, __LINE__);
1241
1242		// add base path to the top of the list
1243		if (!$this->config_allow_src_above_docroot) {
1244			array_unshift($allowed_dirs, $this->realPathSafe($this->config_document_root));
1245		} else {
1246			if (!$this->config_allow_src_above_phpthumb) {
1247				array_unshift($allowed_dirs, $this->realPathSafe( __DIR__ ));
1248			} else {
1249				// no checks are needed, offload the work to realpath and forget about it
1250				$this->DebugMessage('resolvePath: checks disabled, returning '.$this->realPathSafe($path), __FILE__, __LINE__);
1251				return $this->realPathSafe($path);
1252			}
1253		}
1254		if ($path == '') {
1255			return null; // save us trouble
1256		}
1257
1258		do {
1259			$this->DebugMessage('resolvePath: iteration, path='.$path.', base path = '.$allowed_dirs[0], __FILE__, __LINE__);
1260
1261			$parts = array();
1262			// do not use "cleaner" foreach version of this loop as later code relies on both $segments and $i
1263			// http://support.silisoftware.com/phpBB3/viewtopic.php?t=964
1264			$segments = explode(DIRECTORY_SEPARATOR, $path);
1265			for ($i = 0, $iMax = count($segments); $i < $iMax; $i++) {
1266				$this->applyPathSegment($parts, $segments[$i]);
1267				$thispart = implode(DIRECTORY_SEPARATOR, $parts);
1268				if ($this->isInOpenBasedir($thispart)) {
1269					if (is_link($thispart)) {
1270						break;
1271					}
1272				}
1273			}
1274
1275			$this->DebugMessage('resolvePath: stop at component '.$i, __FILE__, __LINE__);
1276			// test the part up to here
1277			$path = implode(DIRECTORY_SEPARATOR, $parts);
1278			$this->DebugMessage('resolvePath: stop at path='.$path, __FILE__, __LINE__);
1279			if (!$this->matchPath($path, $allowed_dirs)) {
1280				$this->DebugMessage('resolvePath: no match, returning null', __FILE__, __LINE__);
1281				return null;
1282			}
1283			if ($i >= count($segments)) { // reached end
1284				$this->DebugMessage('resolvePath: path parsed, over', __FILE__, __LINE__);
1285				break;
1286			}
1287			// else it's symlink, rewrite path
1288			$path = readlink($path);
1289			$this->DebugMessage('resolvePath: symlink matched, target='.$path, __FILE__, __LINE__);
1290
1291			/*
1292			Replace base path with symlink target.
1293			Assuming:
1294			  /www/img/external -> /external
1295			This is allowed:
1296			  GET /www/img/external/../external/test/pic.jpg
1297			This isn't:
1298			  GET /www/img/external/../www/img/pic.jpg
1299			So there's only one base path which is the last symlink target, but any number of stable whitelisted paths.
1300			*/
1301			if ($this->config_auto_allow_symlinks) {
1302				$allowed_dirs[0] = $path;
1303			}
1304			$path = $path.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, array_slice($segments,$i + 1));
1305		} while (true);
1306		return $path;
1307	}
1308
1309
1310	public function realPathSafe($filename) {
1311		// http://php.net/manual/en/function.realpath.php -- "Note: The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE"
1312		// realPathSafe() provides a reasonable facsimile of realpath() but does not resolve symbolic links, nor does it check that the file/path actually exists
1313		if (!$this->config_disable_realpath) {
1314			return realpath($filename);
1315		}
1316
1317		// http://stackoverflow.com/questions/21421569
1318		$newfilename = preg_replace('#[\\/]+#', DIRECTORY_SEPARATOR, $filename);
1319
1320		if (phpthumb_functions::is_windows()) {
1321			$isAlreadyAbsoluteFilename = preg_match('#^[A-Z]\\:#i', $newfilename);  // C:\path\filename.ext
1322		} else {
1323			$isAlreadyAbsoluteFilename = ($newfilename[0] == DIRECTORY_SEPARATOR);  // /path/filename.ext
1324		}
1325		if (!$isAlreadyAbsoluteFilename) {
1326			// not already an absolute filename, prepend current directory
1327			$newfilename =  __DIR__ .DIRECTORY_SEPARATOR.$newfilename;
1328		}
1329		do {
1330			$beforeloop = $newfilename;
1331
1332			// Replace all sequences of more than one / with a single one [[ If you're working on a system that treats // at the start of a path as special, make sure you replace multiple / characters at the start with two of them. This is the only place where POSIX allows (but does not mandate) special handling for multiples, in all other cases, multiple / characters are equivalent to a single one.]]
1333			$newfilename = preg_replace('#'.preg_quote(DIRECTORY_SEPARATOR).'+#', DIRECTORY_SEPARATOR, $newfilename);
1334
1335			// Replace all occurrences of /./ with /
1336			$newfilename = preg_replace('#'.preg_quote(DIRECTORY_SEPARATOR).'\\.'.preg_quote(DIRECTORY_SEPARATOR).'#', DIRECTORY_SEPARATOR, $newfilename);
1337
1338			// Remove ./ if at the start
1339			$newfilename = preg_replace('#^\\.'.preg_quote(DIRECTORY_SEPARATOR).'#', '', $newfilename);
1340
1341			// Remove /. if at the end
1342			$newfilename = preg_replace('#'.preg_quote(DIRECTORY_SEPARATOR).'\\.$#', '', $newfilename);
1343
1344			// Replace /anything/../ with /
1345			$newfilename = preg_replace('#'.preg_quote(DIRECTORY_SEPARATOR).'[^'.preg_quote(DIRECTORY_SEPARATOR).']+'.preg_quote(DIRECTORY_SEPARATOR).'\\.\\.'.preg_quote(DIRECTORY_SEPARATOR).'#', DIRECTORY_SEPARATOR, $newfilename);
1346
1347			// Remove /anything/.. if at the end
1348			$newfilename = preg_replace('#'.preg_quote(DIRECTORY_SEPARATOR).'[^'.preg_quote(DIRECTORY_SEPARATOR).']+'.preg_quote(DIRECTORY_SEPARATOR).'\\.\\.$#', '', $newfilename);
1349
1350		} while ($newfilename != $beforeloop);
1351		return $newfilename;
1352	}
1353
1354
1355	public function ResolveFilenameToAbsolute($filename) {
1356		if (empty($filename)) {
1357			return false;
1358		}
1359
1360		if (preg_match('#^[a-z0-9]+\\:/{1,2}#i', $filename)) {
1361			// eg: http://host/path/file.jpg (HTTP URL)
1362			// eg: ftp://host/path/file.jpg  (FTP URL)
1363			// eg: data1:/path/file.jpg      (Netware path)
1364
1365			//$AbsoluteFilename = $filename;
1366			return $filename;
1367
1368		} elseif ($this->iswindows && isset($filename[1]) && ($filename[1] == ':')) {
1369
1370			// absolute pathname (Windows)
1371			$AbsoluteFilename = $filename;
1372
1373		} elseif ($this->iswindows && ((substr($filename, 0, 2) == '//') || (substr($filename, 0, 2) == '\\\\'))) {
1374
1375			// absolute pathname (Windows)
1376			$AbsoluteFilename = $filename;
1377
1378		} elseif ($filename[0] == '/') {
1379
1380			if (@is_readable($filename) && !@is_readable($this->config_document_root.$filename)) {
1381
1382				// absolute filename (*nix)
1383				$AbsoluteFilename = $filename;
1384
1385			} elseif (isset($filename[1]) && ($filename[1] == '~')) {
1386
1387				// /~user/path
1388				if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray($filename)) {
1389					$AbsoluteFilename = $ApacheLookupURIarray['filename'];
1390				} else {
1391					$AbsoluteFilename = $this->realPathSafe($filename);
1392					if (@is_readable($AbsoluteFilename)) {
1393						$this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.$filename.'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with $this->realPathSafe($filename)', __FILE__, __LINE__);
1394					} elseif (is_dir(dirname($AbsoluteFilename))) {
1395						$this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname($filename).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with $this->realPathSafe(.)', __FILE__, __LINE__);
1396					} else {
1397						return $this->ErrorImage('phpthumb_functions::ApacheLookupURIarray() failed for "'.$filename.'". This has been known to fail on Apache2 - try using the absolute filename for the source image (ex: "/home/user/httpdocs/image.jpg" instead of "/~user/image.jpg")');
1398					}
1399				}
1400
1401			} else {
1402
1403				// relative filename (any OS)
1404				if (preg_match('#^'.preg_quote($this->config_document_root).'#', $filename)) {
1405					$AbsoluteFilename = $filename;
1406					$this->DebugMessage('ResolveFilenameToAbsolute() NOT prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__);
1407				} else {
1408					$AbsoluteFilename = $this->config_document_root.$filename;
1409					$this->DebugMessage('ResolveFilenameToAbsolute() prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__);
1410				}
1411
1412			}
1413
1414		} else {
1415
1416			// relative to current directory (any OS)
1417			$AbsoluteFilename =  __DIR__ .DIRECTORY_SEPARATOR.preg_replace('#[/\\\\]#', DIRECTORY_SEPARATOR, $filename);
1418
1419			if (substr(dirname(@$_SERVER['PHP_SELF']), 0, 2) == '/~') {
1420				if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) {
1421					$AbsoluteFilename = $ApacheLookupURIarray['filename'].DIRECTORY_SEPARATOR.$filename;
1422				} else {
1423					$AbsoluteFilename = $this->realPathSafe('.').DIRECTORY_SEPARATOR.$filename;
1424					if (@is_readable($AbsoluteFilename)) {
1425						$this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with $this->realPathSafe(.)/$filename', __FILE__, __LINE__);
1426					} elseif (is_dir(dirname($AbsoluteFilename))) {
1427						$this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with $this->realPathSafe(.)', __FILE__, __LINE__);
1428					} else {
1429						return $this->ErrorImage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'". This has been known to fail on Apache2 - try using the absolute filename for the source image');
1430					}
1431				}
1432			}
1433
1434		}
1435		/*
1436		// removed 2014-May-30: http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
1437		if (is_link($AbsoluteFilename)) {
1438			$this->DebugMessage('is_link()==true, changing "'.$AbsoluteFilename.'" to "'.readlink($AbsoluteFilename).'"', __FILE__, __LINE__);
1439			$AbsoluteFilename = readlink($AbsoluteFilename);
1440		}
1441		if ($this->realPathSafe($AbsoluteFilename)) {
1442			$AbsoluteFilename = $this->realPathSafe($AbsoluteFilename);
1443		}
1444		*/
1445		if ($this->iswindows) {
1446			$AbsoluteFilename = preg_replace('#^'.preg_quote($this->realPathSafe($this->config_document_root)).'#i', str_replace('\\', '\\\\', $this->realPathSafe($this->config_document_root)), $AbsoluteFilename);
1447			$AbsoluteFilename = str_replace(DIRECTORY_SEPARATOR, '/', $AbsoluteFilename);
1448		}
1449		$resolvedAbsoluteFilename = $this->resolvePath($AbsoluteFilename, $this->config_additional_allowed_dirs);
1450		if (!$this->config_allow_src_above_docroot && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($this->config_document_root))).'#', $resolvedAbsoluteFilename)) {
1451			$this->DebugMessage('!$this->config_allow_src_above_docroot therefore setting "'.$AbsoluteFilename.'" (outside "'.$this->realPathSafe($this->config_document_root).'") to null', __FILE__, __LINE__);
1452			return false;
1453		}
1454		if (!$this->config_allow_src_above_phpthumb && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/',  __DIR__ )).'#', $resolvedAbsoluteFilename)) {
1455			$this->DebugMessage('!$this->config_allow_src_above_phpthumb therefore setting "'.$AbsoluteFilename.'" (outside "'. __DIR__ .'") to null', __FILE__, __LINE__);
1456			return false;
1457		}
1458		return $resolvedAbsoluteFilename;
1459	}
1460
1461
1462	public function file_exists_ignoreopenbasedir($filename, $cached=true) {
1463		static $open_basedirs = null;
1464		static $file_exists_cache = array();
1465		if (!$cached || !isset($file_exists_cache[$filename])) {
1466			if (is_null($open_basedirs)) {
1467				$open_basedirs = preg_split('#[;:]#', ini_get('open_basedir'));
1468			}
1469			if (is_null($filename)) { // shouldn't happen, but https://github.com/JamesHeinrich/phpThumb/issues/188
1470				$file_exists_cache[$filename] = false;
1471			} elseif (empty($open_basedirs) || in_array(dirname($filename), $open_basedirs)) {
1472				$file_exists_cache[$filename] = file_exists($filename);
1473			} elseif ($this->iswindows) {
1474				$ls_filename = trim(phpthumb_functions::SafeExec('dir /b '.phpthumb_functions::escapeshellarg_replacement($filename)));
1475				$file_exists_cache[$filename] = ($ls_filename == basename($filename));  // command dir /b return only filename without path
1476			} else {
1477				$ls_filename = trim(phpthumb_functions::SafeExec('ls '.phpthumb_functions::escapeshellarg_replacement($filename)));
1478				$file_exists_cache[$filename] = ($ls_filename == $filename);
1479			}
1480		}
1481		return $file_exists_cache[$filename];
1482	}
1483
1484
1485	public function ImageMagickWhichConvert() {
1486		static $WhichConvert = null;
1487		if (null === $WhichConvert) {
1488			if ($this->iswindows) {
1489				$WhichConvert = false;
1490			} else {
1491				$IMwhichConvertCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMwhichConvert.txt';
1492				if (($cachedwhichconvertstring = @file_get_contents($IMwhichConvertCacheFilename)) !== false) {
1493					$WhichConvert = $cachedwhichconvertstring;
1494				} else {
1495					$WhichConvert = trim(phpthumb_functions::SafeExec('which convert'));
1496					@file_put_contents($IMwhichConvertCacheFilename, $WhichConvert);
1497					@chmod($IMwhichConvertCacheFilename, $this->getParameter('config_file_create_mask'));
1498				}
1499			}
1500		}
1501		return $WhichConvert;
1502	}
1503
1504
1505	public function ImageMagickCommandlineBase() {
1506		static $commandline = null;
1507		if (null === $commandline) {
1508			if ($this->issafemode) {
1509				$commandline = '';
1510				return $commandline;
1511			}
1512
1513			$IMcommandlineBaseCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMcommandlineBase.txt';
1514			if (($commandline = @file_get_contents($IMcommandlineBaseCacheFilename)) !== false) {
1515				return $commandline;
1516			}
1517
1518			$commandline = (null !== $this->config_imagemagick_path ? $this->config_imagemagick_path : '');
1519
1520			if ($this->config_imagemagick_path && ($this->config_imagemagick_path != $this->realPathSafe($this->config_imagemagick_path))) {
1521				if (@is_executable($this->realPathSafe($this->config_imagemagick_path))) {
1522					$this->DebugMessage('Changing $this->config_imagemagick_path ('.$this->config_imagemagick_path.') to $this->realPathSafe($this->config_imagemagick_path) ('.$this->realPathSafe($this->config_imagemagick_path).')', __FILE__, __LINE__);
1523					$this->config_imagemagick_path = $this->realPathSafe($this->config_imagemagick_path);
1524				} else {
1525					$this->DebugMessage('Leaving $this->config_imagemagick_path as ('.$this->config_imagemagick_path.') because !is_execuatable($this->realPathSafe($this->config_imagemagick_path)) ('.$this->realPathSafe($this->config_imagemagick_path).')', __FILE__, __LINE__);
1526				}
1527			}
1528			if (!empty($this->config_imagemagick_path)) {
1529				$this->DebugMessage('                  file_exists('.$this->config_imagemagick_path.') = '. (int) (@file_exists($this->config_imagemagick_path)), __FILE__, __LINE__);
1530				$this->DebugMessage('file_exists_ignoreopenbasedir('.$this->config_imagemagick_path.') = '. (int) $this->file_exists_ignoreopenbasedir($this->config_imagemagick_path), __FILE__, __LINE__);
1531				$this->DebugMessage('                      is_file('.$this->config_imagemagick_path.') = '. (int) (@is_file($this->config_imagemagick_path)), __FILE__, __LINE__);
1532				$this->DebugMessage('                is_executable('.$this->config_imagemagick_path.') = '. (int) (@is_executable($this->config_imagemagick_path)), __FILE__, __LINE__);
1533			}
1534
1535			if ($this->file_exists_ignoreopenbasedir($this->config_imagemagick_path)) {
1536
1537				$this->DebugMessage('using ImageMagick path from $this->config_imagemagick_path ('.$this->config_imagemagick_path.')', __FILE__, __LINE__);
1538				if ($this->iswindows) {
1539					$commandline  = '';
1540					$commandline .= substr($this->config_imagemagick_path, 0, 2);
1541					$commandline .= ' && cd '.phpthumb_functions::escapeshellarg_replacement(str_replace('/', DIRECTORY_SEPARATOR, substr(dirname($this->config_imagemagick_path), 2)));
1542					$commandline .= ' && '.phpthumb_functions::escapeshellarg_replacement(basename($this->config_imagemagick_path));
1543				} else {
1544					$commandline = phpthumb_functions::escapeshellarg_replacement($this->config_imagemagick_path);
1545				}
1546
1547			} else {
1548
1549				$which_convert = $this->ImageMagickWhichConvert();
1550				$IMversion     = $this->ImageMagickVersion();
1551
1552				if ($which_convert && ($which_convert[0] == '/') && $this->file_exists_ignoreopenbasedir($which_convert)) {
1553
1554					// `which convert` *should* return the path if "convert" exist, or nothing if it doesn't
1555					// other things *may* get returned, like "sh: convert: not found" or "no convert in /usr/local/bin /usr/sbin /usr/bin /usr/ccs/bin"
1556					// so only do this if the value returned exists as a file
1557					$this->DebugMessage('using ImageMagick path from `which convert` ('.$which_convert.')', __FILE__, __LINE__);
1558					$commandline = 'convert';
1559
1560				} elseif ($IMversion) {
1561
1562					$this->DebugMessage('setting ImageMagick path to $this->config_imagemagick_path ('.$this->config_imagemagick_path.') ['.$IMversion.']', __FILE__, __LINE__);
1563					$commandline = $this->config_imagemagick_path;
1564
1565				} else {
1566
1567					$this->DebugMessage('ImageMagickThumbnailToGD() aborting because cannot find convert in $this->config_imagemagick_path ('.$this->config_imagemagick_path.'), and `which convert` returned ('.$which_convert.')', __FILE__, __LINE__);
1568					$commandline = '';
1569
1570				}
1571
1572			}
1573
1574			@file_put_contents($IMcommandlineBaseCacheFilename, $commandline);
1575			@chmod($IMcommandlineBaseCacheFilename, $this->getParameter('config_file_create_mask'));
1576		}
1577		return $commandline;
1578	}
1579
1580
1581	public function ImageMagickVersion($returnRAW=false) {
1582		static $versionstring = null;
1583		if (null === $versionstring) {
1584			$versionstring = array(0=>false, 1=>false);
1585
1586			$IMversionCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMversion.txt';
1587			if ($cachedversionstring = @file_get_contents($IMversionCacheFilename)) {
1588
1589				$versionstring = explode("\n", $cachedversionstring, 2);
1590				$versionstring[0] = ($versionstring[0] ? $versionstring[0] : false); // "false" is stored as an empty string in the cache file
1591				$versionstring[1] = ($versionstring[1] ? $versionstring[1] : false); // "false" is stored as an empty string in the cache file
1592
1593			} else {
1594
1595				$commandline = $this->ImageMagickCommandlineBase();
1596				$commandline = (null !== $commandline ? $commandline : '');
1597				if ($commandline) {
1598					$commandline .= ' --version';
1599					$this->DebugMessage('ImageMagick version checked with "'.$commandline.'"', __FILE__, __LINE__);
1600					$versionstring[1] = trim(phpthumb_functions::SafeExec($commandline));
1601					if (preg_match('#^Version: [^\d]*([ 0-9\\.\\:Q/\\-]+)#i', $versionstring[1], $matches)) {
1602						$versionstring[0] = trim($matches[1]);
1603					} else {
1604						$versionstring[0] = false;
1605						$this->DebugMessage('ImageMagick did not return recognized version string ('.$versionstring[1].')', __FILE__, __LINE__);
1606					}
1607					$this->DebugMessage('ImageMagick convert --version says "'.@$matches[0].'"', __FILE__, __LINE__);
1608				}
1609
1610				@file_put_contents($IMversionCacheFilename, $versionstring[0]."\n".$versionstring[1]);
1611				@chmod($IMversionCacheFilename, $this->getParameter('config_file_create_mask'));
1612
1613			}
1614		}
1615		return $versionstring[ (int) $returnRAW ];
1616	}
1617
1618
1619	public function ImageMagickSwitchAvailable($switchname) {
1620		static $IMoptions = null;
1621		if (null === $IMoptions) {
1622			$IMoptions = array();
1623			$commandline = $this->ImageMagickCommandlineBase();
1624			if (null !== $commandline) {
1625				$commandline .= ' -help';
1626				$IMhelp_lines = explode("\n", phpthumb_functions::SafeExec($commandline));
1627				foreach ($IMhelp_lines as $line) {
1628					if (preg_match('#^[\\+\\-]([a-z\\-]+) #', trim($line), $matches)) {
1629						$IMoptions[$matches[1]] = true;
1630					}
1631				}
1632			}
1633		}
1634		if (is_array($switchname)) {
1635			$allOK = true;
1636			foreach ($switchname as $key => $value) {
1637				if (!isset($IMoptions[$value])) {
1638					$allOK = false;
1639					break;
1640				}
1641			}
1642			$this->DebugMessage('ImageMagickSwitchAvailable('.implode(';', $switchname).') = '. (int) $allOK .'', __FILE__, __LINE__);
1643		} else {
1644			$allOK = isset($IMoptions[$switchname]);
1645			$this->DebugMessage('ImageMagickSwitchAvailable('.$switchname.') = '. (int) $allOK .'', __FILE__, __LINE__);
1646		}
1647		return $allOK;
1648	}
1649
1650
1651	public function ImageMagickFormatsList() {
1652		static $IMformatsList = null;
1653		if (null === $IMformatsList) {
1654			$IMformatsList = '';
1655			$commandline = $this->ImageMagickCommandlineBase();
1656			if (!is_null($commandline)) {
1657				$commandline = dirname($commandline).DIRECTORY_SEPARATOR.str_replace('convert', 'identify', basename($commandline));
1658				$commandline .= ' -list format';
1659				$IMformatsList = phpthumb_functions::SafeExec($commandline);
1660			}
1661		}
1662		return $IMformatsList;
1663	}
1664
1665
1666	public function SourceDataToTempFile() {
1667		if ($IMtempSourceFilename = $this->phpThumb_tempnam()) {
1668			$IMtempSourceFilename = $this->realPathSafe($IMtempSourceFilename);
1669			ob_start();
1670			$fp_tempfile = fopen($IMtempSourceFilename, 'wb');
1671			$tempfile_open_error  = ob_get_contents();
1672			ob_end_clean();
1673			if ($fp_tempfile) {
1674				fwrite($fp_tempfile, $this->rawImageData);
1675				fclose($fp_tempfile);
1676				@chmod($IMtempSourceFilename, $this->getParameter('config_file_create_mask'));
1677				$this->sourceFilename = $IMtempSourceFilename;
1678				$this->DebugMessage('ImageMagickThumbnailToGD() setting $this->sourceFilename to "'.$IMtempSourceFilename.'" from $this->rawImageData ('.strlen($this->rawImageData).' bytes)', __FILE__, __LINE__);
1679			} else {
1680				$this->DebugMessage('ImageMagickThumbnailToGD() FAILED setting $this->sourceFilename to "'.$IMtempSourceFilename.'" (failed to open for writing: "'.$tempfile_open_error.'")', __FILE__, __LINE__);
1681			}
1682			unset($tempfile_open_error, $IMtempSourceFilename);
1683			return true;
1684		}
1685		$this->DebugMessage('SourceDataToTempFile() FAILED because $this->phpThumb_tempnam() failed', __FILE__, __LINE__);
1686		return false;
1687	}
1688
1689
1690	public function ImageMagickThumbnailToGD() {
1691		// http://www.imagemagick.org/script/command-line-options.php
1692
1693		$this->useRawIMoutput = true;
1694		if (phpthumb_functions::gd_version()) {
1695			// if GD is not available, must use whatever ImageMagick can output
1696
1697			// $CannotMagickParameters contains options that cannot be used with ImageMagick
1698			$CannotMagickParameters = array('ica');
1699			foreach ($CannotMagickParameters as $parameter) {
1700				if (isset($this->$parameter)) {
1701					$this->DebugMessage('cannot process with ImageMagick because "'.$parameter.'" is set', __FILE__, __LINE__);
1702					$this->useRawIMoutput = false;
1703					return false;
1704				}
1705			}
1706
1707			// $UnAllowedParameters contains options that can only be processed in GD, not ImageMagick
1708			// note: 'fltr' *may* need to be processed by GD, but we'll check that in more detail below
1709			$UnAllowedParameters = array('xto', 'ar', 'bg', 'bc');
1710			// 'ra' may be part of this list, if not a multiple of 90 degrees
1711			foreach ($UnAllowedParameters as $parameter) {
1712				if (isset($this->$parameter)) {
1713					$this->DebugMessage('$this->useRawIMoutput=false because "'.$parameter.'" is set', __FILE__, __LINE__);
1714					$this->useRawIMoutput = false;
1715					break;
1716				}
1717			}
1718		}
1719		$this->DebugMessage('$this->useRawIMoutput='.($this->useRawIMoutput ? 'true' : 'false').' after checking $UnAllowedParameters', __FILE__, __LINE__);
1720		$ImageCreateFunction = '';
1721		$outputFormat = $this->thumbnailFormat;
1722		if (phpthumb_functions::gd_version()) {
1723			if ($this->useRawIMoutput) {
1724				switch ($this->thumbnailFormat) {
1725					case 'gif':
1726						$ImageCreateFunction = 'imagecreatefromgif';
1727						$this->is_alpha = true;
1728						break;
1729					case 'png':
1730						$ImageCreateFunction = 'imagecreatefrompng';
1731						$this->is_alpha = true;
1732						break;
1733					case 'jpg':
1734					case 'jpeg':
1735						$ImageCreateFunction = 'imagecreatefromjpeg';
1736						break;
1737					case 'webp':
1738						$ImageCreateFunction = 'imagecreatefromwebp';
1739						$this->is_alpha = true;
1740						break;
1741					case 'avif':
1742						$ImageCreateFunction = 'imagecreatefromavif';
1743						$this->is_alpha = true;
1744						break;
1745					default:
1746						$this->DebugMessage('Forcing output to PNG because $this->thumbnailFormat ('.$this->thumbnailFormat.' is not a GD-supported format)', __FILE__, __LINE__);
1747						$outputFormat = 'png';
1748						$ImageCreateFunction = 'imagecreatefrompng';
1749						$this->is_alpha = true;
1750						$this->useRawIMoutput = false;
1751						break;
1752				}
1753				if (!function_exists($ImageCreateFunction)) {
1754					// ImageMagickThumbnailToGD() depends on imagecreatefrompng/imagecreatefromgif
1755					//$this->DebugMessage('ImageMagickThumbnailToGD() aborting because '.@$ImageCreateFunction.'() is not available', __FILE__, __LINE__);
1756					$this->useRawIMoutput = true;
1757					//return false;
1758				}
1759			} else {
1760				$outputFormat = 'png';
1761				$ImageCreateFunction = 'imagecreatefrompng';
1762				$this->is_alpha = true;
1763				$this->useRawIMoutput = false;
1764			}
1765		}
1766
1767		// http://freealter.org/doc_distrib/ImageMagick-5.1.1/www/convert.html
1768		if (!$this->sourceFilename && $this->rawImageData) {
1769			$this->SourceDataToTempFile();
1770		}
1771		if (!$this->sourceFilename) {
1772			$this->DebugMessage('ImageMagickThumbnailToGD() aborting because $this->sourceFilename is empty', __FILE__, __LINE__);
1773			$this->useRawIMoutput = false;
1774			return false;
1775		}
1776		if ($this->issafemode) {
1777			$this->DebugMessage('ImageMagickThumbnailToGD() aborting because safe_mode is enabled', __FILE__, __LINE__);
1778			$this->useRawIMoutput = false;
1779			return false;
1780		}
1781// TO BE FIXED
1782//if (true) {
1783//	$this->DebugMessage('ImageMagickThumbnailToGD() aborting it is broken right now', __FILE__, __LINE__);
1784//	$this->useRawIMoutput = false;
1785//	return false;
1786//}
1787
1788		$commandline = $this->ImageMagickCommandlineBase();
1789		if ($commandline) {
1790			$commandline .= ' '.phpthumb_functions::escapeshellarg_replacement(preg_replace('#[/\\\\]#', DIRECTORY_SEPARATOR, $this->sourceFilename).(($outputFormat == 'gif') ? '' : '['. (int) $this->sfn .']')); // [0] means first frame of (GIF) animation, can be ignored
1791			if ($IMtempfilename = $this->phpThumb_tempnam()) {
1792				$IMtempfilename = $this->realPathSafe($IMtempfilename);
1793
1794				$IMuseExplicitImageOutputDimensions = false;
1795				if ($this->ImageMagickSwitchAvailable('thumbnail') && $this->config_imagemagick_use_thumbnail) {
1796					$IMresizeParameter = 'thumbnail';
1797				} else {
1798					$IMresizeParameter = 'resize';
1799
1800					// some (older? around 2002) versions of IM won't accept "-resize 100x" but require "-resize 100x100"
1801					$commandline_test = $this->ImageMagickCommandlineBase().' logo: -resize 1x '.phpthumb_functions::escapeshellarg_replacement($IMtempfilename).' 2>&1';
1802					$IMresult_test = phpthumb_functions::SafeExec($commandline_test);
1803					$IMuseExplicitImageOutputDimensions = preg_match('#image dimensions are zero#i', $IMresult_test);
1804					$this->DebugMessage('IMuseExplicitImageOutputDimensions = '. (int) $IMuseExplicitImageOutputDimensions, __FILE__, __LINE__);
1805					if ($fp_im_temp = @fopen($IMtempfilename, 'wb')) {
1806						// erase temp image so ImageMagick logo doesn't get output if other processing fails
1807						fclose($fp_im_temp);
1808						@chmod($IMtempfilename, $this->getParameter('config_file_create_mask'));
1809					}
1810				}
1811
1812
1813				ob_start();
1814				$getimagesize = getimagesize($this->sourceFilename);
1815				$GetImageSizeError = ob_get_contents();
1816				ob_end_clean();
1817				if (is_array($getimagesize)) {
1818					$this->DebugMessage('getimagesize('.$this->sourceFilename.') SUCCEEDED: '.print_r($getimagesize, true), __FILE__, __LINE__);
1819				} else {
1820					$this->DebugMessage('getimagesize('.$this->sourceFilename.') FAILED with error "'.$GetImageSizeError.'"', __FILE__, __LINE__);
1821				}
1822				if (null !== $this->dpi && $this->ImageMagickSwitchAvailable('density')) {
1823					// for vector source formats only (WMF, PDF, etc)
1824					if (is_array($getimagesize) && isset($getimagesize[2]) && ($getimagesize[2] == IMAGETYPE_PNG)) {
1825						// explicitly exclude PNG from "-flatten" to make sure transparency is preserved
1826						// https://github.com/JamesHeinrich/phpThumb/issues/65
1827					} else {
1828						$commandline .= ' -flatten';
1829						$commandline .= ' -density '.phpthumb_functions::escapeshellarg_replacement($this->dpi);
1830					}
1831				}
1832				if (is_array($getimagesize)) {
1833					$this->DebugMessage('getimagesize('.$this->sourceFilename.') returned [w='.$getimagesize[0].';h='.$getimagesize[1].';f='.$getimagesize[2].']', __FILE__, __LINE__);
1834					$this->source_width  = $getimagesize[0];
1835					$this->source_height = $getimagesize[1];
1836					$this->DebugMessage('source dimensions set to '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__);
1837					$this->SetOrientationDependantWidthHeight();
1838
1839					if (!preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat)) {
1840						// not a transparency-capable format
1841						$commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('#'.($this->bg ? $this->bg : 'FFFFFF'));
1842						if (!stristr($commandline, ' -flatten')) {
1843							$commandline .= ' -flatten';
1844						}
1845					} else {
1846						if ($getimagesize[2] == IMAGETYPE_PNG && !$this->bg) {
1847							$commandline .= ' -background none';
1848						}
1849					}
1850					if ($getimagesize[2] == IMAGETYPE_GIF) {
1851						$commandline .= ' -coalesce'; // may be needed for animated GIFs
1852					}
1853					if ($this->source_width || $this->source_height) {
1854						if ($this->zc) {
1855
1856							$borderThickness = 0;
1857							if (!empty($this->fltr)) {
1858								foreach ($this->fltr as $key => $value) {
1859									if (preg_match('#^bord\|([\d]+)#', $value, $matches)) {
1860										$borderThickness = $matches[1];
1861										break;
1862									}
1863								}
1864							}
1865							$wAll = (int) max($this->w, $this->wp, $this->wl, $this->ws) - (2 * $borderThickness);
1866							$hAll = (int) max($this->h, $this->hp, $this->hl, $this->hs) - (2 * $borderThickness);
1867							$imAR = $this->source_width / $this->source_height;
1868							$zcAR = (($wAll && $hAll) ? $wAll / $hAll : 1);
1869							$side  = phpthumb_functions::nonempty_min($this->source_width, $this->source_height, max($wAll, $hAll));
1870							$sideX = phpthumb_functions::nonempty_min($this->source_width,                       $wAll, round($hAll * $zcAR));
1871							$sideY = phpthumb_functions::nonempty_min(                     $this->source_height, $hAll, round($wAll / $zcAR));
1872
1873							$thumbnailH = round(max($sideY, ($sideY * $zcAR) / $imAR));
1874							if ($this->aoe == 1) {
1875								$commandline .= ' -'.$IMresizeParameter.' "'.$wAll.'x'.$hAll.'^"';
1876							} else {
1877								$commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($IMuseExplicitImageOutputDimensions ? $thumbnailH : '').'x'.$thumbnailH);
1878							}
1879
1880							switch (strtoupper($this->zc)) {
1881								case 'T':
1882									$commandline .= ' -gravity north';
1883									break;
1884								case 'B':
1885									$commandline .= ' -gravity south';
1886									break;
1887								case 'L':
1888									$commandline .= ' -gravity west';
1889									break;
1890								case 'R':
1891									$commandline .= ' -gravity east';
1892									break;
1893								case 'TL':
1894									$commandline .= ' -gravity northwest';
1895									break;
1896								case 'TR':
1897									$commandline .= ' -gravity northeast';
1898									break;
1899								case 'BL':
1900									$commandline .= ' -gravity southwest';
1901									break;
1902								case 'BR':
1903									$commandline .= ' -gravity southeast';
1904									break;
1905								case '1':
1906								case 'C':
1907								default:
1908									$commandline .= ' -gravity center';
1909									break;
1910							}
1911
1912							if (($wAll > 0) && ($hAll > 0)) {
1913								$commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($wAll.'x'.$hAll.'+0+0');
1914							} else {
1915								$commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($side.'x'.$side.'+0+0');
1916							}
1917							if ($this->ImageMagickSwitchAvailable('repage')) {
1918								$commandline .= ' +repage';
1919							} else {
1920								$this->DebugMessage('Skipping "+repage" because ImageMagick (v'.$this->ImageMagickVersion().') does not support it', __FILE__, __LINE__);
1921							}
1922
1923						} elseif ($this->sw || $this->sh || $this->sx || $this->sy) {
1924
1925							$crop_param   = '';
1926							$crop_param  .=     ($this->sw ? (($this->sw < 2) ? round($this->sw * $this->source_width)  : $this->sw) : $this->source_width);
1927							$crop_param  .= 'x'.($this->sh ? (($this->sh < 2) ? round($this->sh * $this->source_height) : $this->sh) : $this->source_height);
1928							$crop_param  .= '+'.(($this->sx < 2) ? round($this->sx * $this->source_width)  : $this->sx);
1929							$crop_param  .= '+'.(($this->sy < 2) ? round($this->sy * $this->source_height) : $this->sy);
1930// TO BE FIXED
1931// makes 1x1 output
1932// http://trainspotted.com/phpThumb/phpThumb.php?src=/content/CNR/47/CNR-4728-LD-L-20110723-898.jpg&w=100&h=100&far=1&f=png&fltr[]=lvl&sx=0.05&sy=0.25&sw=0.92&sh=0.42
1933// '/usr/bin/convert' -density 150 -thumbnail 100x100 -contrast-stretch '0.1%' '/var/www/vhosts/trainspotted.com/httpdocs/content/CNR/47/CNR-4728-LD-L-20110723-898.jpg[0]' png:'/var/www/vhosts/trainspotted.com/httpdocs/phpThumb/_cache/pThumbIIUlvj'
1934							$commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($crop_param);
1935
1936							// this is broken for aoe=1, but unsure how to fix. Send advice to info@silisoftware.com
1937							if ($this->w || $this->h) {
1938								//if ($this->ImageMagickSwitchAvailable('repage')) {
1939if (false) {
1940// TO BE FIXED
1941// newer versions of ImageMagick require -repage <geometry>
1942									$commandline .= ' -repage';
1943								} else {
1944									$this->DebugMessage('Skipping "-repage" because ImageMagick (v'.$this->ImageMagickVersion().') does not support it', __FILE__, __LINE__);
1945								}
1946								if ($IMuseExplicitImageOutputDimensions) {
1947									if ($this->w && !$this->h) {
1948										$this->h = ceil($this->w / ($this->source_width / $this->source_height));
1949									} elseif ($this->h && !$this->w) {
1950										$this->w = ceil($this->h * ($this->source_width / $this->source_height));
1951									}
1952								}
1953								$commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h);
1954							}
1955
1956						} else {
1957
1958							if ($this->iar && ((int) $this->w > 0) && ((int) $this->h > 0)) {
1959
1960								list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra);
1961								$nw = ((round($nw) != 0) ? round($nw) : '');
1962								$nh = ((round($nh) != 0) ? round($nh) : '');
1963								$commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($nw.'x'.$nh.'!');
1964
1965							} elseif ($this->far && ((int) $this->w > 0) && ((int) $this->h > 0)) {
1966
1967								$commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(phpthumb_functions::nonempty_min($this->w, $getimagesize[0]).'x'.phpthumb_functions::nonempty_min($this->h, $getimagesize[1]));
1968								$commandline .= ' -gravity center';
1969								if ($this->bg) {
1970									$commandline .= ' -background ' . phpthumb_functions::escapeshellarg_replacement('#' . $this->bg);
1971								} else {
1972									$commandline .= ' -background none';
1973								}
1974								$commandline .= ' -extent '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h);
1975
1976							} else {
1977
1978								$this->w = (($this->aoe && $this->w) ? $this->w : ($this->w ? phpthumb_functions::nonempty_min($this->w, $getimagesize[0]) : null));
1979								$this->h = (($this->aoe && $this->h) ? $this->h : ($this->h ? phpthumb_functions::nonempty_min($this->h, $getimagesize[1]) : null));
1980								if ($this->w || $this->h) {
1981									if ($IMuseExplicitImageOutputDimensions) {
1982										if ($this->w && !$this->h) {
1983											$this->h = ceil($this->w / ($this->source_width / $this->source_height));
1984										} elseif ($this->h && !$this->w) {
1985											$this->w = ceil($this->h * ($this->source_width / $this->source_height));
1986										}
1987									}
1988									list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra);
1989									$nw = ((round($nw) != 0) ? round($nw) : '');
1990									$nh = ((round($nh) != 0) ? round($nh) : '');
1991									$commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($nw.'x'.$nh);
1992								}
1993
1994							}
1995						}
1996					}
1997
1998				} else {
1999
2000					$this->DebugMessage('getimagesize('.$this->sourceFilename.') failed', __FILE__, __LINE__);
2001					if ($this->w || $this->h) {
2002						$exactDimensionsBang = (($this->iar && ((int) $this->w > 0) && ((int) $this->h > 0)) ? '!' : '');
2003						if ($IMuseExplicitImageOutputDimensions) {
2004							// unknown source aspect ratio, just put large number and hope IM figures it out
2005							$commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($this->w ? $this->w : '9999').'x'.($this->h ? $this->h : '9999').$exactDimensionsBang);
2006						} else {
2007							$commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h.$exactDimensionsBang);
2008						}
2009					}
2010
2011				}
2012
2013				if ($this->ra) {
2014					$this->ra = (int) $this->ra;
2015					if ($this->ImageMagickSwitchAvailable('rotate')) {
2016						if (!preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat) || phpthumb_functions::version_compare_replacement($this->ImageMagickVersion(), '6.3.7', '>=')) {
2017							$this->DebugMessage('Using ImageMagick rotate', __FILE__, __LINE__);
2018							$commandline .= ' -rotate '.phpthumb_functions::escapeshellarg_replacement($this->ra);
2019							if (($this->ra % 90) != 0) {
2020								if (preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat)) {
2021									// alpha-capable format
2022									$commandline .= ' -background rgba(255,255,255,0)';
2023								} else {
2024									$commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('#'.($this->bg ? $this->bg : 'FFFFFF'));
2025								}
2026							}
2027							$this->ra = 0;
2028						} else {
2029							$this->DebugMessage('Not using ImageMagick rotate because alpha background buggy before v6.3.7', __FILE__, __LINE__);
2030						}
2031					} else {
2032						$this->DebugMessage('Not using ImageMagick rotate because not supported', __FILE__, __LINE__);
2033					}
2034				}
2035
2036				$successfullyProcessedFilters = array();
2037				foreach ($this->fltr as $filterkey => $filtercommand) {
2038					@list($command, $parameter) = explode('|', $filtercommand, 2);
2039					switch ($command) {
2040						case 'brit':
2041							if ($this->ImageMagickSwitchAvailable('modulate')) {
2042								$commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement((100 + (int) $parameter).',100,100');
2043								$successfullyProcessedFilters[] = $filterkey;
2044							}
2045							break;
2046
2047						case 'cont':
2048							if ($this->ImageMagickSwitchAvailable('contrast')) {
2049								$contDiv10 = round((int) $parameter / 10);
2050								if ($contDiv10 > 0) {
2051									$contDiv10 = min($contDiv10, 100);
2052									for ($i = 0; $i < $contDiv10; $i++) {
2053										$commandline .= ' -contrast'; // increase contrast by 10%
2054									}
2055								} elseif ($contDiv10 < 0) {
2056									$contDiv10 = max($contDiv10, -100);
2057									for ($i = $contDiv10; $i < 0; $i++) {
2058										$commandline .= ' +contrast'; // decrease contrast by 10%
2059									}
2060								} else {
2061									// do nothing
2062								}
2063								$successfullyProcessedFilters[] = $filterkey;
2064							}
2065							break;
2066
2067						case 'ds':
2068							if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
2069								if ($parameter == 100) {
2070									$commandline .= ' -colorspace GRAY';
2071									$commandline .= ' -modulate 100,0,100';
2072								} else {
2073									$commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement('100,'.(100 - (int) $parameter).',100');
2074								}
2075								$successfullyProcessedFilters[] = $filterkey;
2076							}
2077							break;
2078
2079						case 'sat':
2080							if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
2081								if ($parameter == -100) {
2082									$commandline .= ' -colorspace GRAY';
2083									$commandline .= ' -modulate 100,0,100';
2084								} else {
2085									$commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement('100,'.(100 + (int) $parameter).',100');
2086								}
2087								$successfullyProcessedFilters[] = $filterkey;
2088							}
2089							break;
2090
2091						case 'gray':
2092							if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
2093								$commandline .= ' -colorspace GRAY';
2094								$commandline .= ' -modulate 100,0,100';
2095								$successfullyProcessedFilters[] = $filterkey;
2096							}
2097							break;
2098
2099						case 'clr':
2100							if ($this->ImageMagickSwitchAvailable(array('fill', 'colorize'))) {
2101								@list($amount, $color) = explode('|', $parameter);
2102								$commandline .= ' -fill '.phpthumb_functions::escapeshellarg_replacement('#'.preg_replace('#[^0-9A-F]#i', '', $color));
2103								$commandline .= ' -colorize '.phpthumb_functions::escapeshellarg_replacement(min(max((int) $amount, 0), 100));
2104                                $successfullyProcessedFilters[] = $filterkey;
2105							}
2106							break;
2107
2108						case 'sep':
2109							if ($this->ImageMagickSwitchAvailable('sepia-tone')) {
2110								@list($amount, $color) = explode('|', $parameter);
2111								$amount = ($amount ? $amount : 80);
2112								if (!$color) {
2113									$commandline .= ' -sepia-tone '.phpthumb_functions::escapeshellarg_replacement(min(max((int) $amount, 0), 100).'%');
2114									$successfullyProcessedFilters[] = $filterkey;
2115								}
2116							}
2117							break;
2118
2119						case 'gam':
2120							@list($amount) = explode('|', $parameter);
2121							$amount = min(max((float) $amount, 0.001), 10);
2122							if (number_format($amount, 3) != '1.000') {
2123								if ($this->ImageMagickSwitchAvailable('gamma')) {
2124									$commandline .= ' -gamma '.phpthumb_functions::escapeshellarg_replacement($amount);
2125									$successfullyProcessedFilters[] = $filterkey;
2126								}
2127							}
2128							break;
2129
2130						case 'neg':
2131							if ($this->ImageMagickSwitchAvailable('negate')) {
2132								$commandline .= ' -negate';
2133								$successfullyProcessedFilters[] = $filterkey;
2134							}
2135							break;
2136
2137						case 'th':
2138							@list($amount) = explode('|', $parameter);
2139							if ($this->ImageMagickSwitchAvailable(array('threshold', 'dither', 'monochrome'))) {
2140								$commandline .= ' -threshold '.phpthumb_functions::escapeshellarg_replacement(round(min(max((int) $amount, 0), 255) / 2.55).'%');
2141								$commandline .= ' -dither';
2142								$commandline .= ' -monochrome';
2143								$successfullyProcessedFilters[] = $filterkey;
2144							}
2145							break;
2146
2147						case 'rcd':
2148							if ($this->ImageMagickSwitchAvailable(array('colors', 'dither'))) {
2149								@list($colors, $dither) = explode('|', $parameter);
2150								$colors = ($colors                ?  (int) $colors : 256);
2151								$dither  = ((strlen($dither) > 0) ? (bool) $dither : true);
2152								$commandline .= ' -colors '.phpthumb_functions::escapeshellarg_replacement(max($colors, 8)); // ImageMagick will otherwise fail with "cannot quantize to fewer than 8 colors"
2153								$commandline .= ($dither ? ' -dither' : ' +dither');
2154								$successfullyProcessedFilters[] = $filterkey;
2155							}
2156							break;
2157
2158						case 'flip':
2159							if ($this->ImageMagickSwitchAvailable(array('flip', 'flop'))) {
2160								if (strpos(strtolower($parameter), 'x') !== false) {
2161									$commandline .= ' -flop';
2162								}
2163								if (strpos(strtolower($parameter), 'y') !== false) {
2164									$commandline .= ' -flip';
2165								}
2166								$successfullyProcessedFilters[] = $filterkey;
2167							}
2168							break;
2169
2170						case 'edge':
2171							if ($this->ImageMagickSwitchAvailable('edge')) {
2172								$parameter = (!empty($parameter) ? $parameter : 2);
2173								$commandline .= ' -edge '.phpthumb_functions::escapeshellarg_replacement(!empty($parameter) ? (int) $parameter : 1);
2174								$successfullyProcessedFilters[] = $filterkey;
2175							}
2176							break;
2177
2178						case 'emb':
2179							if ($this->ImageMagickSwitchAvailable(array('emboss', 'negate'))) {
2180								$parameter = (!empty($parameter) ? $parameter : 2);
2181								$commandline .= ' -emboss '.phpthumb_functions::escapeshellarg_replacement((int) $parameter);
2182								if ($parameter < 2) {
2183									$commandline .= ' -negate'; // ImageMagick negates the image for some reason with '-emboss 1';
2184								}
2185								$successfullyProcessedFilters[] = $filterkey;
2186							}
2187							break;
2188
2189						case 'lvl':
2190							@list($band, $method, $threshold) = explode('|', $parameter);
2191							$band      = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band))       : '*');
2192							$method    = ((strlen($method) > 0)    ? (int) $method :   2);
2193							$threshold = ((strlen($threshold) > 0) ? min(max((float) $threshold, 0), 100) : 0.1);
2194
2195							$band = preg_replace('#[^RGBA\\*]#', '', strtoupper($band));
2196
2197							if (($method > 1) && !$this->ImageMagickSwitchAvailable(array('channel', 'contrast-stretch'))) {
2198								// Because ImageMagick processing happens before PHP-GD filters, and because some
2199								// clipping is involved in the "lvl" filter, if "lvl" happens before "wb" then the
2200								// "wb" filter will have (almost) no effect. Therefore, if "wb" is enabled then
2201								// force the "lvl" filter to be processed by GD, not ImageMagick.
2202								foreach ($this->fltr as $fltr_key => $fltr_value) {
2203									list($fltr_cmd) = explode('|', $fltr_value);
2204									if ($fltr_cmd == 'wb') {
2205										$this->DebugMessage('Setting "lvl" filter method to "0" (from "'.$method.'") because white-balance filter also enabled', __FILE__, __LINE__);
2206										$method = 0;
2207									}
2208								}
2209							}
2210
2211							switch ($method) {
2212								case 0: // internal RGB
2213								case 1: // internal grayscale
2214									break;
2215								case 2: // ImageMagick "contrast-stretch"
2216									if ($this->ImageMagickSwitchAvailable('contrast-stretch')) {
2217										if ($band != '*') {
2218											$commandline .= ' -channel '.phpthumb_functions::escapeshellarg_replacement(strtoupper($band));
2219										}
2220										$threshold = preg_replace('#[^0-9\\.]#', '', $threshold); // should be unneccesary, but just to be double-sure
2221										//$commandline .= ' -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%');
2222										$commandline .= ' -contrast-stretch \''.$threshold.'%\'';
2223										if ($band != '*') {
2224											$commandline .= ' +channel';
2225										}
2226										$successfullyProcessedFilters[] = $filterkey;
2227									}
2228									break;
2229								case 3: // ImageMagick "normalize"
2230									if ($this->ImageMagickSwitchAvailable('normalize')) {
2231										if ($band != '*') {
2232											$commandline .= ' -channel '.phpthumb_functions::escapeshellarg_replacement(strtoupper($band));
2233										}
2234										$commandline .= ' -normalize';
2235										if ($band != '*') {
2236											$commandline .= ' +channel';
2237										}
2238										$successfullyProcessedFilters[] = $filterkey;
2239									}
2240									break;
2241								default:
2242									$this->DebugMessage('unsupported method ('.$method.') for "lvl" filter', __FILE__, __LINE__);
2243									break;
2244							}
2245							if (isset($this->fltr[$filterkey]) && ($method > 1)) {
2246								$this->fltr[$filterkey] = $command.'|'.$band.'|0|'.$threshold;
2247								$this->DebugMessage('filter "lvl" remapped from method "'.$method.'" to method "0" because ImageMagick support is missing', __FILE__, __LINE__);
2248							}
2249							break;
2250
2251						case 'wb':
2252							if ($this->ImageMagickSwitchAvailable(array('channel', 'contrast-stretch'))) {
2253								@list($threshold) = explode('|', $parameter);
2254								$threshold = (!empty($threshold) ? min(max((float) $threshold, 0), 100) : 0.1);
2255								$threshold = preg_replace('#[^0-9\\.]#', '', $threshold); // should be unneccesary, but just to be double-sure
2256								//$commandline .= ' -channel R -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // doesn't work on Windows because most versions of PHP do not properly
2257								//$commandline .= ' -channel G -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // escape special characters (such as %) and just replace them with spaces
2258								//$commandline .= ' -channel B -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // https://bugs.php.net/bug.php?id=43261
2259								$commandline .= ' -channel R -contrast-stretch \''.$threshold.'%\'';
2260								$commandline .= ' -channel G -contrast-stretch \''.$threshold.'%\'';
2261								$commandline .= ' -channel B -contrast-stretch \''.$threshold.'%\'';
2262								$commandline .= ' +channel';
2263								$successfullyProcessedFilters[] = $filterkey;
2264							}
2265							break;
2266
2267						case 'blur':
2268							if ($this->ImageMagickSwitchAvailable('blur')) {
2269								@list($radius) = explode('|', $parameter);
2270								$radius = (!empty($radius) ? min(max((int) $radius, 0), 25) : 1);
2271								$commandline .= ' -blur '.phpthumb_functions::escapeshellarg_replacement($radius);
2272								$successfullyProcessedFilters[] = $filterkey;
2273							}
2274							break;
2275
2276						case 'gblr':
2277							@list($radius) = explode('|', $parameter);
2278							$radius = (!empty($radius) ? min(max((int) $radius, 0), 25) : 1);
2279							// "-gaussian" changed to "-gaussian-blur" sometime around 2009
2280							if ($this->ImageMagickSwitchAvailable('gaussian-blur')) {
2281								$commandline .= ' -gaussian-blur '.phpthumb_functions::escapeshellarg_replacement($radius);
2282								$successfullyProcessedFilters[] = $filterkey;
2283							} elseif ($this->ImageMagickSwitchAvailable('gaussian')) {
2284								$commandline .= ' -gaussian '.phpthumb_functions::escapeshellarg_replacement($radius);
2285								$successfullyProcessedFilters[] = $filterkey;
2286							}
2287							break;
2288
2289						case 'usm':
2290							if ($this->ImageMagickSwitchAvailable('unsharp')) {
2291								@list($amount, $radius, $threshold) = explode('|', $parameter);
2292								$amount    = ($amount            ? min(max((int) $amount,    0), 255) : 80);
2293								$radius    = ($radius            ? min(max((int) $radius,    0),  10) : 0.5);
2294								$threshold = ('' !== $threshold ? min(max((int) $threshold, 0),  50) : 3);
2295								$commandline .= ' -unsharp '.phpthumb_functions::escapeshellarg_replacement(number_format(($radius * 2) - 1, 2, '.', '').'x1+'.number_format($amount / 100, 2, '.', '').'+'.number_format($threshold / 100, 2, '.', ''));
2296								$successfullyProcessedFilters[] = $filterkey;
2297							}
2298							break;
2299
2300						case 'bord':
2301							if ($this->ImageMagickSwitchAvailable(array('border', 'bordercolor', 'thumbnail', 'crop'))) {
2302								if (!$this->zc) {
2303									@list($width, $rX, $rY, $color) = explode('|', $parameter);
2304									$width = (int) $width;
2305									$rX    = (int) $rX;
2306									$rY    = (int) $rY;
2307									if ($width && !$rX && !$rY) {
2308										if (!phpthumb_functions::IsHexColor($color)) {
2309											$color = ((!empty($this->bc) && phpthumb_functions::IsHexColor($this->bc)) ? $this->bc : '000000');
2310										}
2311										$commandline .= ' -border '.phpthumb_functions::escapeshellarg_replacement((int) $width);
2312										$commandline .= ' -bordercolor '.phpthumb_functions::escapeshellarg_replacement('#'.$color);
2313
2314										if (preg_match('# \\-crop "([\d]+)x([\d]+)\\+0\\+0" #', $commandline, $matches)) {
2315											$commandline = str_replace(' -crop "'.$matches[1].'x'.$matches[2].'+0+0" ', ' -crop '.phpthumb_functions::escapeshellarg_replacement(($matches[1] - (2 * $width)).'x'.($matches[2] - (2 * $width)).'+0+0').' ', $commandline);
2316										} elseif (preg_match('# \\-'.$IMresizeParameter.' "([0-9]+)x([0-9]+)" #', $commandline, $matches)) {
2317											$commandline = str_replace(' -'.$IMresizeParameter.' "'.$matches[1].'x'.$matches[2].'" ', ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($matches[1] - (2 * $width)).'x'.($matches[2] - (2 * $width))).' ', $commandline);
2318										}
2319										$successfullyProcessedFilters[] = $filterkey;
2320									}
2321								}
2322							}
2323							break;
2324
2325						case 'crop':
2326							break;
2327
2328						case 'sblr':
2329							break;
2330
2331						case 'mean':
2332							break;
2333
2334						case 'smth':
2335							break;
2336
2337						case 'bvl':
2338							break;
2339
2340						case 'wmi':
2341							break;
2342
2343						case 'wmt':
2344							break;
2345
2346						case 'over':
2347							break;
2348
2349						case 'hist':
2350							break;
2351
2352						case 'fram':
2353							break;
2354
2355						case 'drop':
2356							break;
2357
2358						case 'mask':
2359							break;
2360
2361						case 'elip':
2362							break;
2363
2364						case 'ric':
2365							break;
2366
2367						case 'stc':
2368							break;
2369
2370						case 'size':
2371							break;
2372
2373						default:
2374							$this->DebugMessage('Unknown $this->fltr['.$filterkey.'] ('.$filtercommand.') -- deleting filter command', __FILE__, __LINE__);
2375							$successfullyProcessedFilters[] = $filterkey;
2376							break;
2377					}
2378					if (!isset($this->fltr[$filterkey])) {
2379						$this->DebugMessage('Processed $this->fltr['.$filterkey.'] ('.$filtercommand.') with ImageMagick', __FILE__, __LINE__);
2380					} else {
2381						$this->DebugMessage('Skipping $this->fltr['.$filterkey.'] ('.$filtercommand.') with ImageMagick', __FILE__, __LINE__);
2382					}
2383				}
2384				$this->DebugMessage('Remaining $this->fltr after ImageMagick: ('.$this->phpThumbDebugVarDump($this->fltr).')', __FILE__, __LINE__);
2385				if (count($this->fltr) > 0) {
2386					$this->useRawIMoutput = false;
2387				}
2388
2389				if (preg_match('#jpe?g#i', $outputFormat) && $this->q) {
2390					if ($this->ImageMagickSwitchAvailable(array('quality', 'interlace'))) {
2391						$commandline .= ' -quality '.phpthumb_functions::escapeshellarg_replacement($this->thumbnailQuality);
2392						if ($this->config_output_interlace) {
2393							// causes weird things with animated GIF... leave for JPEG only
2394							$commandline .= ' -interlace line '; // Use Line or Plane to create an interlaced PNG or GIF or progressive JPEG image
2395						}
2396					}
2397				}
2398				$commandline .= ' '.$outputFormat.':'.phpthumb_functions::escapeshellarg_replacement($IMtempfilename);
2399				if (!$this->iswindows) {
2400					$commandline .= ' 2>&1';
2401				}
2402				$this->DebugMessage('ImageMagick called as ('.$commandline.')', __FILE__, __LINE__);
2403				$IMresult = phpthumb_functions::SafeExec($commandline);
2404				clearstatcache();
2405				if (!@file_exists($IMtempfilename) || !@filesize($IMtempfilename)) {
2406					$this->FatalError('ImageMagick failed with message ('.trim($IMresult).')');
2407					$this->DebugMessage('ImageMagick failed with message ('.trim($IMresult).')', __FILE__, __LINE__);
2408					if ($this->iswindows && !$IMresult) {
2409						$this->DebugMessage('Check to make sure that PHP has read+write permissions to "'.dirname($IMtempfilename).'"', __FILE__, __LINE__);
2410					}
2411
2412				} else {
2413
2414					foreach ($successfullyProcessedFilters as $dummy => $filterkey) {
2415						unset($this->fltr[$filterkey]);
2416					}
2417					$this->IMresizedData = file_get_contents($IMtempfilename);
2418					$getimagesize_imresized = @getimagesize($IMtempfilename);
2419					$this->DebugMessage('getimagesize('.$IMtempfilename.') returned [w='.$getimagesize_imresized[0].';h='.$getimagesize_imresized[1].';f='.$getimagesize_imresized[2].']', __FILE__, __LINE__);
2420					if (($this->config_max_source_pixels > 0) && (($getimagesize_imresized[0] * $getimagesize_imresized[1]) > $this->config_max_source_pixels)) {
2421						$this->DebugMessage('skipping ImageMagickThumbnailToGD::'.$ImageCreateFunction.'() because IM output is too large ('.$getimagesize_imresized[0].'x'.$getimagesize_imresized[0].' = '.($getimagesize_imresized[0] * $getimagesize_imresized[1]).' > '.$this->config_max_source_pixels.')', __FILE__, __LINE__);
2422					} elseif (function_exists(@$ImageCreateFunction) && ($this->gdimg_source = @$ImageCreateFunction($IMtempfilename))) {
2423						$this->source_width  = imagesx($this->gdimg_source);
2424						$this->source_height = imagesy($this->gdimg_source);
2425						$this->DebugMessage('ImageMagickThumbnailToGD::'.$ImageCreateFunction.'() succeeded, $this->gdimg_source is now ('.$this->source_width.'x'.$this->source_height.')', __FILE__, __LINE__);
2426						$this->DebugMessage('ImageMagickThumbnailToGD() returning $this->IMresizedData ('.strlen($this->IMresizedData).' bytes)', __FILE__, __LINE__);
2427					} else {
2428						$this->useRawIMoutput = true;
2429						$this->DebugMessage('$this->useRawIMoutput set to TRUE because '.@$ImageCreateFunction.'('.$IMtempfilename.') failed', __FILE__, __LINE__);
2430					}
2431					if (file_exists($IMtempfilename)) {
2432						$this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__);
2433						@unlink($IMtempfilename);
2434					}
2435					return true;
2436
2437				}
2438				if (file_exists($IMtempfilename)) {
2439					$this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__);
2440					@unlink($IMtempfilename);
2441				}
2442
2443			} elseif ($this->issafemode) {
2444				$this->DebugMessage('ImageMagickThumbnailToGD() aborting because PHP safe_mode is enabled and phpThumb_tempnam() failed', __FILE__, __LINE__);
2445				$this->useRawIMoutput = false;
2446			} else {
2447				if (file_exists($IMtempfilename)) {
2448					$this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__);
2449					@unlink($IMtempfilename);
2450				}
2451				$this->DebugMessage('ImageMagickThumbnailToGD() aborting, phpThumb_tempnam() failed', __FILE__, __LINE__);
2452			}
2453		} else {
2454			$this->DebugMessage('ImageMagickThumbnailToGD() aborting because ImageMagickCommandlineBase() failed', __FILE__, __LINE__);
2455		}
2456		$this->useRawIMoutput = false;
2457		return false;
2458	}
2459
2460
2461	public function Rotate() {
2462		if ($this->ra || $this->ar) {
2463			if (!function_exists('imagerotate')) {
2464				$this->DebugMessage('!function_exists(imagerotate)', __FILE__, __LINE__);
2465				return false;
2466			}
2467			if (!include_once __DIR__ .'/phpthumb.filters.php' ) {
2468				$this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying filters ('.implode(';', $this->fltr).')', __FILE__, __LINE__);
2469				return false;
2470			}
2471
2472			$this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
2473			if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
2474				return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
2475			}
2476
2477			$rotate_angle = 0;
2478			if ($this->ra) {
2479
2480				$rotate_angle = (float) $this->ra;
2481
2482			} else {
2483
2484				if ($this->ar == 'x') {
2485					if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.2.0', '>=')) {
2486						if ($this->sourceFilename) {
2487							if (function_exists('exif_read_data')) {
2488								if ($exif_data = @exif_read_data($this->sourceFilename, 'IFD0')) {
2489									// http://sylvana.net/jpegcrop/exif_orientation.html
2490									switch (@$exif_data['Orientation']) {
2491										case 1:
2492											$rotate_angle = 0;
2493											break;
2494										case 3:
2495											$rotate_angle = 180;
2496											break;
2497										case 6:
2498											$rotate_angle = 270;
2499											break;
2500										case 8:
2501											$rotate_angle = 90;
2502											break;
2503
2504										default:
2505											$this->DebugMessage('EXIF auto-rotate failed because unknown $exif_data[Orientation] "'.@$exif_data['Orientation'].'"', __FILE__, __LINE__);
2506											return false;
2507											break;
2508									}
2509									$this->DebugMessage('EXIF auto-rotate set to '.$rotate_angle.' degrees ($exif_data[Orientation] = "'.@$exif_data['Orientation'].'")', __FILE__, __LINE__);
2510								} else {
2511									$this->DebugMessage('failed: exif_read_data('.$this->sourceFilename.')', __FILE__, __LINE__);
2512									return false;
2513								}
2514							} else {
2515								$this->DebugMessage('!function_exists(exif_read_data)', __FILE__, __LINE__);
2516								return false;
2517							}
2518						} else {
2519							$this->DebugMessage('Cannot auto-rotate from EXIF data because $this->sourceFilename is empty', __FILE__, __LINE__);
2520							return false;
2521						}
2522					} else {
2523						$this->DebugMessage('Cannot auto-rotate from EXIF data because PHP is less than v4.2.0 ('. PHP_VERSION .')', __FILE__, __LINE__);
2524						return false;
2525					}
2526				} elseif (($this->ar == 'l') && ($this->source_height > $this->source_width)) {
2527					$rotate_angle = 270;
2528				} elseif (($this->ar == 'L') && ($this->source_height > $this->source_width)) {
2529					$rotate_angle = 90;
2530				} elseif (($this->ar == 'p') && ($this->source_width > $this->source_height)) {
2531					$rotate_angle = 90;
2532				} elseif (($this->ar == 'P') && ($this->source_width > $this->source_height)) {
2533					$rotate_angle = 270;
2534				}
2535
2536			}
2537			if ($rotate_angle % 90) {
2538				$this->is_alpha = true;
2539			}
2540			phpthumb_filters::ImprovedImageRotate($this->gdimg_source, $rotate_angle, $this->config_background_hexcolor, $this->bg, $this);
2541			$this->source_width  = imagesx($this->gdimg_source);
2542			$this->source_height = imagesy($this->gdimg_source);
2543		}
2544		return true;
2545	}
2546
2547
2548	public function FixedAspectRatio() {
2549		// optional fixed-dimension images (regardless of aspect ratio)
2550
2551		if (!$this->far) {
2552			// do nothing
2553			return true;
2554		}
2555
2556		if (!$this->w || !$this->h) {
2557			return false;
2558		}
2559		$this->thumbnail_width  = $this->w;
2560		$this->thumbnail_height = $this->h;
2561		$this->is_alpha = true;
2562		if ($this->thumbnail_image_width >= $this->thumbnail_width) {
2563
2564			$aspectratio = $this->thumbnail_image_height / $this->thumbnail_image_width;
2565			if ($this->w) {
2566				$this->thumbnail_image_height = round($this->thumbnail_image_width * $aspectratio);
2567				$this->thumbnail_height = ($this->h ? $this->h : $this->thumbnail_image_height);
2568			} elseif ($this->thumbnail_image_height < $this->thumbnail_height) {
2569				$this->thumbnail_image_height = $this->thumbnail_height;
2570				$this->thumbnail_image_width  = round($this->thumbnail_image_height / $aspectratio);
2571			}
2572
2573		} else {
2574
2575			$aspectratio = $this->thumbnail_image_width / $this->thumbnail_image_height;
2576			if ($this->h) {
2577				$this->thumbnail_image_width = round($this->thumbnail_image_height * $aspectratio);
2578			} elseif ($this->thumbnail_image_width < $this->thumbnail_width) {
2579				$this->thumbnail_image_width = $this->thumbnail_width;
2580				$this->thumbnail_image_height  = round($this->thumbnail_image_width / $aspectratio);
2581			}
2582
2583		}
2584		return true;
2585	}
2586
2587
2588	public function OffsiteDomainIsAllowed($hostname, $allowed_domains) {
2589		static $domain_is_allowed = array();
2590		$hostname = strtolower($hostname);
2591		if (!isset($domain_is_allowed[$hostname])) {
2592			$domain_is_allowed[$hostname] = false;
2593			foreach ($allowed_domains as $valid_domain) {
2594				$starpos = strpos($valid_domain, '*');
2595				if ($starpos !== false) {
2596					$valid_domain = substr($valid_domain, $starpos + 1);
2597					if (preg_match('#'.preg_quote($valid_domain).'$#', $hostname)) {
2598						$domain_is_allowed[$hostname] = true;
2599						break;
2600					}
2601				} else {
2602					if (strtolower($valid_domain) === $hostname) {
2603						$domain_is_allowed[$hostname] = true;
2604						break;
2605					}
2606				}
2607			}
2608		}
2609		return $domain_is_allowed[$hostname];
2610	}
2611
2612
2613	public function AntiOffsiteLinking() {
2614		// Optional anti-offsite hijacking of the thumbnail script
2615		$allow   = true;
2616		if ($allow && $this->config_nooffsitelink_enabled && (@$_SERVER['HTTP_REFERER'] || $this->config_nooffsitelink_require_refer)) {
2617			$this->DebugMessage('AntiOffsiteLinking() checking $_SERVER[HTTP_REFERER] "'.@$_SERVER['HTTP_REFERER'].'"', __FILE__, __LINE__);
2618			foreach ($this->config_nooffsitelink_valid_domains as $key => $valid_domain) {
2619				// $_SERVER['HTTP_HOST'] contains the port number, so strip it out here to make default configuration work
2620				list($clean_domain) = explode(':', $valid_domain);
2621				$this->config_nooffsitelink_valid_domains[$key] = $clean_domain;
2622			}
2623			$parsed_url = phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
2624			if (!$this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nooffsitelink_valid_domains)) {
2625				$allow   = false;
2626				//$this->DebugMessage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is NOT in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__);
2627				$this->ErrorImage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is NOT in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')');
2628			} else {
2629				$this->DebugMessage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__);
2630			}
2631		}
2632
2633		if ($allow && $this->config_nohotlink_enabled && preg_match('#^(f|ht)tps?\://#i', $this->src)) {
2634			$parsed_url = phpthumb_functions::ParseURLbetter($this->src);
2635			//if (!phpthumb_functions::CaseInsensitiveInArray(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) {
2636			if (!$this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) {
2637				// This domain is not allowed
2638				$allow = false;
2639				$this->DebugMessage('AntiOffsiteLinking() - "'.$parsed_url['host'].'" is NOT in $this->config_nohotlink_valid_domains ('.implode(';', $this->config_nohotlink_valid_domains).')', __FILE__, __LINE__);
2640			} else {
2641				$this->DebugMessage('AntiOffsiteLinking() - "'.$parsed_url['host'].'" is in $this->config_nohotlink_valid_domains ('.implode(';', $this->config_nohotlink_valid_domains).')', __FILE__, __LINE__);
2642			}
2643		}
2644
2645		if ($allow) {
2646			$this->DebugMessage('AntiOffsiteLinking() says this is allowed', __FILE__, __LINE__);
2647			return true;
2648		}
2649
2650		if (!phpthumb_functions::IsHexColor($this->config_error_bgcolor)) {
2651			return $this->ErrorImage('Invalid hex color string "'.$this->config_error_bgcolor.'" for $this->config_error_bgcolor');
2652		}
2653		if (!phpthumb_functions::IsHexColor($this->config_error_textcolor)) {
2654			return $this->ErrorImage('Invalid hex color string "'.$this->config_error_textcolor.'" for $this->config_error_textcolor');
2655		}
2656		if ($this->config_nooffsitelink_erase_image) {
2657
2658			return $this->ErrorImage($this->config_nooffsitelink_text_message, $this->thumbnail_width, $this->thumbnail_height);
2659
2660		} else {
2661
2662			$this->config_nooffsitelink_watermark_src = $this->ResolveFilenameToAbsolute($this->config_nooffsitelink_watermark_src);
2663			if (is_file($this->config_nooffsitelink_watermark_src)) {
2664
2665				if (!include_once __DIR__ .'/phpthumb.filters.php' ) {
2666					$this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying watermark', __FILE__, __LINE__);
2667					return false;
2668				}
2669				$watermark_img = $this->ImageCreateFromStringReplacement(file_get_contents($this->config_nooffsitelink_watermark_src));
2670				$phpthumbFilters = new phpthumb_filters();
2671				$phpthumbFilters->phpThumbObject = &$this;
2672				$opacity = 50;
2673				$margin  = 5;
2674				$phpthumbFilters->WatermarkOverlay($this->gdimg_output, $watermark_img, '*', $opacity, $margin);
2675				imagedestroy($watermark_img);
2676				unset($phpthumbFilters);
2677
2678			} else {
2679
2680				$nohotlink_text_array = explode("\n", wordwrap($this->config_nooffsitelink_text_message, floor($this->thumbnail_width / imagefontwidth($this->config_error_fontsize)), "\n"));
2681				$nohotlink_text_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_error_textcolor);
2682
2683				$topoffset = round(($this->thumbnail_height - (count($nohotlink_text_array) * imagefontheight($this->config_error_fontsize))) / 2);
2684
2685				$rowcounter = 0;
2686				$this->DebugMessage('AntiOffsiteLinking() writing '.count($nohotlink_text_array).' lines of text "'.$this->config_nooffsitelink_text_message.'" (in #'.$this->config_error_textcolor.') on top of image', __FILE__, __LINE__);
2687				foreach ($nohotlink_text_array as $textline) {
2688					$leftoffset = max(0, round(($this->thumbnail_width - (strlen($textline) * imagefontwidth($this->config_error_fontsize))) / 2));
2689					imagestring($this->gdimg_output, $this->config_error_fontsize, $leftoffset, $topoffset + ($rowcounter++ * imagefontheight($this->config_error_fontsize)), $textline, $nohotlink_text_color);
2690				}
2691
2692			}
2693
2694		}
2695		return true;
2696	}
2697
2698
2699	public function AlphaChannelFlatten() {
2700		if (!$this->is_alpha) {
2701			// image doesn't have alpha transparency, no need to flatten
2702			$this->DebugMessage('skipping AlphaChannelFlatten() because !$this->is_alpha', __FILE__, __LINE__);
2703			return false;
2704		}
2705		switch ($this->thumbnailFormat) {
2706			case 'png':
2707			case 'webp':
2708			case 'avif':
2709			case 'ico':
2710				// image has alpha transparency, but output as PNG, WEBP, AVIF, ICO which can handle it
2711				$this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "'.$this->thumbnailFormat.'")', __FILE__, __LINE__);
2712				return false;
2713				break;
2714
2715			case 'gif':
2716				// image has alpha transparency, but output as GIF which can handle only single-color transparency
2717				$CurrentImageColorTransparent = imagecolortransparent($this->gdimg_output);
2718				if ($CurrentImageColorTransparent == -1) {
2719					// no transparent color defined
2720
2721					if (phpthumb_functions::gd_version() < 2.0) {
2722						$this->DebugMessage('AlphaChannelFlatten() failed because GD version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2723						return false;
2724					}
2725
2726					if ($img_alpha_mixdown_dither = @imagecreatetruecolor(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) {
2727
2728						$dither_color = array();
2729						for ($i = 0; $i <= 255; $i++) {
2730							$dither_color[$i] = imagecolorallocate($img_alpha_mixdown_dither, $i, $i, $i);
2731						}
2732
2733						// scan through current truecolor image copy alpha channel to temp image as grayscale
2734						for ($x = 0; $x < $this->thumbnail_width; $x++) {
2735							for ($y = 0; $y < $this->thumbnail_height; $y++) {
2736								$PixelColor = phpthumb_functions::GetPixelColor($this->gdimg_output, $x, $y);
2737								imagesetpixel($img_alpha_mixdown_dither, $x, $y, $dither_color[ $PixelColor[ 'alpha'] * 2 ]);
2738							}
2739						}
2740
2741						// dither alpha channel grayscale version down to 2 colors
2742						imagetruecolortopalette($img_alpha_mixdown_dither, true, 2);
2743
2744						// reduce color palette to 256-1 colors (leave one palette position for transparent color)
2745						imagetruecolortopalette($this->gdimg_output, true, 255);
2746
2747						// allocate a new color for transparent color index
2748						$TransparentColor = imagecolorallocate($this->gdimg_output, 1, 254, 253);
2749						imagecolortransparent($this->gdimg_output, $TransparentColor);
2750
2751						// scan through alpha channel image and note pixels with >50% transparency
2752						for ($x = 0; $x < $this->thumbnail_width; $x++) {
2753							for ($y = 0; $y < $this->thumbnail_height; $y++) {
2754								$AlphaChannelPixel = phpthumb_functions::GetPixelColor($img_alpha_mixdown_dither, $x, $y);
2755								if ($AlphaChannelPixel['red'] > 127) {
2756									imagesetpixel($this->gdimg_output, $x, $y, $TransparentColor);
2757								}
2758							}
2759						}
2760						imagedestroy($img_alpha_mixdown_dither);
2761
2762						$this->DebugMessage('AlphaChannelFlatten() set image to 255+1 colors with transparency for GIF output', __FILE__, __LINE__);
2763						return true;
2764
2765					} else {
2766						$this->DebugMessage('AlphaChannelFlatten() failed imagecreate('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).')', __FILE__, __LINE__);
2767						return false;
2768					}
2769
2770				} else {
2771					// a single transparent color already defined, leave as-is
2772					$this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "'.$this->thumbnailFormat.'") and imagecolortransparent() returned "'.$CurrentImageColorTransparent.'"', __FILE__, __LINE__);
2773					return true;
2774				}
2775				break;
2776		}
2777		$this->DebugMessage('continuing AlphaChannelFlatten() for output format "'.$this->thumbnailFormat.'"', __FILE__, __LINE__);
2778		// image has alpha transparency, and is being output in a format that doesn't support it -- flatten
2779		if ($gdimg_flatten_temp = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height)) {
2780
2781			$this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
2782			if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
2783				return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
2784			}
2785			$background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor);
2786			imagefilledrectangle($gdimg_flatten_temp, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color);
2787			imagecopy($gdimg_flatten_temp, $this->gdimg_output, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height);
2788
2789			imagealphablending($this->gdimg_output, true);
2790			imagesavealpha($this->gdimg_output, false);
2791			imagecolortransparent($this->gdimg_output, -1);
2792			imagecopy($this->gdimg_output, $gdimg_flatten_temp, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height);
2793
2794			imagedestroy($gdimg_flatten_temp);
2795			return true;
2796
2797		} else {
2798			$this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
2799		}
2800		return false;
2801	}
2802
2803
2804	public function ApplyFilters() {
2805		if ($this->fltr && is_array($this->fltr)) {
2806			if (!include_once __DIR__ .'/phpthumb.filters.php' ) {
2807				$this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying filters ('.implode(';', $this->fltr).')', __FILE__, __LINE__);
2808				return false;
2809			}
2810			$phpthumbFilters = new phpthumb_filters();
2811			$phpthumbFilters->phpThumbObject = &$this;
2812			foreach ($this->fltr as $filtercommand) {
2813				@list($command, $parameter) = explode('|', $filtercommand, 2);
2814				$this->DebugMessage('Attempting to process filter command "'.$command.'('.$parameter.')"', __FILE__, __LINE__);
2815				switch ($command) {
2816					case 'brit': // Brightness
2817						$phpthumbFilters->Brightness($this->gdimg_output, $parameter);
2818						break;
2819
2820					case 'cont': // Contrast
2821						$phpthumbFilters->Contrast($this->gdimg_output, $parameter);
2822						break;
2823
2824					case 'ds': // Desaturation
2825						$phpthumbFilters->Desaturate($this->gdimg_output, $parameter, '');
2826						break;
2827
2828					case 'sat': // Saturation
2829						$phpthumbFilters->Saturation($this->gdimg_output, $parameter, '');
2830						break;
2831
2832					case 'gray': // Grayscale
2833						$phpthumbFilters->Grayscale($this->gdimg_output);
2834						break;
2835
2836					case 'clr': // Colorize
2837						if (phpthumb_functions::gd_version() < 2) {
2838							$this->DebugMessage('Skipping Colorize() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2839							break;
2840						}
2841						@list($amount, $color) = explode('|', $parameter, 2);
2842						$phpthumbFilters->Colorize($this->gdimg_output, $amount, $color);
2843						break;
2844
2845					case 'sep': // Sepia
2846						if (phpthumb_functions::gd_version() < 2) {
2847							$this->DebugMessage('Skipping Sepia() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2848							break;
2849						}
2850						@list($amount, $color) = explode('|', $parameter, 2);
2851						$phpthumbFilters->Sepia($this->gdimg_output, $amount, $color);
2852						break;
2853
2854					case 'gam': // Gamma correction
2855						$phpthumbFilters->Gamma($this->gdimg_output, $parameter);
2856						break;
2857
2858					case 'neg': // Negative colors
2859						$phpthumbFilters->Negative($this->gdimg_output);
2860						break;
2861
2862					case 'th': // Threshold
2863						$phpthumbFilters->Threshold($this->gdimg_output, $parameter);
2864						break;
2865
2866					case 'rcd': // ReduceColorDepth
2867						if (phpthumb_functions::gd_version() < 2) {
2868							$this->DebugMessage('Skipping ReduceColorDepth() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2869							break;
2870						}
2871						@list($colors, $dither) = explode('|', $parameter, 2);
2872						$colors = ($colors                ?  (int) $colors : 256);
2873						$dither  = ((strlen($dither) > 0) ? (bool) $dither : true);
2874						$phpthumbFilters->ReduceColorDepth($this->gdimg_output, $colors, $dither);
2875						break;
2876
2877					case 'flip': // Flip
2878						$phpthumbFilters->Flip($this->gdimg_output, strpos(strtolower($parameter), 'x') !== false, strpos(strtolower($parameter), 'y') !== false);
2879						break;
2880
2881					case 'edge': // EdgeDetect
2882						$phpthumbFilters->EdgeDetect($this->gdimg_output);
2883						break;
2884
2885					case 'emb': // Emboss
2886						$phpthumbFilters->Emboss($this->gdimg_output);
2887						break;
2888
2889					case 'bvl': // Bevel
2890						@list($width, $color1, $color2) = explode('|', $parameter, 3);
2891						$phpthumbFilters->Bevel($this->gdimg_output, $width, $color1, $color2);
2892						break;
2893
2894					case 'lvl': // autoLevels
2895						@list($band, $method, $threshold) = explode('|', $parameter, 3);
2896						$band      = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*');
2897						$method    = ((strlen($method) > 0)    ? (int) $method :   2);
2898						$threshold = ((strlen($threshold) > 0) ? (float) $threshold : 0.1);
2899
2900						$phpthumbFilters->HistogramStretch($this->gdimg_output, $band, $method, $threshold);
2901						break;
2902
2903					case 'wb': // WhiteBalance
2904						$phpthumbFilters->WhiteBalance($this->gdimg_output, $parameter);
2905						break;
2906
2907					case 'hist': // Histogram overlay
2908						if (phpthumb_functions::gd_version() < 2) {
2909							$this->DebugMessage('Skipping HistogramOverlay() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2910							break;
2911						}
2912						@list($bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y) = explode('|', $parameter, 8);
2913						$bands     = ($bands     ? $bands     :  '*');
2914						$colors    = ($colors    ? $colors    :   '');
2915						$width     = ($width     ? $width     : 0.25);
2916						$height    = ($height    ? $height    : 0.25);
2917						$alignment = ($alignment ? $alignment : 'BR');
2918						$opacity   = ($opacity   ? $opacity   :   50);
2919						$margin_x  = ($margin_x  ? $margin_x  :    5);
2920						// $margin_y -- it wasn't forgotten, let the value always pass unchanged
2921						$phpthumbFilters->HistogramOverlay($this->gdimg_output, $bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y);
2922						break;
2923
2924					case 'fram': // Frame
2925						@list($frame_width, $edge_width, $color_frame, $color1, $color2) = explode('|', $parameter, 5);
2926						$phpthumbFilters->Frame($this->gdimg_output, $frame_width, $edge_width, $color_frame, $color1, $color2);
2927						break;
2928
2929					case 'drop': // DropShadow
2930						if (phpthumb_functions::gd_version() < 2) {
2931							$this->DebugMessage('Skipping DropShadow() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2932							return false;
2933						}
2934						$this->is_alpha = true;
2935						@list($distance, $width, $color, $angle, $fade) = explode('|', $parameter, 5);
2936						$phpthumbFilters->DropShadow($this->gdimg_output, $distance, $width, $color, $angle, $fade);
2937						break;
2938
2939					case 'mask': // Mask cropping
2940						if (phpthumb_functions::gd_version() < 2) {
2941							$this->DebugMessage('Skipping Mask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2942							return false;
2943						}
2944						@list($mask_filename, $invert) = explode('|', $parameter, 2);
2945						$mask_filename = $this->ResolveFilenameToAbsolute($mask_filename);
2946						if (@is_readable($mask_filename) && ($fp_mask = @fopen($mask_filename, 'rb'))) {
2947							$MaskImageData = '';
2948							do {
2949								$buffer = fread($fp_mask, 8192);
2950								$MaskImageData .= $buffer;
2951							} while (strlen($buffer) > 0);
2952							fclose($fp_mask);
2953							if ($gdimg_mask = $this->ImageCreateFromStringReplacement($MaskImageData)) {
2954								if ($invert && phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
2955									imagefilter($gdimg_mask, IMG_FILTER_NEGATE);
2956								}
2957								$this->is_alpha = true;
2958								$phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output);
2959								imagedestroy($gdimg_mask);
2960							} else {
2961								$this->DebugMessage('ImageCreateFromStringReplacement() failed for "'.$mask_filename.'"', __FILE__, __LINE__);
2962							}
2963						} else {
2964							$this->DebugMessage('Cannot open mask file "'.$mask_filename.'"', __FILE__, __LINE__);
2965						}
2966						break;
2967
2968					case 'elip': // Ellipse cropping
2969						if (phpthumb_functions::gd_version() < 2) {
2970							$this->DebugMessage('Skipping Ellipse() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2971							return false;
2972						}
2973						$this->is_alpha = true;
2974						$phpthumbFilters->Ellipse($this->gdimg_output);
2975						break;
2976
2977					case 'ric': // RoundedImageCorners
2978						if (phpthumb_functions::gd_version() < 2) {
2979							$this->DebugMessage('Skipping RoundedImageCorners() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
2980							return false;
2981						}
2982						@list($radius_x, $radius_y) = explode('|', $parameter, 2);
2983						if (($radius_x < 1) || ($radius_y < 1)) {
2984							$this->DebugMessage('Skipping RoundedImageCorners('.$radius_x.', '.$radius_y.') because x/y radius is less than 1', __FILE__, __LINE__);
2985							break;
2986						}
2987						$this->is_alpha = true;
2988						$phpthumbFilters->RoundedImageCorners($this->gdimg_output, $radius_x, $radius_y);
2989						break;
2990
2991					case 'crop': // Crop
2992						@list($left, $right, $top, $bottom) = explode('|', $parameter, 4);
2993						$phpthumbFilters->Crop($this->gdimg_output, $left, $right, $top, $bottom);
2994						break;
2995
2996					case 'bord': // Border
2997						@list($border_width, $radius_x, $radius_y, $hexcolor_border) = explode('|', $parameter, 4);
2998						$this->is_alpha = true;
2999						$phpthumbFilters->ImageBorder($this->gdimg_output, $border_width, $radius_x, $radius_y, $hexcolor_border);
3000						break;
3001
3002					case 'over': // Overlay
3003						@list($filename, $underlay, $margin, $opacity) = explode('|', $parameter, 4);
3004						$underlay = (bool) ($underlay              ? $underlay : false);
3005						$margin   =        ((strlen($margin)  > 0) ? $margin   : ($underlay ? 0.1 : 0.0));
3006						$opacity  =        ((strlen($opacity) > 0) ? $opacity  : 100);
3007						if (($margin > 0) && ($margin < 1)) {
3008							$margin = min(0.499, $margin);
3009						} elseif (($margin > -1) && ($margin < 0)) {
3010							$margin = max(-0.499, $margin);
3011						}
3012
3013						$filename = $this->ResolveFilenameToAbsolute($filename);
3014						if (@is_readable($filename) && ($fp_watermark = @fopen($filename, 'rb'))) {
3015							$WatermarkImageData = '';
3016							do {
3017								$buffer = fread($fp_watermark, 8192);
3018								$WatermarkImageData .= $buffer;
3019							} while (strlen($buffer) > 0);
3020							fclose($fp_watermark);
3021							if ($img_watermark = $this->ImageCreateFromStringReplacement($WatermarkImageData)) {
3022								if (($margin > 0) && ($margin < 1)) {
3023									$resized_x = max(1, imagesx($this->gdimg_output) - round(2 * (imagesx($this->gdimg_output) * $margin)));
3024									$resized_y = max(1, imagesy($this->gdimg_output) - round(2 * (imagesy($this->gdimg_output) * $margin)));
3025								} else {
3026									$resized_x = max(1, imagesx($this->gdimg_output) - round(2 * $margin));
3027									$resized_y = max(1, imagesy($this->gdimg_output) - round(2 * $margin));
3028								}
3029
3030								if ($underlay) {
3031
3032									if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) {
3033										imagealphablending($img_watermark_resized, false);
3034										imagesavealpha($img_watermark_resized, true);
3035										$this->ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark_resized), imagesy($img_watermark_resized), imagesx($img_watermark), imagesy($img_watermark));
3036										if ($img_source_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
3037											imagealphablending($img_source_resized, false);
3038											imagesavealpha($img_source_resized, true);
3039											$this->ImageResizeFunction($img_source_resized, $this->gdimg_output, 0, 0, 0, 0, imagesx($img_source_resized), imagesy($img_source_resized), imagesx($this->gdimg_output), imagesy($this->gdimg_output));
3040											$phpthumbFilters->WatermarkOverlay($img_watermark_resized, $img_source_resized, 'C', $opacity, $margin);
3041											imagecopy($this->gdimg_output, $img_watermark_resized, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output));
3042										} else {
3043											$this->DebugMessage('phpthumb_functions::ImageCreateFunction('.$resized_x.', '.$resized_y.')', __FILE__, __LINE__);
3044										}
3045										imagedestroy($img_watermark_resized);
3046									} else {
3047										$this->DebugMessage('phpthumb_functions::ImageCreateFunction('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).')', __FILE__, __LINE__);
3048									}
3049
3050								} else { // overlay
3051
3052									if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
3053										imagealphablending($img_watermark_resized, false);
3054										imagesavealpha($img_watermark_resized, true);
3055										$this->ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark_resized), imagesy($img_watermark_resized), imagesx($img_watermark), imagesy($img_watermark));
3056										$phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark_resized, 'C', $opacity, $margin);
3057										imagedestroy($img_watermark_resized);
3058									} else {
3059										$this->DebugMessage('phpthumb_functions::ImageCreateFunction('.$resized_x.', '.$resized_y.')', __FILE__, __LINE__);
3060									}
3061
3062								}
3063								imagedestroy($img_watermark);
3064
3065							} else {
3066								$this->DebugMessage('ImageCreateFromStringReplacement() failed for "'.$filename.'"', __FILE__, __LINE__);
3067							}
3068						} else {
3069							$this->DebugMessage('Cannot open overlay file "'.$filename.'"', __FILE__, __LINE__);
3070						}
3071						break;
3072
3073					case 'wmi': // WaterMarkImage
3074						@list($filename, $alignment, $opacity, $margin['x'], $margin['y'], $rotate_angle) = explode('|', $parameter, 6);
3075						// $margin can be pixel margin or percent margin if $alignment is text, or max width/height if $alignment is position like "50x75"
3076						$alignment    = ($alignment            ? $alignment            : 'BR');
3077						$opacity      = ('' != $opacity ? (int) $opacity : 50);
3078						$rotate_angle = ('' != $rotate_angle ? (int) $rotate_angle : 0);
3079						if (!preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
3080							$margins = array('x', 'y');
3081							foreach ($margins as $xy) {
3082								$margin[$xy] = ('' !== $margin[ $xy ] ? $margin[ $xy] : 5);
3083								if (($margin[$xy] > 0) && ($margin[$xy] < 1)) {
3084									$margin[$xy] = min(0.499, $margin[$xy]);
3085								} elseif (($margin[$xy] > -1) && ($margin[$xy] < 0)) {
3086									$margin[$xy] = max(-0.499, $margin[$xy]);
3087								}
3088							}
3089						}
3090
3091						$filename = $this->ResolveFilenameToAbsolute($filename);
3092						if (@is_readable($filename)) {
3093							if ($img_watermark = $this->ImageCreateFromFilename($filename)) {
3094								if ($rotate_angle !== 0) {
3095									$phpthumbFilters->ImprovedImageRotate($img_watermark, $rotate_angle, 'FFFFFF', null, $this);
3096								}
3097								if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
3098									$watermark_max_width  = (int) ($margin[ 'x'] ? $margin[ 'x'] : imagesx($img_watermark));
3099									$watermark_max_height = (int) ($margin[ 'y'] ? $margin[ 'y'] : imagesy($img_watermark));
3100									$scale = phpthumb_functions::ScaleToFitInBox(imagesx($img_watermark), imagesy($img_watermark), $watermark_max_width, $watermark_max_height, true, true);
3101									$this->DebugMessage('Scaling watermark by a factor of '.number_format($scale, 4), __FILE__, __LINE__);
3102									if (($scale > 1) || ($scale < 1)) {
3103										if ($img_watermark2 = phpthumb_functions::ImageCreateFunction($scale * imagesx($img_watermark), $scale * imagesy($img_watermark))) {
3104											imagealphablending($img_watermark2, false);
3105											imagesavealpha($img_watermark2, true);
3106											$this->ImageResizeFunction($img_watermark2, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark2), imagesy($img_watermark2), imagesx($img_watermark), imagesy($img_watermark));
3107											$img_watermark = $img_watermark2;
3108										} else {
3109											$this->DebugMessage('ImageCreateFunction('.($scale * imagesx($img_watermark)).', '.($scale * imagesx($img_watermark)).') failed', __FILE__, __LINE__);
3110										}
3111									}
3112									$watermark_dest_x = round($matches[1] - (imagesx($img_watermark) / 2));
3113									$watermark_dest_y = round($matches[2] - (imagesy($img_watermark) / 2));
3114									$alignment = $watermark_dest_x.'x'.$watermark_dest_y;
3115								}
3116								$phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark, $alignment, $opacity, $margin['x'], $margin['y']);
3117								imagedestroy($img_watermark);
3118								if (isset($img_watermark2) && (is_resource($img_watermark2) || (is_object($img_watermark2) && $img_watermark2 instanceOf \GdImage))) {
3119									imagedestroy($img_watermark2);
3120								}
3121							} else {
3122								$this->DebugMessage('ImageCreateFromFilename() failed for "'.$filename.'"', __FILE__, __LINE__);
3123							}
3124						} else {
3125							$this->DebugMessage('!is_readable('.$filename.')', __FILE__, __LINE__);
3126						}
3127						break;
3128
3129					case 'wmt': // WaterMarkText
3130						@list($text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend, $lineheight) = explode('|', $parameter, 12);
3131						$text       = ($text            ? $text       : '');
3132						$size       = ($size            ? $size       : 3);
3133						$alignment  = ($alignment       ? $alignment  : 'BR');
3134						$hex_color  = ($hex_color       ? $hex_color  : '000000');
3135						$ttffont    = ($ttffont         ? $ttffont    : '');
3136						$opacity    = ('' != $opacity ? $opacity    : 50);
3137						$margin     = ('' != $margin ? $margin     : 5);
3138						$angle      = ('' != $angle ? $angle      : 0);
3139						$bg_color   = ($bg_color        ? $bg_color   : false);
3140						$bg_opacity = ($bg_opacity      ? $bg_opacity : 0);
3141						$fillextend = ($fillextend      ? $fillextend : '');
3142						$lineheight = ($lineheight      ? $lineheight : 1.0);
3143
3144						if (basename($ttffont) == $ttffont) {
3145							$ttffont = $this->realPathSafe($this->config_ttf_directory.DIRECTORY_SEPARATOR.$ttffont);
3146						} else {
3147							$ttffont = $this->ResolveFilenameToAbsolute($ttffont);
3148						}
3149						$phpthumbFilters->WatermarkText($this->gdimg_output, $text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend, $lineheight);
3150						break;
3151
3152					case 'blur': // Blur
3153						@list($radius) = explode('|', $parameter, 1);
3154						$radius = ($radius ? $radius : 1);
3155						if (phpthumb_functions::gd_version() >= 2) {
3156							$phpthumbFilters->Blur($this->gdimg_output, $radius);
3157						} else {
3158							$this->DebugMessage('Skipping Blur() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
3159						}
3160						break;
3161
3162					case 'gblr': // Gaussian Blur
3163						$phpthumbFilters->BlurGaussian($this->gdimg_output);
3164						break;
3165
3166					case 'sblr': // Selective Blur
3167						$phpthumbFilters->BlurSelective($this->gdimg_output);
3168						break;
3169
3170					case 'mean': // MeanRemoval blur
3171						$phpthumbFilters->MeanRemoval($this->gdimg_output);
3172						break;
3173
3174					case 'smth': // Smooth blur
3175						$phpthumbFilters->Smooth($this->gdimg_output, $parameter);
3176						break;
3177
3178					case 'usm': // UnSharpMask sharpening
3179						@list($amount, $radius, $threshold) = explode('|', $parameter, 3);
3180						$amount    = ($amount            ? $amount    : 80);
3181						$radius    = ($radius            ? $radius    : 0.5);
3182						$threshold = ('' !== $threshold ? $threshold : 3);
3183						if (phpthumb_functions::gd_version() >= 2.0) {
3184							ob_start();
3185							if (!@include_once __DIR__ .'/phpthumb.unsharp.php' ) {
3186								$include_error = ob_get_contents();
3187								if ($include_error) {
3188									$this->DebugMessage('include_once("'. __DIR__ .'/phpthumb.unsharp.php") generated message: "'.$include_error.'"', __FILE__, __LINE__);
3189								}
3190								$this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.unsharp.php" which is required for unsharp masking', __FILE__, __LINE__);
3191								ob_end_clean();
3192								return false;
3193							}
3194							ob_end_clean();
3195							phpUnsharpMask::applyUnsharpMask($this->gdimg_output, $amount, $radius, $threshold);
3196						} else {
3197							$this->DebugMessage('Skipping unsharp mask because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
3198							return false;
3199						}
3200						break;
3201
3202					case 'size': // Resize
3203						@list($newwidth, $newheight, $stretch) = explode('|', $parameter);
3204						$newwidth  = (!$newwidth  ? imagesx($this->gdimg_output) : ((($newwidth  > 0) && ($newwidth  < 1)) ? round($newwidth  * imagesx($this->gdimg_output)) : round($newwidth)));
3205						$newheight = (!$newheight ? imagesy($this->gdimg_output) : ((($newheight > 0) && ($newheight < 1)) ? round($newheight * imagesy($this->gdimg_output)) : round($newheight)));
3206						$stretch   = ($stretch ? true : false);
3207						if ($stretch) {
3208							$scale_x = phpthumb_functions::ScaleToFitInBox(imagesx($this->gdimg_output), imagesx($this->gdimg_output), $newwidth,  $newwidth,  true, true);
3209							$scale_y = phpthumb_functions::ScaleToFitInBox(imagesy($this->gdimg_output), imagesy($this->gdimg_output), $newheight, $newheight, true, true);
3210						} else {
3211							$scale_x = phpthumb_functions::ScaleToFitInBox(imagesx($this->gdimg_output), imagesy($this->gdimg_output), $newwidth, $newheight, true, true);
3212							$scale_y = $scale_x;
3213						}
3214						$this->DebugMessage('Scaling watermark ('.($stretch ? 'with' : 'without').' stretch) by a factor of "'.number_format($scale_x, 4).' x '.number_format($scale_y, 4).'"', __FILE__, __LINE__);
3215						if (($scale_x > 1) || ($scale_x < 1) || ($scale_y > 1) || ($scale_y < 1)) {
3216							if ($img_temp = phpthumb_functions::ImageCreateFunction(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) {
3217								imagecopy($img_temp, $this->gdimg_output, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output));
3218								if ($this->gdimg_output = phpthumb_functions::ImageCreateFunction($scale_x * imagesx($img_temp), $scale_y * imagesy($img_temp))) {
3219									imagealphablending($this->gdimg_output, false);
3220									imagesavealpha($this->gdimg_output, true);
3221									$this->ImageResizeFunction($this->gdimg_output, $img_temp, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output), imagesx($img_temp), imagesy($img_temp));
3222								} else {
3223									$this->DebugMessage('ImageCreateFunction('.($scale_x * imagesx($img_temp)).', '.($scale_y * imagesy($img_temp)).') failed', __FILE__, __LINE__);
3224								}
3225								imagedestroy($img_temp);
3226							} else {
3227								$this->DebugMessage('ImageCreateFunction('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).') failed', __FILE__, __LINE__);
3228							}
3229						}
3230						break;
3231
3232					case 'rot': // ROTate
3233						@list($angle, $bgcolor) = explode('|', $parameter, 2);
3234						$phpthumbFilters->ImprovedImageRotate($this->gdimg_output, $angle, $bgcolor, null, $this);
3235						break;
3236
3237					case 'stc': // Source Transparent Color
3238						@list($hexcolor, $min_limit, $max_limit) = explode('|', $parameter, 3);
3239						if (!phpthumb_functions::IsHexColor($hexcolor)) {
3240							$this->DebugMessage('Skipping SourceTransparentColor hex color is invalid ('.$hexcolor.')', __FILE__, __LINE__);
3241							return false;
3242						}
3243						$min_limit = ('' !== $min_limit ? $min_limit :  5);
3244						$max_limit = ('' !== $max_limit ? $max_limit : 10);
3245						if ($gdimg_mask = $phpthumbFilters->SourceTransparentColorMask($this->gdimg_output, $hexcolor, $min_limit, $max_limit)) {
3246							$this->is_alpha = true;
3247							$phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output);
3248							imagedestroy($gdimg_mask);
3249						} else {
3250							$this->DebugMessage('SourceTransparentColorMask() failed for "'.$hexcolor.','.$min_limit.','.$max_limit.'"', __FILE__, __LINE__);
3251						}
3252						break;
3253				}
3254				$this->DebugMessage('Finished processing filter command "'.$command.'('.$parameter.')"', __FILE__, __LINE__);
3255			}
3256		}
3257		return true;
3258	}
3259
3260
3261	public function MaxFileSize() {
3262		if (phpthumb_functions::gd_version() < 2) {
3263			$this->DebugMessage('Skipping MaxFileSize() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
3264			return false;
3265		}
3266		if ($this->maxb > 0) {
3267			switch ($this->thumbnailFormat) {
3268				case 'png':
3269				case 'gif':
3270					$imgRenderFunction = 'image'.$this->thumbnailFormat;
3271
3272					ob_start();
3273					$imgRenderFunction($this->gdimg_output);
3274					$imgdata = ob_get_contents();
3275					ob_end_clean();
3276
3277					if (strlen($imgdata) > $this->maxb) {
3278						for ($i = 8; $i >= 1; $i--) {
3279							$tempIMG = imagecreatetruecolor(imagesx($this->gdimg_output), imagesy($this->gdimg_output));
3280							imagecopy($tempIMG, $this->gdimg_output, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output));
3281							imagetruecolortopalette($tempIMG, true, pow(2, $i));
3282							ob_start();
3283							$imgRenderFunction($tempIMG);
3284							$imgdata = ob_get_contents();
3285							ob_end_clean();
3286
3287							if (strlen($imgdata) <= $this->maxb) {
3288								imagetruecolortopalette($this->gdimg_output, true, pow(2, $i));
3289								break;
3290							}
3291						}
3292					}
3293					break;
3294
3295				case 'jpeg':
3296					ob_start();
3297					imagejpeg($this->gdimg_output);
3298					$imgdata = ob_get_contents();
3299					ob_end_clean();
3300
3301					if (strlen($imgdata) > $this->maxb) {
3302						for ($i = 3; $i < 20; $i++) {
3303							$q = round(100 * (1 - log10($i / 2)));
3304							ob_start();
3305							imagejpeg($this->gdimg_output, null, $q);
3306							$imgdata = ob_get_contents();
3307							ob_end_clean();
3308
3309							$this->thumbnailQuality = $q;
3310							if (strlen($imgdata) <= $this->maxb) {
3311								break;
3312							}
3313						}
3314					}
3315					if (strlen($imgdata) > $this->maxb) {
3316						return false;
3317					}
3318					break;
3319
3320				default:
3321					return false;
3322			}
3323		}
3324		return true;
3325	}
3326
3327
3328	public function CalculateThumbnailDimensions() {
3329		$this->DebugMessage('CalculateThumbnailDimensions() starting with [W,H,sx,sy,sw,sh] initially set to ['.$this->source_width.','.$this->source_height.','.$this->sx.','.$this->sy.','.$this->sw.','.$this->sh.']', __FILE__, __LINE__);
3330//echo $this->source_width.'x'.$this->source_height.'<hr>';
3331		$this->thumbnailCropX = ($this->sx ? (($this->sx >= 2) ? $this->sx : round($this->sx * $this->source_width))  : 0);
3332//echo $this->thumbnailCropX.'<br>';
3333		$this->thumbnailCropY = ($this->sy ? (($this->sy >= 2) ? $this->sy : round($this->sy * $this->source_height)) : 0);
3334//echo $this->thumbnailCropY.'<br>';
3335		$this->thumbnailCropW = ($this->sw ? (($this->sw >= 2) ? $this->sw : round($this->sw * $this->source_width))  : $this->source_width);
3336//echo $this->thumbnailCropW.'<br>';
3337		$this->thumbnailCropH = ($this->sh ? (($this->sh >= 2) ? $this->sh : round($this->sh * $this->source_height)) : $this->source_height);
3338//echo $this->thumbnailCropH.'<hr>';
3339
3340		// limit source area to original image area
3341		$this->thumbnailCropW = max(1, min($this->thumbnailCropW, $this->source_width  - $this->thumbnailCropX));
3342		$this->thumbnailCropH = max(1, min($this->thumbnailCropH, $this->source_height - $this->thumbnailCropY));
3343
3344		$this->DebugMessage('CalculateThumbnailDimensions() starting with [x,y,w,h] initially set to ['.$this->thumbnailCropX.','.$this->thumbnailCropY.','.$this->thumbnailCropW.','.$this->thumbnailCropH.']', __FILE__, __LINE__);
3345
3346
3347		if ($this->zc && $this->w && $this->h) {
3348			// Zoom Crop
3349			// retain proportional resizing we did above, but crop off larger dimension so smaller
3350			// dimension fully fits available space
3351
3352			$scaling_X = $this->source_width  / $this->w;
3353			$scaling_Y = $this->source_height / $this->h;
3354			if ($scaling_X > $scaling_Y) {
3355				// some of the width will need to be cropped
3356				$allowable_width = $this->source_width / $scaling_X * $scaling_Y;
3357				$this->thumbnailCropW = round($allowable_width);
3358				$this->thumbnailCropX = round(($this->source_width - $allowable_width) / 2);
3359
3360			} elseif ($scaling_Y > $scaling_X) {
3361				// some of the height will need to be cropped
3362				$allowable_height = $this->source_height / $scaling_Y * $scaling_X;
3363				$this->thumbnailCropH = round($allowable_height);
3364				$this->thumbnailCropY = round(($this->source_height - $allowable_height) / 2);
3365
3366			} else {
3367				// image fits perfectly, no cropping needed
3368			}
3369			$this->thumbnail_width  = $this->w;
3370			$this->thumbnail_height = $this->h;
3371			$this->thumbnail_image_width  = $this->thumbnail_width;
3372			$this->thumbnail_image_height = $this->thumbnail_height;
3373
3374		} elseif ($this->iar && $this->w && $this->h) {
3375
3376			// Ignore Aspect Ratio
3377			// stretch image to fit exactly 'w' x 'h'
3378			$this->thumbnail_width  = $this->w;
3379			$this->thumbnail_height = $this->h;
3380			$this->thumbnail_image_width  = $this->thumbnail_width;
3381			$this->thumbnail_image_height = $this->thumbnail_height;
3382
3383		} else {
3384
3385			$original_aspect_ratio = $this->thumbnailCropW / $this->thumbnailCropH;
3386			if ($this->aoe) {
3387				if ($this->w && $this->h) {
3388					$maxwidth  = min($this->w, $this->h * $original_aspect_ratio);
3389					$maxheight = min($this->h, $this->w / $original_aspect_ratio);
3390				} elseif ($this->w) {
3391					$maxwidth  = $this->w;
3392					$maxheight = $this->w / $original_aspect_ratio;
3393				} elseif ($this->h) {
3394					$maxwidth  = $this->h * $original_aspect_ratio;
3395					$maxheight = $this->h;
3396				} else {
3397					$maxwidth  = $this->thumbnailCropW;
3398					$maxheight = $this->thumbnailCropH;
3399				}
3400			} else {
3401				$maxwidth  = phpthumb_functions::nonempty_min($this->w, $this->thumbnailCropW, $this->config_output_maxwidth);
3402				$maxheight = phpthumb_functions::nonempty_min($this->h, $this->thumbnailCropH, $this->config_output_maxheight);
3403//echo $maxwidth.'x'.$maxheight.'<br>';
3404				$maxwidth  = min($maxwidth, $maxheight * $original_aspect_ratio);
3405				$maxheight = min($maxheight, $maxwidth / $original_aspect_ratio);
3406//echo $maxwidth.'x'.$maxheight.'<hr>';
3407			}
3408
3409			$this->thumbnail_image_width  = $maxwidth;
3410			$this->thumbnail_image_height = $maxheight;
3411			$this->thumbnail_width  = $maxwidth;
3412			$this->thumbnail_height = $maxheight;
3413
3414			$this->FixedAspectRatio();
3415		}
3416
3417		$this->thumbnail_width  = max(1, floor($this->thumbnail_width));
3418		$this->thumbnail_height = max(1, floor($this->thumbnail_height));
3419		return true;
3420	}
3421
3422
3423	public function CreateGDoutput() {
3424		$this->CalculateThumbnailDimensions();
3425
3426		// create the GD image (either true-color or 256-color, depending on GD version)
3427		$this->gdimg_output = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height);
3428
3429		// images that have transparency must have the background filled with the configured 'bg' color otherwise the transparent color will appear as black
3430		imagesavealpha($this->gdimg_output, true);
3431		if ($this->is_alpha && phpthumb_functions::gd_version() >= 2) {
3432
3433			imagealphablending($this->gdimg_output, false);
3434			$output_full_alpha = phpthumb_functions::ImageColorAllocateAlphaSafe($this->gdimg_output, 255, 255, 255, 127);
3435			imagefilledrectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $output_full_alpha);
3436
3437		} else {
3438
3439			$current_transparent_color = imagecolortransparent($this->gdimg_source);
3440			if ($this->bg || (@$current_transparent_color >= 0)) {
3441
3442				$this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
3443				if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
3444					return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
3445				}
3446				$background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor);
3447				imagefilledrectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color);
3448
3449			}
3450
3451		}
3452		$this->DebugMessage('CreateGDoutput() returning canvas "'.$this->thumbnail_width.'x'.$this->thumbnail_height.'"', __FILE__, __LINE__);
3453		return true;
3454	}
3455
3456	public function SetOrientationDependantWidthHeight() {
3457		$this->DebugMessage('SetOrientationDependantWidthHeight() starting with "'.$this->source_width.'"x"'.$this->source_height.'"', __FILE__, __LINE__);
3458		if ($this->source_height > $this->source_width) {
3459			// portrait
3460			$this->w = phpthumb_functions::OneOfThese($this->wp, $this->w, $this->ws, $this->wl);
3461			$this->h = phpthumb_functions::OneOfThese($this->hp, $this->h, $this->hs, $this->hl);
3462		} elseif ($this->source_height < $this->source_width) {
3463			// landscape
3464			$this->w = phpthumb_functions::OneOfThese($this->wl, $this->w, $this->ws, $this->wp);
3465			$this->h = phpthumb_functions::OneOfThese($this->hl, $this->h, $this->hs, $this->hp);
3466		} else {
3467			// square
3468			$this->w = phpthumb_functions::OneOfThese($this->ws, $this->w, $this->wl, $this->wp);
3469			$this->h = phpthumb_functions::OneOfThese($this->hs, $this->h, $this->hl, $this->hp);
3470		}
3471		//$this->w = round($this->w ? $this->w : (($this->h && $this->source_height) ? $this->h * $this->source_width  / $this->source_height : $this->w));
3472		//$this->h = round($this->h ? $this->h : (($this->w && $this->source_width)  ? $this->w * $this->source_height / $this->source_width  : $this->h));
3473		$this->DebugMessage('SetOrientationDependantWidthHeight() setting w="'. (int) $this->w .'", h="'. (int) $this->h .'"', __FILE__, __LINE__);
3474		return true;
3475	}
3476
3477	public function ExtractEXIFgetImageSize() {
3478		$this->DebugMessage('starting ExtractEXIFgetImageSize()', __FILE__, __LINE__);
3479
3480		if (preg_match('#^http:#i', $this->src) && !$this->sourceFilename && $this->rawImageData) {
3481			$this->SourceDataToTempFile();
3482		}
3483		if (null === $this->getimagesizeinfo) {
3484			if ($this->sourceFilename) {
3485				if ($this->getimagesizeinfo = @getimagesize($this->sourceFilename)) {
3486				$this->source_width  = $this->getimagesizeinfo[0];
3487				$this->source_height = $this->getimagesizeinfo[1];
3488				$this->DebugMessage('getimagesize('.$this->sourceFilename.') says image is '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__);
3489				} else {
3490					$this->DebugMessage('getimagesize('.$this->sourceFilename.') failed', __FILE__, __LINE__);
3491				}
3492			} else {
3493				$this->DebugMessage('skipping getimagesize() because $this->sourceFilename is empty', __FILE__, __LINE__);
3494			}
3495		} else {
3496			$this->DebugMessage('skipping getimagesize() because !is_null($this->getimagesizeinfo)', __FILE__, __LINE__);
3497		}
3498
3499		if (is_resource($this->gdimg_source) || (is_object($this->gdimg_source) && $this->gdimg_source instanceOf \GdImage)) {
3500
3501			$this->source_width  = imagesx($this->gdimg_source);
3502			$this->source_height = imagesy($this->gdimg_source);
3503
3504			$this->SetOrientationDependantWidthHeight();
3505
3506		} elseif ($this->rawImageData && !$this->sourceFilename) {
3507
3508			if ($this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
3509				$this->DebugMessage('NOT bypassing EXIF and getimagesize sections because source image is too large for GD ('.$this->source_width.'x'.$this->source_width.'='.($this->source_width * $this->source_height * 5).'MB)', __FILE__, __LINE__);
3510			} else {
3511				$this->DebugMessage('bypassing EXIF and getimagesize sections because $this->rawImageData is set, and $this->sourceFilename is not set, and source image is not too large for GD ('.$this->source_width.'x'.$this->source_width.'='.($this->source_width * $this->source_height * 5).'MB)', __FILE__, __LINE__);
3512			}
3513
3514		}
3515
3516		if (!empty($this->getimagesizeinfo)) {
3517			// great
3518			$this->getimagesizeinfo['filesize'] = @filesize($this->sourceFilename);
3519		} elseif (!$this->rawImageData) {
3520			$this->DebugMessage('getimagesize("'.$this->sourceFilename.'") failed', __FILE__, __LINE__);
3521		}
3522
3523		if ($this->config_prefer_imagemagick) {
3524			if ($this->ImageMagickThumbnailToGD()) {
3525				return true;
3526			}
3527			$this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
3528		}
3529
3530		if (isset($this->getimagesizeinfo[1])) {
3531		$this->source_width  = $this->getimagesizeinfo[0];
3532		$this->source_height = $this->getimagesizeinfo[1];
3533		}
3534
3535		$this->SetOrientationDependantWidthHeight();
3536
3537		if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.2.0', '>=') && function_exists('exif_read_data')) {
3538			switch (@$this->getimagesizeinfo[2]) {
3539				case IMAGETYPE_JPEG:
3540				case IMAGETYPE_TIFF_II:
3541				case IMAGETYPE_TIFF_MM:
3542					$this->exif_raw_data = @exif_read_data($this->sourceFilename, 0, true);
3543					break;
3544			}
3545		}
3546		if (function_exists('exif_thumbnail') && (@$this->getimagesizeinfo[2] == IMAGETYPE_JPEG)) {
3547			// Extract EXIF info from JPEGs
3548
3549			$this->exif_thumbnail_width  = '';
3550			$this->exif_thumbnail_height = '';
3551			$this->exif_thumbnail_type   = '';
3552
3553			// The parameters width, height and imagetype are available since PHP v4.3.0
3554			if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.3.0', '>=')) {
3555
3556				$this->exif_thumbnail_data = @exif_thumbnail($this->sourceFilename, $this->exif_thumbnail_width, $this->exif_thumbnail_height, $this->exif_thumbnail_type);
3557
3558			} else {
3559
3560				// older versions of exif_thumbnail output an error message but NOT return false on failure
3561				ob_start();
3562				$this->exif_thumbnail_data = exif_thumbnail($this->sourceFilename);
3563				$exit_thumbnail_error = ob_get_contents();
3564				ob_end_clean();
3565				if (!$exit_thumbnail_error && $this->exif_thumbnail_data) {
3566
3567					if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
3568						$this->exif_thumbnail_width  = imagesx($gdimg_exif_temp);
3569						$this->exif_thumbnail_height = imagesy($gdimg_exif_temp);
3570						$this->exif_thumbnail_type   = 2; // (2 == JPEG) before PHP v4.3.0 only JPEG format EXIF thumbnails are returned
3571						unset($gdimg_exif_temp);
3572					} else {
3573						return $this->ErrorImage('Failed - $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data) in '.__FILE__.' on line '.__LINE__);
3574					}
3575
3576				}
3577
3578			}
3579
3580		} elseif (!function_exists('exif_thumbnail')) {
3581
3582			$this->DebugMessage('exif_thumbnail() does not exist, cannot extract EXIF thumbnail', __FILE__, __LINE__);
3583
3584		}
3585
3586		$this->DebugMessage('EXIF thumbnail extraction: (size='.(!empty($this->exif_thumbnail_data) ? strlen((string) $this->exif_thumbnail_data) : 0).'; type="'.$this->exif_thumbnail_type.'"; '. (int) $this->exif_thumbnail_width .'x'. (int) $this->exif_thumbnail_height .')', __FILE__, __LINE__);
3587
3588		// see if EXIF thumbnail can be used directly with no processing
3589		if ($this->config_use_exif_thumbnail_for_speed && $this->exif_thumbnail_data) {
3590			while (true) {
3591				if (!$this->xto) {
3592					$source_ar = $this->source_width / $this->source_height;
3593					$exif_ar = $this->exif_thumbnail_width / $this->exif_thumbnail_height;
3594					if (number_format($source_ar, 2) != number_format($exif_ar, 2)) {
3595						$this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar ('.$source_ar.' != '.$exif_ar.')', __FILE__, __LINE__);
3596						break;
3597					}
3598					if ($this->w && ($this->w != $this->exif_thumbnail_width)) {
3599						$this->DebugMessage('not using EXIF thumbnail because $this->w != $this->exif_thumbnail_width ('.$this->w.' != '.$this->exif_thumbnail_width.')', __FILE__, __LINE__);
3600						break;
3601					}
3602					if ($this->h && ($this->h != $this->exif_thumbnail_height)) {
3603						$this->DebugMessage('not using EXIF thumbnail because $this->h != $this->exif_thumbnail_height ('.$this->h.' != '.$this->exif_thumbnail_height.')', __FILE__, __LINE__);
3604						break;
3605					}
3606					$CannotBeSetParameters = array('sx', 'sy', 'sh', 'sw', 'far', 'bg', 'bc', 'fltr', 'phpThumbDebug');
3607					foreach ($CannotBeSetParameters as $parameter) {
3608						if ($this->$parameter) {
3609							break 2;
3610						}
3611					}
3612				}
3613
3614				$this->DebugMessage('setting $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data)', __FILE__, __LINE__);
3615				$this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data);
3616				$this->source_width  = imagesx($this->gdimg_source);
3617				$this->source_height = imagesy($this->gdimg_source);
3618				return true;
3619			}
3620		}
3621
3622		if (($this->config_max_source_pixels > 0) && (($this->source_width * $this->source_height) > $this->config_max_source_pixels)) {
3623
3624			// Source image is larger than would fit in available PHP memory.
3625			// If ImageMagick is installed, use it to generate the thumbnail.
3626			// Else, if an EXIF thumbnail is available, use that as the source image.
3627			// Otherwise, no choice but to fail with an error message
3628			$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__);
3629			if (!$this->config_prefer_imagemagick && $this->ImageMagickThumbnailToGD()) {
3630				// excellent, we have a thumbnailed source image
3631				return true;
3632			}
3633
3634		}
3635		return true;
3636	}
3637
3638
3639	public function SetCacheFilename() {
3640		if (null !== $this->cache_filename) {
3641			$this->DebugMessage('$this->cache_filename already set, skipping SetCacheFilename()', __FILE__, __LINE__);
3642			return true;
3643		}
3644		if (null === $this->config_cache_directory) {
3645			$this->setCacheDirectory();
3646			if (!$this->config_cache_directory) {
3647				$this->DebugMessage('SetCacheFilename() failed because $this->config_cache_directory is empty', __FILE__, __LINE__);
3648				return false;
3649			}
3650		}
3651		$this->setOutputFormat();
3652
3653		if (!$this->sourceFilename && !$this->rawImageData && $this->src) {
3654			$this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
3655		}
3656
3657		if ($this->config_cache_default_only_suffix && $this->sourceFilename) {
3658			// simplified cache filenames:
3659			// only use default parameters in phpThumb.config.php
3660			// substitute source filename into * in $this->config_cache_default_only_suffix
3661			// (eg: '*_thumb' becomes 'picture_thumb.jpg')
3662			if (strpos($this->config_cache_default_only_suffix, '*') === false) {
3663				$this->DebugMessage('aborting simplified caching filename because no * in "'.$this->config_cache_default_only_suffix.'"', __FILE__, __LINE__);
3664			} else {
3665				preg_match('#(.+)(\\.[a-z0-9]+)?$#i', basename($this->sourceFilename), $matches);
3666				$this->cache_filename = $this->config_cache_directory.DIRECTORY_SEPARATOR.rawurlencode(str_replace('*', @$matches[1], $this->config_cache_default_only_suffix)).'.'.strtolower($this->thumbnailFormat);
3667				return true;
3668			}
3669		}
3670
3671		$this->cache_filename = '';
3672		if ($this->new) {
3673			$broad_directory_name = strtolower(md5($this->new));
3674			$this->cache_filename .= '_new'.$broad_directory_name;
3675		} elseif ($this->md5s) {
3676			// source image MD5 hash provided
3677			$this->DebugMessage('SetCacheFilename() _raw set from $this->md5s = "'.$this->md5s.'"', __FILE__, __LINE__);
3678			$broad_directory_name = $this->md5s;
3679			$this->cache_filename .= '_raw'.$this->md5s;
3680		} elseif (!$this->src && $this->rawImageData) {
3681			$this->DebugMessage('SetCacheFilename() _raw set from md5($this->rawImageData) = "'.md5($this->rawImageData).'"', __FILE__, __LINE__);
3682			$broad_directory_name = strtolower(md5($this->rawImageData));
3683			$this->cache_filename .= '_raw'.$broad_directory_name;
3684		} else {
3685			$this->DebugMessage('SetCacheFilename() _src set from md5($this->sourceFilename) "'.$this->sourceFilename.'" = "'.md5($this->sourceFilename).'"', __FILE__, __LINE__);
3686			$broad_directory_name = strtolower(md5($this->sourceFilename));
3687			$this->cache_filename .= '_src'.$broad_directory_name;
3688		}
3689		if (!empty($_SERVER['HTTP_REFERER']) && $this->config_nooffsitelink_enabled) {
3690			$parsed_url1 = @phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
3691			$parsed_url2 = @phpthumb_functions::ParseURLbetter('http://'.@$_SERVER['HTTP_HOST']);
3692			if (@$parsed_url1['host'] && @$parsed_url2['host'] && ($parsed_url1['host'] != $parsed_url2['host'])) {
3693				// include "_offsite" only if nooffsitelink_enabled and if referrer doesn't match the domain of the current server
3694				$this->cache_filename .= '_offsite';
3695			}
3696		}
3697
3698		$ParametersString = '';
3699		if ($this->fltr && is_array($this->fltr)) {
3700			$ParametersString .= '_fltr'.implode('_fltr', $this->fltr);
3701		}
3702		$FilenameParameters1 = array('ar', 'bg', 'bc', 'far', 'sx', 'sy', 'sw', 'sh', 'zc');
3703		foreach ($FilenameParameters1 as $key) {
3704			if ($this->$key) {
3705				$ParametersString .= '_'.$key.$this->$key;
3706			}
3707		}
3708		$FilenameParameters2 = array('h', 'w', 'wl', 'wp', 'ws', 'hp', 'hs', 'xto', 'ra', 'iar', 'aoe', 'maxb', 'sfn', 'dpi');
3709		foreach ($FilenameParameters2 as $key) {
3710			if ($this->$key) {
3711				$ParametersString .= '_'.$key. (int) $this->$key;
3712			}
3713		}
3714		$FilenameParameters3 = array('ica');
3715		foreach ($FilenameParameters3 as $key) {
3716			if ($this->$key) {
3717				$ParametersString .= '_'.$key.substr(md5($this->$key), 0, 4);
3718			}
3719		}
3720		if ($this->thumbnailFormat == 'jpeg') {
3721			// only JPEG output has variable quality option
3722			$ParametersString .= '_q'. (int) $this->thumbnailQuality;
3723		}
3724		$this->DebugMessage('SetCacheFilename() _par set from md5('.$ParametersString.')', __FILE__, __LINE__);
3725		$this->cache_filename .= '_par'.strtolower(md5($ParametersString));
3726
3727		if ($this->md5s) {
3728			// source image MD5 hash provided
3729			// do not source image modification date --
3730			// cached image will be used even if file was modified or removed
3731		} elseif (!$this->config_cache_source_filemtime_ignore_remote && preg_match('#^(f|ht)tps?\://#i', $this->src)) {
3732			$this->cache_filename .= '_dat'. (int) phpthumb_functions::filedate_remote($this->src);
3733		} elseif (!$this->config_cache_source_filemtime_ignore_local && $this->src && !$this->rawImageData) {
3734			$this->cache_filename .= '_dat'. (int) (@filemtime($this->sourceFilename));
3735		}
3736
3737		$this->cache_filename .= '.'.strtolower($this->thumbnailFormat);
3738		$broad_directories = '';
3739		for ($i = 0; $i < $this->config_cache_directory_depth; $i++) {
3740			$broad_directories .= DIRECTORY_SEPARATOR.substr($broad_directory_name, 0, $i + 1);
3741		}
3742
3743		$this->cache_filename = $this->config_cache_directory.$broad_directories.DIRECTORY_SEPARATOR.$this->config_cache_prefix.rawurlencode($this->cache_filename);
3744		return true;
3745	}
3746
3747
3748	public function SourceImageIsTooLarge($width, $height) {
3749		if (!$this->config_max_source_pixels) {
3750			return false;
3751		}
3752		if ($this->php_memory_limit && function_exists('memory_get_usage')) {
3753			$available_memory = $this->php_memory_limit - memory_get_usage();
3754			return (bool) (($width * $height * 5) > $available_memory);
3755		}
3756		return (bool) (($width * $height) > $this->config_max_source_pixels);
3757	}
3758
3759	public function ImageCreateFromFilename($filename) {
3760		// try to create GD image source directly via GD, if possible,
3761		// rather than buffering to memory and creating with imagecreatefromstring
3762		$ImageCreateWasAttempted = false;
3763		$gd_image = false;
3764
3765		$this->DebugMessage('starting ImageCreateFromFilename('.$filename.')', __FILE__, __LINE__);
3766		if ($filename && ($getimagesizeinfo = @getimagesize($filename))) {
3767			if (!$this->SourceImageIsTooLarge($getimagesizeinfo[0], $getimagesizeinfo[1])) {
3768				$ImageCreateFromFunction = array(
3769					IMAGETYPE_GIF  => 'imagecreatefromgif',
3770					IMAGETYPE_JPEG => 'imagecreatefromjpeg',
3771					IMAGETYPE_PNG  => 'imagecreatefrompng',
3772					IMAGETYPE_WBMP => 'imagecreatefromwbmp',
3773					IMAGETYPE_WEBP => 'imagecreatefromwebp',
3774					IMAGETYPE_AVIF => 'imagecreatefromavif',
3775				);
3776				$this->DebugMessage('ImageCreateFromFilename found ($getimagesizeinfo[2]=='.@$getimagesizeinfo[2].')', __FILE__, __LINE__);
3777				switch (@$getimagesizeinfo[2]) {
3778					case IMAGETYPE_GIF:
3779					case IMAGETYPE_JPEG:
3780					case IMAGETYPE_PNG:
3781					case IMAGETYPE_WBMP:
3782					case IMAGETYPE_WEBP:
3783					case IMAGETYPE_AVIF:
3784						$ImageCreateFromFunctionName = $ImageCreateFromFunction[$getimagesizeinfo[2]];
3785						if (function_exists($ImageCreateFromFunctionName)) {
3786							$this->DebugMessage('Calling '.$ImageCreateFromFunctionName.'('.$filename.')', __FILE__, __LINE__);
3787							$ImageCreateWasAttempted = true;
3788							$gd_image = $ImageCreateFromFunctionName($filename);
3789						} else {
3790							$this->DebugMessage('NOT calling '.$ImageCreateFromFunctionName.'('.$filename.') because !function_exists('.$ImageCreateFromFunctionName.')', __FILE__, __LINE__);
3791						}
3792						break;
3793
3794					case IMAGETYPE_SWF:
3795					case IMAGETYPE_PSD:
3796					case IMAGETYPE_BMP:
3797					case IMAGETYPE_TIFF_II:
3798					case IMAGETYPE_TIFF_MM:
3799					case IMAGETYPE_JPC:
3800					case IMAGETYPE_JP2:
3801					case IMAGETYPE_JPX:
3802					case IMAGETYPE_JB2:
3803					case IMAGETYPE_SWC:
3804					case IMAGETYPE_IFF:
3805					case IMAGETYPE_XBM:
3806					case IMAGETYPE_ICO:
3807						$this->DebugMessage('No built-in image creation function for image type "'.@$getimagesizeinfo[2].'" ($getimagesizeinfo[2])', __FILE__, __LINE__);
3808						break;
3809
3810					default:
3811						$this->DebugMessage('Unknown value for $getimagesizeinfo[2]: "'.@$getimagesizeinfo[2].'"', __FILE__, __LINE__);
3812						break;
3813				}
3814			} else {
3815				$this->DebugMessage('image is '.$getimagesizeinfo[0].'x'.$getimagesizeinfo[1].' and therefore contains more pixels ('.($getimagesizeinfo[0] * $getimagesizeinfo[1]).') than $this->config_max_source_pixels setting ('.$this->config_max_source_pixels.')', __FILE__, __LINE__);
3816				return false;
3817			}
3818		} else {
3819			$this->DebugMessage('empty $filename or getimagesize('.$filename.') failed', __FILE__, __LINE__);
3820		}
3821
3822		if (!$gd_image) {
3823			// cannot create from filename, attempt to create source image with imagecreatefromstring, if possible
3824			if ($ImageCreateWasAttempted) {
3825				$this->DebugMessage($ImageCreateFromFunctionName.'() was attempted but FAILED', __FILE__, __LINE__);
3826			}
3827			$this->DebugMessage('Populating $rawimagedata', __FILE__, __LINE__);
3828			$rawimagedata = '';
3829			if ($fp = @fopen($filename, 'rb')) {
3830				$filesize = filesize($filename);
3831				$blocksize = 8192;
3832				$blockreads = ceil($filesize / $blocksize);
3833				for ($i = 0; $i < $blockreads; $i++) {
3834					$rawimagedata .= fread($fp, $blocksize);
3835				}
3836				fclose($fp);
3837			} else {
3838				$this->DebugMessage('cannot fopen('.$filename.')', __FILE__, __LINE__);
3839			}
3840			if ($rawimagedata) {
3841				$this->DebugMessage('attempting ImageCreateFromStringReplacement($rawimagedata ('.strlen($rawimagedata).' bytes), true)', __FILE__, __LINE__);
3842				$gd_image = $this->ImageCreateFromStringReplacement($rawimagedata, true);
3843			}
3844		}
3845		return $gd_image;
3846	}
3847
3848	public function SourceImageToGD() {
3849		if (is_resource($this->gdimg_source) || (is_object($this->gdimg_source) && $this->gdimg_source instanceOf \GdImage)) {
3850			$this->source_width  = imagesx($this->gdimg_source);
3851			$this->source_height = imagesy($this->gdimg_source);
3852			$this->DebugMessage('skipping SourceImageToGD() because $this->gdimg_source is already a resource ('.$this->source_width.'x'.$this->source_height.')', __FILE__, __LINE__);
3853			return true;
3854		}
3855		$this->DebugMessage('starting SourceImageToGD()', __FILE__, __LINE__);
3856
3857		if ($this->config_prefer_imagemagick) {
3858			if (empty($this->sourceFilename) && !empty($this->rawImageData)) {
3859				$this->DebugMessage('Copying raw image data to temp file and trying again with ImageMagick', __FILE__, __LINE__);
3860				if ($tempnam = $this->phpThumb_tempnam()) {
3861					if (file_put_contents($tempnam, $this->rawImageData)) {
3862						$this->sourceFilename = $tempnam;
3863						if ($this->ImageMagickThumbnailToGD()) {
3864							// excellent, we have a thumbnailed source image
3865							$this->DebugMessage('ImageMagickThumbnailToGD() succeeded', __FILE__, __LINE__);
3866						} else {
3867							$this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
3868						}
3869						@chmod($tempnam, $this->getParameter('config_file_create_mask'));
3870					} else {
3871						$this->DebugMessage('failed to put $this->rawImageData into temp file "'.$tempnam.'"', __FILE__, __LINE__);
3872					}
3873				} else {
3874					$this->DebugMessage('failed to generate temp file name', __FILE__, __LINE__);
3875				}
3876			}
3877		}
3878		if (!$this->gdimg_source && $this->rawImageData) {
3879
3880			if ($this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
3881				$memory_get_usage = (function_exists('memory_get_usage') ? memory_get_usage() : 0);
3882				return $this->ErrorImage('Source image is too large ('.$this->source_width.'x'.$this->source_height.' = '.number_format($this->source_width * $this->source_height / 1000000, 1).'Mpx, max='.number_format($this->config_max_source_pixels / 1000000, 1).'Mpx) for GD creation (either install ImageMagick or increase PHP memory_limit to at least '.ceil(($memory_get_usage + (5 * $this->source_width * $this->source_height)) / 1048576).'M).');
3883			}
3884			if ($this->md5s && ($this->md5s != md5($this->rawImageData))) {
3885				return $this->ErrorImage('$this->md5s != md5($this->rawImageData)'."\n".'"'.$this->md5s.'" != '."\n".'"'.md5($this->rawImageData).'"');
3886			}
3887			//if ($this->issafemode) {
3888			//	return $this->ErrorImage('Cannot generate thumbnails from raw image data when PHP SAFE_MODE enabled');
3889			//}
3890			$this->gdimg_source = $this->ImageCreateFromStringReplacement($this->rawImageData);
3891			if (!$this->gdimg_source) {
3892				if (substr($this->rawImageData, 0, 2) === 'BM') {
3893					$this->getimagesizeinfo[2] = 6; // BMP
3894				} elseif (substr($this->rawImageData, 0, 4) === 'II'."\x2A\x00") {
3895					$this->getimagesizeinfo[2] = 7; // TIFF (littlendian)
3896				} elseif (substr($this->rawImageData, 0, 4) === 'MM'."\x00\x2A") {
3897					$this->getimagesizeinfo[2] = 8; // TIFF (bigendian)
3898				}
3899				$this->DebugMessage('SourceImageToGD.ImageCreateFromStringReplacement() failed with unknown image type "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).')', __FILE__, __LINE__);
3900//				return $this->ErrorImage('Unknown image type identified by "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).') in SourceImageToGD()['.__LINE__.']');
3901			}
3902
3903		} elseif (!$this->gdimg_source && $this->sourceFilename) {
3904
3905			if ($this->md5s && ($this->md5s != phpthumb_functions::md5_file_safe($this->sourceFilename))) {
3906				return $this->ErrorImage('$this->md5s != md5(sourceFilename)'."\n".'"'.$this->md5s.'" != '."\n".'"'.phpthumb_functions::md5_file_safe($this->sourceFilename).'"');
3907			}
3908			switch (@$this->getimagesizeinfo[2]) {
3909				case 1:
3910				case 3:
3911					// GIF or PNG input file may have transparency
3912					$this->is_alpha = true;
3913					break;
3914			}
3915			if (!$this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
3916				$this->gdimg_source = $this->ImageCreateFromFilename($this->sourceFilename);
3917			}
3918
3919		}
3920
3921		while (true) {
3922			if ($this->gdimg_source) {
3923				$this->DebugMessage('Not using EXIF thumbnail data because $this->gdimg_source is already set', __FILE__, __LINE__);
3924				break;
3925			}
3926			if (!$this->exif_thumbnail_data) {
3927				$this->DebugMessage('Not using EXIF thumbnail data because $this->exif_thumbnail_data is empty', __FILE__, __LINE__);
3928				break;
3929			}
3930			if (ini_get('safe_mode')) {
3931				if (!$this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
3932					$this->DebugMessage('Using EXIF thumbnail data because source image too large and safe_mode enabled', __FILE__, __LINE__);
3933					$this->aoe = true;
3934				} else {
3935					break;
3936				}
3937			} else {
3938				if (!$this->config_use_exif_thumbnail_for_speed) {
3939					$this->DebugMessage('Not using EXIF thumbnail data because $this->config_use_exif_thumbnail_for_speed is FALSE', __FILE__, __LINE__);
3940					break;
3941				}
3942				if (($this->thumbnailCropX != 0) || ($this->thumbnailCropY != 0)) {
3943					$this->DebugMessage('Not using EXIF thumbnail data because source cropping is enabled ('.$this->thumbnailCropX.','.$this->thumbnailCropY.')', __FILE__, __LINE__);
3944					break;
3945				}
3946				if (($this->w > $this->exif_thumbnail_width) || ($this->h > $this->exif_thumbnail_height)) {
3947					$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__);
3948					break;
3949				}
3950				$source_ar = $this->source_width / $this->source_height;
3951				$exif_ar   = $this->exif_thumbnail_width / $this->exif_thumbnail_height;
3952				if (number_format($source_ar, 2) != number_format($exif_ar, 2)) {
3953					$this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar ('.$source_ar.' != '.$exif_ar.')', __FILE__, __LINE__);
3954					break;
3955				}
3956			}
3957
3958			// EXIF thumbnail exists, and is equal to or larger than destination thumbnail, and will be use as source image
3959			$this->DebugMessage('Trying to use EXIF thumbnail as source image', __FILE__, __LINE__);
3960
3961			if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
3962
3963				$this->DebugMessage('Successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
3964				$this->gdimg_source   = $gdimg_exif_temp;
3965				$this->source_width   = $this->exif_thumbnail_width;
3966				$this->source_height  = $this->exif_thumbnail_height;
3967				$this->thumbnailCropW = $this->source_width;
3968				$this->thumbnailCropH = $this->source_height;
3969				return true;
3970
3971			} else {
3972				$this->DebugMessage('$this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false) failed', __FILE__, __LINE__);
3973			}
3974
3975			break;
3976		}
3977
3978		if (!$this->gdimg_source) {
3979			$this->DebugMessage('$this->gdimg_source is still empty', __FILE__, __LINE__);
3980
3981			$this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
3982
3983			$imageHeader = '';
3984			$gd_info = gd_info();
3985			$GDreadSupport = false;
3986			switch (@$this->getimagesizeinfo[2]) {
3987				case 1:
3988					$imageHeader = 'Content-Type: image/gif';
3989					$GDreadSupport = (bool) @$gd_info['GIF Read Support'];
3990					break;
3991				case 2:
3992					$imageHeader = 'Content-Type: image/jpeg';
3993					$GDreadSupport = (bool) @$gd_info['JPG Support'];
3994					break;
3995				case 3:
3996					$imageHeader = 'Content-Type: image/png';
3997					$GDreadSupport = (bool) @$gd_info['PNG Support'];
3998					break;
3999			}
4000			if ($imageHeader) {
4001				// cannot create image for whatever reason (maybe imagecreatefromjpeg et al are not available?)
4002				// and ImageMagick is not available either, no choice but to output original (not resized/modified) data and exit
4003				if ($this->config_error_die_on_source_failure) {
4004					$errormessages = array();
4005					$errormessages[] = 'All attempts to create GD image source failed.';
4006					if ($this->fatalerror) {
4007						$errormessages[] = $this->fatalerror;
4008					}
4009					if ($this->issafemode) {
4010						$errormessages[] = 'Safe Mode enabled, therefore ImageMagick is unavailable. (disable Safe Mode if possible)';
4011					} elseif (!$this->ImageMagickVersion()) {
4012						$errormessages[] = 'ImageMagick is not installed (it is highly recommended that you install it).';
4013					}
4014					if ($this->SourceImageIsTooLarge($this->getimagesizeinfo[0], $this->getimagesizeinfo[1])) {
4015						$memory_get_usage = (function_exists('memory_get_usage') ? memory_get_usage() : 0);
4016						$errormessages[] = 'Source image is too large ('.$this->getimagesizeinfo[0].'x'.$this->getimagesizeinfo[1].' = '.number_format($this->getimagesizeinfo[0] * $this->getimagesizeinfo[1] / 1000000, 1).'Mpx, max='.number_format($this->config_max_source_pixels / 1000000, 1).'Mpx) for GD creation (either install ImageMagick or increase PHP memory_limit to at least '.ceil(($memory_get_usage + (5 * $this->getimagesizeinfo[0] * $this->getimagesizeinfo[1])) / 1048576).'M).';
4017					} elseif (!$GDreadSupport) {
4018						$errormessages[] = 'GD does not have read support for "'.$imageHeader.'".';
4019					} else {
4020						$errormessages[] = 'Source image probably corrupt.';
4021					}
4022					$this->ErrorImage(implode("\n", $errormessages));
4023
4024				} else {
4025					$this->DebugMessage('All attempts to create GD image source failed ('.(ini_get('safe_mode') ? 'Safe Mode enabled, ImageMagick unavailable and source image probably too large for GD': ($GDreadSupport ? 'source image probably corrupt' : 'GD does not have read support for "'.$imageHeader.'"')).'), cannot generate thumbnail');
4026					//$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__);
4027					//if (!$this->phpThumbDebug) {
4028					//	header($imageHeader);
4029					//	echo $this->rawImageData;
4030					//	exit;
4031					//}
4032					return false;
4033				}
4034			}
4035
4036			//switch (substr($this->rawImageData, 0, 2)) {
4037			//	case 'BM':
4038			switch (@$this->getimagesizeinfo[2]) {
4039				case 6:
4040					ob_start();
4041					if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
4042						ob_end_clean();
4043						return $this->ErrorImage('include_once('. __DIR__ .'/phpthumb.bmp.php) failed');
4044					}
4045					ob_end_clean();
4046					if ($fp = @fopen($this->sourceFilename, 'rb')) {
4047						$this->rawImageData = '';
4048						while (!feof($fp)) {
4049							$this->rawImageData .= fread($fp, 32768);
4050						}
4051						fclose($fp);
4052					}
4053					$phpthumb_bmp = new phpthumb_bmp();
4054					$this->gdimg_source = $phpthumb_bmp->phpthumb_bmp2gd($this->rawImageData, phpthumb_functions::gd_version() >= 2.0);
4055					unset($phpthumb_bmp);
4056					if ($this->gdimg_source) {
4057						$this->DebugMessage('$phpthumb_bmp->phpthumb_bmp2gd() succeeded', __FILE__, __LINE__);
4058					} else {
4059						return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on BMP source conversion' : 'phpthumb_bmp2gd() failed');
4060					}
4061					break;
4062			//}
4063			//switch (substr($this->rawImageData, 0, 4)) {
4064			//	case 'II'."\x2A\x00":
4065			//	case 'MM'."\x00\x2A":
4066				case 7:
4067				case 8:
4068					return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on TIFF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support TIFF source images without it');
4069					break;
4070
4071				//case "\xD7\xCD\xC6\x9A":
4072				//	return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on WMF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support WMF source images without it');
4073				//	break;
4074			}
4075
4076			if (!$this->gdimg_source) {
4077				if ($this->rawImageData) {
4078					$HeaderFourBytes = substr($this->rawImageData, 0, 4);
4079				} elseif ($this->sourceFilename) {
4080					if ($fp = @fopen($this->sourceFilename, 'rb')) {
4081						$HeaderFourBytes = fread($fp, 4);
4082						fclose($fp);
4083					} else {
4084						return $this->ErrorImage('failed to open "'.$this->sourceFilename.'" SourceImageToGD() ['.__LINE__.']');
4085					}
4086				} else {
4087					return $this->ErrorImage('Unable to create image, neither filename nor image data suppplied in SourceImageToGD() ['.__LINE__.']');
4088				}
4089				if (!$this->ImageMagickVersion() && !phpthumb_functions::gd_version()) {
4090					return $this->ErrorImage('Neither GD nor ImageMagick seem to be installed on this server. At least one (preferably GD), or better both, MUST be installed for phpThumb to work.');
4091				} elseif ($HeaderFourBytes == "\xD7\xCD\xC6\x9A") { // WMF
4092					return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on WMF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support WMF source images without it');
4093				} elseif ($HeaderFourBytes == '%PDF') { // "%PDF"
4094					return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick and GhostScript are both required for PDF source images; GhostScript may not be properly configured' : 'ImageMagick and/or GhostScript are unavailable and phpThumb() does not support PDF source images without them');
4095				} elseif (substr($HeaderFourBytes, 0, 3) == "\xFF\xD8\xFF") { // JPEG
4096					return $this->ErrorImage('Image (JPEG) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
4097				} elseif ($HeaderFourBytes == '%PNG') { // "%PNG"
4098					return $this->ErrorImage('Image (PNG) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
4099				} elseif (substr($HeaderFourBytes, 0, 3) == 'GIF') { // GIF
4100					return $this->ErrorImage('Image (GIF) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
4101				}
4102				return $this->ErrorImage('Unknown image type identified by "'.$HeaderFourBytes.'" ('.phpthumb_functions::HexCharDisplay($HeaderFourBytes).') in SourceImageToGD() ['.__LINE__.']');
4103			}
4104		}
4105
4106		if (!$this->gdimg_source) {
4107			if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
4108				$this->DebugMessage('All other attempts failed, but successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
4109				$this->gdimg_source = $gdimg_exif_temp;
4110				// override allow-enlarging setting if EXIF thumbnail is the only source available
4111				// otherwise thumbnails larger than the EXIF thumbnail will be created at EXIF size
4112				$this->aoe = true;
4113				return true;
4114			}
4115			return false;
4116		}
4117
4118		$this->source_width  = imagesx($this->gdimg_source);
4119		$this->source_height = imagesy($this->gdimg_source);
4120		return true;
4121	}
4122
4123	private function ImageCropAuto() {
4124		// ImageCropAuto
4125		if (!is_null($this->ica)) {
4126			$this->DebugMessage('ImageCropAuto('.$this->ica.') starting', __FILE__, __LINE__);
4127			if (function_exists('imagecropauto')) { // (PHP 5 >= 5.5.0, PHP 7)
4128				// https://www.php.net/manual/en/function.imagecropauto.php
4129				// 0 = IMG_CROP_DEFAULT
4130				// 1 = IMG_CROP_TRANSPARENT
4131				// 2 = IMG_CROP_BLACK
4132				// 3 = IMG_CROP_WHITE
4133				// 4 = IMG_CROP_SIDES
4134				// 5 = IMG_CROP_THRESHOLD
4135				if (preg_match('#^(([0-4])|(5)\\|(0?\\.?[0-9]+)\\|([0-9A-F]{6}))$#i', $this->ica, $matches)) {
4136					@list($dummy, $dummy, $ica_mode1, $ica_mode2, $ica_threshold, $ica_color) = $matches;
4137					if ($ica_mode2) {
4138						$param_color = hexdec($ica_color);
4139						if (!imageistruecolor($this->gdimg_source)) {
4140							$param_color = imagecolorclosest($this->gdimg_source, hexdec(substr($ica_color, 0, 2)), hexdec(substr($ica_color, 2, 2)), hexdec(substr($ica_color, 4, 2)));
4141						}
4142						$cropped = imagecropauto($this->gdimg_source, intval($ica_mode2), floatval($ica_threshold), $param_color);
4143					} else {
4144						$cropped = imagecropauto($this->gdimg_source, intval($ica_mode1));
4145					}
4146					if ($cropped !== false) {                 // in case a new image resource was returned
4147						$this->DebugMessage('ImageCropAuto changing source image size from '.imagesx($this->gdimg_source).'x'.imagesy($this->gdimg_source).' to '.imagesx($cropped).'x'.imagesy($cropped), __FILE__, __LINE__);
4148						imagedestroy($this->gdimg_source);    // we destroy the original image
4149						$this->gdimg_source = $cropped;       // and assign the cropped image to $im
4150						$this->source_width  = imagesx($this->gdimg_source);
4151						$this->source_height = imagesy($this->gdimg_source);
4152					} else {
4153						$this->DebugMessage('imagecropauto failed', __FILE__, __LINE__);
4154					}
4155				} else {
4156					$this->DebugMessage('invalid "ica" parameter syntax, ignoring', __FILE__, __LINE__);
4157				}
4158			} else {
4159				$this->DebugMessage('!function_exists(imagecropauto), ignoring "ica" parameter', __FILE__, __LINE__);
4160			}
4161		}
4162		return true;
4163	}
4164
4165	public function phpThumbDebugVarDump($var) {
4166		if (null === $var) {
4167			return 'NULL';
4168		} elseif (is_bool($var)) {
4169			return ($var ? 'TRUE' : 'FALSE');
4170		} elseif (is_string($var)) {
4171			return 'string('.strlen($var).')'.str_repeat(' ', max(0, 3 - strlen(strlen($var)))).' "'.$var.'"';
4172		} elseif (is_int($var)) {
4173			return 'integer     '.$var;
4174		} elseif (is_float($var)) {
4175			return 'float       '.$var;
4176		} elseif (is_array($var)) {
4177			ob_start();
4178			var_dump($var);
4179			$vardumpoutput = ob_get_contents();
4180			ob_end_clean();
4181			return strtr($vardumpoutput, "\n\r\t", '   ');
4182		}
4183		return gettype($var);
4184	}
4185
4186	public function phpThumbDebug($level='') {
4187		if ($level && ($this->phpThumbDebug !== $level)) {
4188			return true;
4189		}
4190		if ($this->config_disable_debug) {
4191			return $this->ErrorImage('phpThumbDebug disabled');
4192		}
4193
4194		$FunctionsExistance  = array('exif_thumbnail', 'gd_info', 'image_type_to_mime_type', 'getimagesize', 'imagecopyresampled', 'imagecopyresized', 'imagecreate', 'imagecreatefromstring', 'imagecreatetruecolor', 'imageistruecolor', 'imagerotate', 'imagetypes', 'version_compare', 'imagecreatefromgif', 'imagecreatefromjpeg', 'imagecreatefrompng', 'imagecreatefromwbmp', 'imagecreatefromxbm', 'imagecreatefromxpm', 'imagecreatefromstring', 'imagecreatefromgd', 'imagecreatefromgd2', 'imagecreatefromgd2part', 'imagejpeg', 'imagegif', 'imagepng', 'imagewbmp');
4195		$ParameterNames      = array('src', 'new', 'w', 'h', 'f', 'q', 'sx', 'sy', 'sw', 'sh', 'far', 'bg', 'bc', 'zc', 'ica', 'file', 'goto', 'err', 'xto', 'ra', 'ar', 'aoe', 'iar', 'maxb');
4196		$ConfigVariableNames = array('document_root', 'temp_directory', 'output_format', 'output_maxwidth', 'output_maxheight', 'error_message_image_default', 'error_bgcolor', 'error_textcolor', 'error_fontsize', 'error_die_on_error', 'error_silent_die_on_error', 'error_die_on_source_failure', 'nohotlink_enabled', 'nohotlink_valid_domains', 'nohotlink_erase_image', 'nohotlink_text_message', 'nooffsitelink_enabled', 'nooffsitelink_valid_domains', 'nooffsitelink_require_refer', 'nooffsitelink_erase_image', 'nooffsitelink_text_message', 'high_security_enabled', 'allow_src_above_docroot', 'allow_src_above_phpthumb', 'max_source_pixels', 'use_exif_thumbnail_for_speed', 'border_hexcolor', 'background_hexcolor', 'ttf_directory', 'disable_pathinfo_parsing', 'disable_imagecopyresampled');
4197		$OtherVariableNames  = array('phpThumbDebug', 'thumbnailQuality', 'thumbnailFormat', 'gdimg_output', 'gdimg_source', 'sourceFilename', 'source_width', 'source_height', 'thumbnailCropX', 'thumbnailCropY', 'thumbnailCropW', 'thumbnailCropH', 'exif_thumbnail_width', 'exif_thumbnail_height', 'exif_thumbnail_type', 'thumbnail_width', 'thumbnail_height', 'thumbnail_image_width', 'thumbnail_image_height');
4198
4199		$DebugOutput = array();
4200		$DebugOutput[] = 'phpThumb() version          = '.$this->phpthumb_version;
4201		$DebugOutput[] = 'phpversion()                = '.@PHP_VERSION;
4202		$DebugOutput[] = 'PHP_OS                      = '.PHP_OS;
4203		$DebugOutput[] = '$_SERVER[SERVER_SOFTWARE]   = '.@$_SERVER['SERVER_SOFTWARE'];
4204		$DebugOutput[] = '__FILE__                    = '.__FILE__;
4205		$DebugOutput[] = 'realpath(.)                 = '.@realpath('.');
4206		$DebugOutput[] = '$_SERVER[PHP_SELF]          = '.@$_SERVER['PHP_SELF'];
4207		$DebugOutput[] = '$_SERVER[HOST_NAME]         = '.@$_SERVER['HOST_NAME'];
4208		$DebugOutput[] = '$_SERVER[HTTP_REFERER]      = '.@$_SERVER['HTTP_REFERER'];
4209		$DebugOutput[] = '$_SERVER[QUERY_STRING]      = '.@$_SERVER['QUERY_STRING'];
4210		$DebugOutput[] = '$_SERVER[PATH_INFO]         = '.@$_SERVER['PATH_INFO'];
4211		$DebugOutput[] = '$_SERVER[DOCUMENT_ROOT]     = '.@$_SERVER['DOCUMENT_ROOT'];
4212		$DebugOutput[] = 'getenv(DOCUMENT_ROOT)       = '.@getenv('DOCUMENT_ROOT');
4213		$DebugOutput[] = '';
4214
4215		$DebugOutput[] = 'get_magic_quotes_gpc()         = '.(function_exists('get_magic_quotes_gpc')     ? $this->phpThumbDebugVarDump(@get_magic_quotes_gpc())     : false);
4216		$DebugOutput[] = 'get_magic_quotes_runtime()     = '.(function_exists('get_magic_quotes_runtime') ? $this->phpThumbDebugVarDump(@get_magic_quotes_runtime()) : false);
4217		$DebugOutput[] = 'error_reporting()              = '.$this->phpThumbDebugVarDump(error_reporting());
4218		$DebugOutput[] = 'ini_get(error_reporting)       = '.$this->phpThumbDebugVarDump(@ini_get('error_reporting'));
4219		$DebugOutput[] = 'ini_get(display_errors)        = '.$this->phpThumbDebugVarDump(@ini_get('display_errors'));
4220		$DebugOutput[] = 'ini_get(allow_url_fopen)       = '.$this->phpThumbDebugVarDump(@ini_get('allow_url_fopen'));
4221		$DebugOutput[] = 'ini_get(disable_functions)     = '.$this->phpThumbDebugVarDump(@ini_get('disable_functions'));
4222		$DebugOutput[] = 'get_cfg_var(disable_functions) = '.$this->phpThumbDebugVarDump(@get_cfg_var('disable_functions'));
4223		$DebugOutput[] = 'ini_get(safe_mode)             = '.$this->phpThumbDebugVarDump(@ini_get('safe_mode'));
4224		$DebugOutput[] = 'ini_get(open_basedir)          = '.$this->phpThumbDebugVarDump(@ini_get('open_basedir'));
4225		$DebugOutput[] = 'ini_get(max_execution_time)    = '.$this->phpThumbDebugVarDump(@ini_get('max_execution_time'));
4226		$DebugOutput[] = 'ini_get(memory_limit)          = '.$this->phpThumbDebugVarDump(@ini_get('memory_limit'));
4227		$DebugOutput[] = 'get_cfg_var(memory_limit)      = '.$this->phpThumbDebugVarDump(@get_cfg_var('memory_limit'));
4228		$DebugOutput[] = 'memory_get_usage()             = '.(function_exists('memory_get_usage') ? $this->phpThumbDebugVarDump(@memory_get_usage()) : 'n/a');
4229		$DebugOutput[] = '';
4230
4231		$DebugOutput[] = '$this->config_prefer_imagemagick            = '.$this->phpThumbDebugVarDump($this->config_prefer_imagemagick);
4232		$DebugOutput[] = '$this->config_imagemagick_path              = '.$this->phpThumbDebugVarDump($this->config_imagemagick_path);
4233		$DebugOutput[] = '$this->ImageMagickWhichConvert()            = '.$this->ImageMagickWhichConvert();
4234		$IMpathUsed = ($this->config_imagemagick_path ? $this->config_imagemagick_path : $this->ImageMagickWhichConvert());
4235		$DebugOutput[] = '[actual ImageMagick path used]              = '.$this->phpThumbDebugVarDump($IMpathUsed);
4236		$DebugOutput[] = 'file_exists([actual ImageMagick path used]) = '.$this->phpThumbDebugVarDump(@file_exists($IMpathUsed));
4237		$DebugOutput[] = 'ImageMagickVersion(false)                   = '.$this->ImageMagickVersion(false);
4238		$DebugOutput[] = 'ImageMagickVersion(true)                    = '.$this->ImageMagickVersion(true);
4239		$DebugOutput[] = '';
4240
4241		$DebugOutput[] = '$this->config_cache_directory               = '.$this->phpThumbDebugVarDump($this->config_cache_directory);
4242		$DebugOutput[] = '$this->config_cache_directory_depth         = '.$this->phpThumbDebugVarDump($this->config_cache_directory_depth);
4243		$DebugOutput[] = '$this->config_cache_disable_warning         = '.$this->phpThumbDebugVarDump($this->config_cache_disable_warning);
4244		$DebugOutput[] = '$this->config_cache_maxage                  = '.$this->phpThumbDebugVarDump($this->config_cache_maxage);
4245		$DebugOutput[] = '$this->config_cache_maxsize                 = '.$this->phpThumbDebugVarDump($this->config_cache_maxsize);
4246		$DebugOutput[] = '$this->config_cache_maxfiles                = '.$this->phpThumbDebugVarDump($this->config_cache_maxfiles);
4247		$DebugOutput[] = '$this->config_cache_force_passthru          = '.$this->phpThumbDebugVarDump($this->config_cache_force_passthru);
4248		$DebugOutput[] = '$this->cache_filename                       = '.$this->phpThumbDebugVarDump($this->cache_filename);
4249		$DebugOutput[] = 'is_readable($this->config_cache_directory)  = '.$this->phpThumbDebugVarDump(@is_readable($this->config_cache_directory));
4250		$DebugOutput[] = 'is_writable($this->config_cache_directory)  = '.$this->phpThumbDebugVarDump(@is_writable($this->config_cache_directory));
4251		$DebugOutput[] = 'is_readable($this->cache_filename)          = '.$this->phpThumbDebugVarDump(@is_readable($this->cache_filename));
4252		$DebugOutput[] = 'is_writable($this->cache_filename)          = '.(@file_exists($this->cache_filename) ? $this->phpThumbDebugVarDump(@is_writable($this->cache_filename)) : 'n/a');
4253		$DebugOutput[] = '';
4254
4255		foreach ($ConfigVariableNames as $varname) {
4256			$varname = 'config_'.$varname;
4257			$value = $this->$varname;
4258			$DebugOutput[] = '$this->'.str_pad($varname, 37, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
4259		}
4260		$DebugOutput[] = '';
4261		foreach ($OtherVariableNames as $varname) {
4262			$value = $this->$varname;
4263			$DebugOutput[] = '$this->'.str_pad($varname, 27, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
4264		}
4265		$DebugOutput[] = 'strlen($this->rawImageData)        = '.(!empty($this->rawImageData)        ? strlen($this->rawImageData)        : '');
4266		$DebugOutput[] = 'strlen($this->exif_thumbnail_data) = '.(!empty($this->exif_thumbnail_data) ? strlen($this->exif_thumbnail_data) : '');
4267		$DebugOutput[] = '';
4268
4269		foreach ($ParameterNames as $varname) {
4270			$value = $this->$varname;
4271			$DebugOutput[] = '$this->'.str_pad($varname, 4, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
4272		}
4273		$DebugOutput[] = '';
4274
4275		foreach ($FunctionsExistance as $functionname) {
4276			$DebugOutput[] = 'builtin_function_exists('.$functionname.')'.str_repeat(' ', 23 - strlen($functionname)).' = '.$this->phpThumbDebugVarDump(phpthumb_functions::builtin_function_exists($functionname));
4277		}
4278		$DebugOutput[] = '';
4279
4280		$gd_info = gd_info();
4281		foreach ($gd_info as $key => $value) {
4282			$DebugOutput[] = 'gd_info.'.str_pad($key, 34, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
4283		}
4284		$DebugOutput[] = '';
4285
4286		$exif_info = phpthumb_functions::exif_info();
4287		foreach ($exif_info as $key => $value) {
4288			$DebugOutput[] = 'exif_info.'.str_pad($key, 26, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
4289		}
4290		$DebugOutput[] = '';
4291
4292		if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) {
4293			foreach ($ApacheLookupURIarray as $key => $value) {
4294				$DebugOutput[] = 'ApacheLookupURIarray.'.str_pad($key, 15, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
4295			}
4296		} else {
4297				$DebugOutput[] = 'ApacheLookupURIarray() -- FAILED';
4298		}
4299		$DebugOutput[] = '';
4300
4301		if (isset($_GET) && is_array($_GET)) {
4302			foreach ($_GET as $key => $value) {
4303				$DebugOutput[] = '$_GET['.$key.']'.str_repeat(' ', 30 - strlen($key)).'= '.$this->phpThumbDebugVarDump($value);
4304			}
4305		}
4306		if (isset($_POST) && is_array($_POST)) {
4307			foreach ($_POST as $key => $value) {
4308				$DebugOutput[] = '$_POST['.$key.']'.str_repeat(' ', 29 - strlen($key)).'= '.$this->phpThumbDebugVarDump($value);
4309			}
4310		}
4311		$DebugOutput[] = '';
4312
4313		$DebugOutput[] = '$this->debugmessages:';
4314		foreach ($this->debugmessages as $errorstring) {
4315			$DebugOutput[] = '  * '.$errorstring;
4316		}
4317		$DebugOutput[] = '';
4318
4319		$DebugOutput[] = '$this->debugtiming:';
4320		foreach ($this->debugtiming as $timestamp => $timingstring) {
4321			$DebugOutput[] = '  * '.$timestamp.' '.$timingstring;
4322		}
4323		$DebugOutput[] = '  * Total processing time: '.number_format(max(array_keys($this->debugtiming)) - min(array_keys($this->debugtiming)), 6);
4324
4325		$this->f = (isset($_GET['f']) ? $_GET['f'] : $this->f); // debug modes 0-2 don't recognize text mode otherwise
4326		return $this->ErrorImage(implode("\n", $DebugOutput), 700, 500, true);
4327	}
4328
4329	public function FatalError($text) {
4330		if (null === $this->fatalerror) {
4331			$this->fatalerror = $text;
4332		}
4333		return true;
4334	}
4335
4336	public function ErrorImage($text, $width=0, $height=0, $forcedisplay=false) {
4337		$width  = ($width  ? $width  : $this->config_error_image_width);
4338		$height = ($height ? $height : $this->config_error_image_height);
4339
4340		$text = 'phpThumb() v'.$this->phpthumb_version."\n".'http://phpthumb.sourceforge.net'."\n\n".($this->config_disable_debug ? 'Error messages disabled.'."\n\n".'edit phpThumb.config.php and (temporarily) set'."\n".'$PHPTHUMB_CONFIG[\'disable_debug\'] = false;'."\n".'to view the details of this error' : $text);
4341
4342		$this->FatalError($text);
4343		$this->DebugMessage($text, __FILE__, __LINE__);
4344		$this->purgeTempFiles();
4345		if ($this->config_error_silent_die_on_error) {
4346			exit;
4347		}
4348		if ($this->phpThumbDebug && !$forcedisplay) {
4349			return false;
4350		}
4351		if (!$this->config_error_die_on_error && !$forcedisplay) {
4352			return false;
4353		}
4354		if ($this->err || $this->config_error_message_image_default) {
4355			// Show generic custom error image instead of error message
4356			// for use on production sites where you don't want debug messages
4357			if (($this->err == 'showerror') || $this->phpThumbDebug) {
4358				// fall through and actually show error message even if default error image is set
4359			} else {
4360				header('Location: '.($this->err ? $this->err : $this->config_error_message_image_default));
4361				exit;
4362			}
4363		}
4364		$this->setOutputFormat();
4365		if (!$this->thumbnailFormat || !$this->config_disable_debug || (phpthumb_functions::gd_version() < 1)) {
4366			$this->thumbnailFormat = 'text';
4367		}
4368		if (@$this->thumbnailFormat == 'text') {
4369			// bypass all GD functions and output text error message
4370			if (!headers_sent()) {
4371				header('Content-type: text/plain');
4372				echo $text;
4373			} else {
4374				echo '<pre>'.htmlspecialchars($text).'</pre>';
4375			}
4376			exit;
4377		}
4378
4379		$FontWidth  = imagefontwidth($this->config_error_fontsize);
4380		$FontHeight = imagefontheight($this->config_error_fontsize);
4381
4382		$LinesOfText = explode("\n", @wordwrap($text, floor($width / $FontWidth), "\n", true));
4383		$height = max($height, count($LinesOfText) * $FontHeight);
4384
4385		$headers_file = '';
4386		$headers_line = '';
4387		if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.3.0', '>=') && headers_sent($headers_file, $headers_line)) {
4388
4389			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>';
4390
4391		} elseif (headers_sent()) {
4392
4393			echo "\n".'**Headers already sent, dumping error message as text:**<br><pre>'."\n\n".$text."\n".'</pre>';
4394
4395		} elseif ($gdimg_error = imagecreate($width, $height)) {
4396
4397			$background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_bgcolor,   true);
4398			$text_color       = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_textcolor, true);
4399			imagefilledrectangle($gdimg_error, 0, 0, $width, $height, $background_color);
4400			$lineYoffset = 0;
4401			foreach ($LinesOfText as $line) {
4402				imagestring($gdimg_error, $this->config_error_fontsize, 2, $lineYoffset, $line, $text_color);
4403				$lineYoffset += $FontHeight;
4404			}
4405			if (function_exists('imagetypes')) {
4406				$imagetypes = imagetypes();
4407				if ($imagetypes & IMG_PNG) {
4408					header('Content-Type: image/png');
4409					imagepng($gdimg_error);
4410				} elseif ($imagetypes & IMG_GIF) {
4411					header('Content-Type: image/gif');
4412					imagegif($gdimg_error);
4413				} elseif ($imagetypes & IMG_JPG) {
4414					header('Content-Type: image/jpeg');
4415					imagejpeg($gdimg_error);
4416				} elseif ($imagetypes & IMG_WBMP) {
4417					header('Content-Type: image/vnd.wap.wbmp');
4418					imagewbmp($gdimg_error);
4419				}
4420			}
4421			imagedestroy($gdimg_error);
4422
4423		}
4424		if (!headers_sent()) {
4425			echo "\n".'**Failed to send graphical error image, dumping error message as text:**<br>'."\n\n".$text;
4426		}
4427		exit;
4428	}
4429
4430	public function ImageCreateFromStringReplacement(&$RawImageData, $DieOnErrors=false) {
4431		// there are serious bugs in the non-bundled versions of GD which may cause
4432		// PHP to segfault when calling imagecreatefromstring() - avoid if at all possible
4433		// when not using a bundled version of GD2
4434		if (!phpthumb_functions::gd_version()) {
4435			if ($DieOnErrors) {
4436				if (!headers_sent()) {
4437					// base64-encoded error image in GIF format
4438					$ERROR_NOGD = 'R0lGODlhIAAgALMAAAAAABQUFCQkJDY2NkZGRldXV2ZmZnJycoaGhpSUlKWlpbe3t8XFxdXV1eTk5P7+/iwAAAAAIAAgAAAE/vDJSau9WILtTAACUinDNijZtAHfCojS4W5H+qxD8xibIDE9h0OwWaRWDIljJSkUJYsN4bihMB8th3IToAKs1VtYM75cyV8sZ8vygtOE5yMKmGbO4jRdICQCjHdlZzwzNW4qZSQmKDaNjhUMBX4BBAlmMywFSRWEmAI6b5gAlhNxokGhooAIK5o/pi9vEw4Lfj4OLTAUpj6IabMtCwlSFw0DCKBoFqwAB04AjI54PyZ+yY3TD0ss2YcVmN/gvpcu4TOyFivWqYJlbAHPpOntvxNAACcmGHjZzAZqzSzcq5fNjxFmAFw9iFRunD1epU6tsIPmFCAJnWYE0FURk7wJDA0MTKpEzoWAAskiAAA7';
4439					header('Content-Type: image/gif');
4440					echo base64_decode($ERROR_NOGD);
4441				} else {
4442					echo '*** ERROR: No PHP-GD support available ***';
4443				}
4444				exit;
4445			} else {
4446				$this->DebugMessage('ImageCreateFromStringReplacement() failed: gd_version says "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
4447				return false;
4448			}
4449		}
4450		if (phpthumb_functions::gd_is_bundled()) {
4451			$this->DebugMessage('ImageCreateFromStringReplacement() calling built-in imagecreatefromstring()', __FILE__, __LINE__);
4452			return @imagecreatefromstring($RawImageData);
4453		}
4454		if ($this->issafemode) {
4455			$this->DebugMessage('ImageCreateFromStringReplacement() failed: cannot create temp file in SAFE_MODE', __FILE__, __LINE__);
4456			return false;
4457		}
4458
4459		switch (substr($RawImageData, 0, 3)) {
4460			case 'GIF':
4461				$ICFSreplacementFunctionName = 'imagecreatefromgif';
4462				break;
4463			case "\xFF\xD8\xFF":
4464				$ICFSreplacementFunctionName = 'imagecreatefromjpeg';
4465				break;
4466			case "\x89".'PN':
4467				$ICFSreplacementFunctionName = 'imagecreatefrompng';
4468				break;
4469			default:
4470				$this->DebugMessage('ImageCreateFromStringReplacement() failed: unknown fileformat signature "'.phpthumb_functions::HexCharDisplay(substr($RawImageData, 0, 3)).'"', __FILE__, __LINE__);
4471				return false;
4472				break;
4473		}
4474		$ErrorMessage = '';
4475		if ($tempnam = $this->phpThumb_tempnam()) {
4476			if ($fp_tempnam = @fopen($tempnam, 'wb')) {
4477				fwrite($fp_tempnam, $RawImageData);
4478				fclose($fp_tempnam);
4479				@chmod($tempnam, $this->getParameter('config_file_create_mask'));
4480				if (($ICFSreplacementFunctionName == 'imagecreatefromgif') && !function_exists($ICFSreplacementFunctionName)) {
4481
4482					// Need to create from GIF file, but imagecreatefromgif does not exist
4483					ob_start();
4484					if (!@include_once __DIR__ .'/phpthumb.gif.php' ) {
4485						$ErrorMessage = 'Failed to include required file "'. __DIR__ .'/phpthumb.gif.php" in '.__FILE__.' on line '.__LINE__;
4486						$this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
4487					}
4488					ob_end_clean();
4489					// gif_loadFileToGDimageResource() cannot read from raw data, write to file first
4490					if ($tempfilename = $this->phpThumb_tempnam()) {
4491						if ($fp_tempfile = @fopen($tempfilename, 'wb')) {
4492							fwrite($fp_tempfile, $RawImageData);
4493							fclose($fp_tempfile);
4494							$gdimg_source = gif_loadFileToGDimageResource($tempfilename);
4495							$this->DebugMessage('gif_loadFileToGDimageResource('.$tempfilename.') completed', __FILE__, __LINE__);
4496							$this->DebugMessage('deleting "'.$tempfilename.'"', __FILE__, __LINE__);
4497							unlink($tempfilename);
4498							return $gdimg_source;
4499						} else {
4500							$ErrorMessage = 'Failed to open tempfile in '.__FILE__.' on line '.__LINE__;
4501							$this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
4502						}
4503					} else {
4504						$ErrorMessage = 'Failed to open generate tempfile name in '.__FILE__.' on line '.__LINE__;
4505						$this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
4506					}
4507
4508				} elseif (function_exists($ICFSreplacementFunctionName) && ($gdimg_source = @$ICFSreplacementFunctionName($tempnam))) {
4509
4510					// great
4511					$this->DebugMessage($ICFSreplacementFunctionName.'('.$tempnam.') succeeded', __FILE__, __LINE__);
4512					$this->DebugMessage('deleting "'.$tempnam.'"', __FILE__, __LINE__);
4513					unlink($tempnam);
4514					return $gdimg_source;
4515
4516				} else {
4517
4518					// GD functions not available, or failed to create image
4519					$this->DebugMessage($ICFSreplacementFunctionName.'('.$tempnam.') '.(function_exists($ICFSreplacementFunctionName) ? 'failed' : 'does not exist'), __FILE__, __LINE__);
4520					if (isset($_GET['phpThumbDebug'])) {
4521						$this->phpThumbDebug();
4522					}
4523
4524				}
4525			} else {
4526				$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';
4527				if ($this->issafemode) {
4528					$ErrorMessage = 'ImageCreateFromStringReplacement() failed in '.__FILE__.' on line '.__LINE__.': cannot create temp file in SAFE_MODE';
4529				}
4530				$this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
4531			}
4532			$this->DebugMessage('deleting "'.$tempnam.'"', __FILE__, __LINE__);
4533			@unlink($tempnam);
4534		} else {
4535			$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';
4536			if ($this->issafemode) {
4537				$ErrorMessage = 'ImageCreateFromStringReplacement() failed in '.__FILE__.' on line '.__LINE__.': cannot create temp file in SAFE_MODE';
4538			}
4539		}
4540		if ($DieOnErrors && $ErrorMessage) {
4541			return $this->ErrorImage($ErrorMessage);
4542		}
4543		return false;
4544	}
4545
4546	public function ImageResizeFunction(&$dst_im, &$src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH) {
4547		$dstX = (int) round($dstX);
4548		$dstY = (int) round($dstY);
4549		$srcX = (int) round($srcX);
4550		$srcY = (int) round($srcY);
4551		$dstW = (int) round($dstW);
4552		$dstH = (int) round($dstH);
4553		$srcW = (int) round($srcW);
4554		$srcH = (int) round($srcH);
4555
4556		$this->DebugMessage('ImageResizeFunction($o, $s, '.$dstX.', '.$dstY.', '.$srcX.', '.$srcY.', '.$dstW.', '.$dstH.', '.$srcW.', '.$srcH.')', __FILE__, __LINE__);
4557		if (($dstW == $srcW) && ($dstH == $srcH)) {
4558			return imagecopy($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH);
4559		}
4560		if (phpthumb_functions::gd_version() >= 2.0) {
4561			if ($this->config_disable_imagecopyresampled) {
4562				return phpthumb_functions::ImageCopyResampleBicubic($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
4563			}
4564			return imagecopyresampled($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
4565		}
4566		return imagecopyresized($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
4567	}
4568
4569	public function InitializeTempDirSetting() {
4570		$this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe((function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : ''))); // sys_get_temp_dir added in PHP v5.2.1
4571		$this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(ini_get('upload_tmp_dir')));
4572		$this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(getenv('TMPDIR')));
4573		$this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(getenv('TMP')));
4574		return true;
4575	}
4576
4577	public function phpThumb_tempnam() {
4578		$this->InitializeTempDirSetting();
4579		$tempnam = $this->realPathSafe(tempnam($this->config_temp_directory, 'pThumb'));
4580		$this->tempFilesToDelete[$tempnam] = $tempnam;
4581		$this->DebugMessage('phpThumb_tempnam() returning "'.$tempnam.'"', __FILE__, __LINE__);
4582		return $tempnam;
4583	}
4584
4585	public function DebugMessage($message, $file='', $line='') {
4586		$this->debugmessages[] = $message.($file ? ' in file "'.(basename($file) ? basename($file) : $file).'"' : '').($line ? ' on line '.$line : '');
4587		return true;
4588	}
4589
4590	public function DebugTimingMessage($message, $file='', $line='', $timestamp=0) {
4591		if (!$timestamp) {
4592			$timestamp = array_sum(explode(' ', microtime()));
4593		}
4594		$this->debugtiming[number_format($timestamp, 6, '.', '')] = ': '.$message.($file ? ' in file "'.(basename($file) ? basename($file) : $file).'"' : '').($line ? ' on line '.$line : '');
4595		return true;
4596	}
4597
4598}
4599