PageRenderTime 79ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/phpthumb/phpthumb.class.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 3969 lines | 3316 code | 428 blank | 225 comment | 738 complexity | 7ceda9e3a161cf78cb9a181219852d64 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. //////////////////////////////////////////////////////////////
  3. /// phpThumb() by James Heinrich <info@silisoftware.com> //
  4. // available at http://phpthumb.sourceforge.net ///
  5. //////////////////////////////////////////////////////////////
  6. /// //
  7. // See: phpthumb.readme.txt for usage instructions //
  8. // ///
  9. //////////////////////////////////////////////////////////////
  10. ob_start();
  11. if (!include_once(dirname(__FILE__).'/phpthumb.functions.php')) {
  12. ob_end_flush();
  13. die('failed to include_once("'.realpath(dirname(__FILE__).'/phpthumb.functions.php').'")');
  14. }
  15. ob_end_clean();
  16. class phpthumb {
  17. // public:
  18. // START PARAMETERS (for object mode and phpThumb.php)
  19. // See phpthumb.readme.txt for descriptions of what each of these values are
  20. var $src = null; // SouRCe filename
  21. var $new = null; // NEW image (phpThumb.php only)
  22. var $w = null; // Width
  23. var $h = null; // Height
  24. var $wp = null; // Width (Portrait Images Only)
  25. var $hp = null; // Height (Portrait Images Only)
  26. var $wl = null; // Width (Landscape Images Only)
  27. var $hl = null; // Height (Landscape Images Only)
  28. var $ws = null; // Width (Square Images Only)
  29. var $hs = null; // Height (Square Images Only)
  30. var $f = null; // output image Format
  31. var $q = 75; // jpeg output Quality
  32. var $sx = null; // Source crop top-left X position
  33. var $sy = null; // Source crop top-left Y position
  34. var $sw = null; // Source crop Width
  35. var $sh = null; // Source crop Height
  36. var $zc = null; // Zoom Crop
  37. var $bc = null; // Border Color
  38. var $bg = null; // BackGround color
  39. var $fltr = array(); // FiLTeRs
  40. var $goto = null; // GO TO url after processing
  41. var $err = null; // default ERRor image filename
  42. var $xto = null; // extract eXif Thumbnail Only
  43. var $ra = null; // Rotate by Angle
  44. var $ar = null; // Auto Rotate
  45. var $aoe = null; // Allow Output Enlargement
  46. var $far = null; // Fixed Aspect Ratio
  47. var $iar = null; // Ignore Aspect Ratio
  48. var $maxb = null; // MAXimum Bytes
  49. var $down = null; // DOWNload thumbnail filename
  50. var $md5s = null; // MD5 hash of Source image
  51. var $sfn = 0; // Source Frame Number
  52. var $dpi = 150; // Dots Per Inch for vector source formats
  53. var $sia = null; // Save Image As filename
  54. var $file = null; // >>>deprecated, DO NOT USE, will be removed in future versions<<<
  55. var $phpThumbDebug = null;
  56. // END PARAMETERS
  57. // public:
  58. // START CONFIGURATION OPTIONS (for object mode only)
  59. // See phpThumb.config.php for descriptions of what each of these settings do
  60. // * Directory Configuration
  61. var $config_cache_directory = null;
  62. var $config_cache_directory_depth = 0;
  63. var $config_cache_disable_warning = true;
  64. var $config_cache_source_enabled = false;
  65. var $config_cache_source_directory = null;
  66. var $config_temp_directory = null;
  67. var $config_document_root = null;
  68. // * Default output configuration:
  69. var $config_output_format = 'jpeg';
  70. var $config_output_maxwidth = 0;
  71. var $config_output_maxheight = 0;
  72. var $config_output_interlace = true;
  73. // * Error message configuration
  74. var $config_error_image_width = 400;
  75. var $config_error_image_height = 100;
  76. var $config_error_message_image_default = '';
  77. var $config_error_bgcolor = 'CCCCFF';
  78. var $config_error_textcolor = 'FF0000';
  79. var $config_error_fontsize = 1;
  80. var $config_error_die_on_error = false;
  81. var $config_error_silent_die_on_error = false;
  82. var $config_error_die_on_source_failure = true;
  83. // * Anti-Hotlink Configuration:
  84. var $config_nohotlink_enabled = true;
  85. var $config_nohotlink_valid_domains = array();
  86. var $config_nohotlink_erase_image = true;
  87. var $config_nohotlink_text_message = 'Off-server thumbnailing is not allowed';
  88. // * Off-server Linking Configuration:
  89. var $config_nooffsitelink_enabled = false;
  90. var $config_nooffsitelink_valid_domains = array();
  91. var $config_nooffsitelink_require_refer = false;
  92. var $config_nooffsitelink_erase_image = true;
  93. var $config_nooffsitelink_watermark_src = '';
  94. var $config_nooffsitelink_text_message = 'Off-server linking is not allowed';
  95. // * Border & Background default colors
  96. var $config_border_hexcolor = '000000';
  97. var $config_background_hexcolor = 'FFFFFF';
  98. // * TrueType Fonts
  99. var $config_ttf_directory = './fonts';
  100. var $config_max_source_pixels = null;
  101. var $config_use_exif_thumbnail_for_speed = false;
  102. var $allow_local_http_src = false;
  103. var $config_imagemagick_path = null;
  104. var $config_prefer_imagemagick = true;
  105. var $config_imagemagick_use_thumbnail = true;
  106. var $config_cache_maxage = null;
  107. var $config_cache_maxsize = null;
  108. var $config_cache_maxfiles = null;
  109. var $config_cache_source_filemtime_ignore_local = false;
  110. var $config_cache_source_filemtime_ignore_remote = true;
  111. var $config_cache_default_only_suffix = false;
  112. var $config_cache_force_passthru = true;
  113. var $config_cache_prefix = ''; // default value set in the constructor below
  114. // * MySQL
  115. var $config_mysql_query = null;
  116. var $config_mysql_hostname = null;
  117. var $config_mysql_username = null;
  118. var $config_mysql_password = null;
  119. var $config_mysql_database = null;
  120. // * Security
  121. var $config_high_security_enabled = false;
  122. var $config_high_security_password = null;
  123. var $config_disable_debug = false;
  124. var $config_allow_src_above_docroot = false;
  125. var $config_allow_src_above_phpthumb = true;
  126. var $config_allow_parameter_file = false;
  127. var $config_allow_parameter_goto = false;
  128. // * HTTP fopen
  129. var $config_http_fopen_timeout = 10;
  130. var $config_http_follow_redirect = true;
  131. // * Compatability
  132. var $config_disable_pathinfo_parsing = false;
  133. var $config_disable_imagecopyresampled = false;
  134. var $config_disable_onlycreateable_passthru = false;
  135. var $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';
  136. // END CONFIGURATION OPTIONS
  137. // public: error messages (read-only; persistant)
  138. var $debugmessages = array();
  139. var $debugtiming = array();
  140. var $fatalerror = null;
  141. // private: (should not be modified directly)
  142. var $thumbnailQuality = 75;
  143. var $thumbnailFormat = null;
  144. var $sourceFilename = null;
  145. var $rawImageData = null;
  146. var $IMresizedData = null;
  147. var $outputImageData = null;
  148. var $useRawIMoutput = false;
  149. var $gdimg_output = null;
  150. var $gdimg_source = null;
  151. var $getimagesizeinfo = null;
  152. var $source_width = null;
  153. var $source_height = null;
  154. var $thumbnailCropX = null;
  155. var $thumbnailCropY = null;
  156. var $thumbnailCropW = null;
  157. var $thumbnailCropH = null;
  158. var $exif_thumbnail_width = null;
  159. var $exif_thumbnail_height = null;
  160. var $exif_thumbnail_type = null;
  161. var $exif_thumbnail_data = null;
  162. var $exif_raw_data = null;
  163. var $thumbnail_width = null;
  164. var $thumbnail_height = null;
  165. var $thumbnail_image_width = null;
  166. var $thumbnail_image_height = null;
  167. var $cache_filename = null;
  168. var $AlphaCapableFormats = array('png', 'ico', 'gif');
  169. var $is_alpha = false;
  170. var $iswindows = null;
  171. var $issafemode = null;
  172. var $phpthumb_version = '1.7.10-201104242100';
  173. //////////////////////////////////////////////////////////////////////
  174. // public: constructor
  175. function phpThumb() {
  176. $this->DebugTimingMessage('phpThumb() constructor', __FILE__, __LINE__);
  177. $this->DebugMessage('phpThumb() v'.$this->phpthumb_version, __FILE__, __LINE__);
  178. $this->config_max_source_pixels = round(max(intval(ini_get('memory_limit')), intval(get_cfg_var('memory_limit'))) * 1048576 * 0.20); // 20% of memory_limit
  179. $this->iswindows = (bool) (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
  180. $this->issafemode = (bool) preg_match('#(1|ON)#i', ini_get('safe_mode'));
  181. $this->config_document_root = (@$_SERVER['DOCUMENT_ROOT'] ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root);
  182. $this->config_cache_prefix = 'phpThumb_cache_'.@$_SERVER['SERVER_NAME'];
  183. $php_sapi_name = strtolower(function_exists('php_sapi_name') ? php_sapi_name() : '');
  184. if ($php_sapi_name == 'cli') {
  185. $this->config_allow_src_above_docroot = true;
  186. }
  187. }
  188. // public:
  189. function setSourceFilename($sourceFilename) {
  190. //$this->resetObject();
  191. //$this->rawImageData = null;
  192. $this->sourceFilename = $sourceFilename;
  193. $this->src = $sourceFilename;
  194. if (is_null($this->config_output_format)) {
  195. $sourceFileExtension = strtolower(substr(strrchr($sourceFilename, '.'), 1));
  196. if (preg_match('#^[a-z]{3,4}$#', $sourceFileExtension)) {
  197. $this->config_output_format = $sourceFileExtension;
  198. $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->config_output_format to "'.$sourceFileExtension.'"', __FILE__, __LINE__);
  199. } else {
  200. $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__);
  201. }
  202. }
  203. $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->sourceFilename to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
  204. return true;
  205. }
  206. // public:
  207. function setSourceData($rawImageData, $sourceFilename='') {
  208. //$this->resetObject();
  209. //$this->sourceFilename = null;
  210. $this->rawImageData = $rawImageData;
  211. $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__);
  212. if ($this->config_cache_source_enabled) {
  213. $sourceFilename = ($sourceFilename ? $sourceFilename : md5($rawImageData));
  214. if (!is_dir($this->config_cache_source_directory)) {
  215. $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not a directory');
  216. } elseif (!@is_writable($this->config_cache_source_directory)) {
  217. $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not writable');
  218. }
  219. $this->DebugMessage('setSourceData() attempting to save source image to "'.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).'"', __FILE__, __LINE__);
  220. if ($fp = @fopen($this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename), 'wb')) {
  221. fwrite($fp, $rawImageData);
  222. fclose($fp);
  223. } elseif (!$this->phpThumbDebug) {
  224. $this->ErrorImage('setSourceData() failed to write to source cache ('.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).')');
  225. }
  226. }
  227. return true;
  228. }
  229. // public:
  230. function setSourceImageResource($gdimg) {
  231. //$this->resetObject();
  232. $this->gdimg_source = $gdimg;
  233. return true;
  234. }
  235. // public:
  236. function setParameter($param, $value) {
  237. if ($param == 'src') {
  238. $this->setSourceFilename($this->ResolveFilenameToAbsolute($value));
  239. } elseif (@is_array($this->$param)) {
  240. if (is_array($value)) {
  241. foreach ($value as $arraykey => $arrayvalue) {
  242. array_push($this->$param, $arrayvalue);
  243. }
  244. } else {
  245. array_push($this->$param, $value);
  246. }
  247. } else {
  248. $this->$param = $value;
  249. }
  250. return true;
  251. }
  252. // public:
  253. function getParameter($param) {
  254. //if (property_exists('phpThumb', $param)) {
  255. return $this->$param;
  256. //}
  257. //$this->DebugMessage('setParameter() attempting to get non-existant parameter "'.$param.'"', __FILE__, __LINE__);
  258. //return false;
  259. }
  260. // public:
  261. function GenerateThumbnail() {
  262. $this->setOutputFormat();
  263. $this->phpThumbDebug('8a');
  264. $this->ResolveSource();
  265. $this->phpThumbDebug('8b');
  266. $this->SetCacheFilename();
  267. $this->phpThumbDebug('8c');
  268. $this->ExtractEXIFgetImageSize();
  269. $this->phpThumbDebug('8d');
  270. if ($this->useRawIMoutput) {
  271. $this->DebugMessage('Skipping rest of GenerateThumbnail() because ($this->useRawIMoutput == true)', __FILE__, __LINE__);
  272. return true;
  273. }
  274. $this->phpThumbDebug('8e');
  275. if (!$this->SourceImageToGD()) {
  276. $this->DebugMessage('SourceImageToGD() failed', __FILE__, __LINE__);
  277. return false;
  278. }
  279. $this->phpThumbDebug('8f');
  280. $this->Rotate();
  281. $this->phpThumbDebug('8g');
  282. $this->CreateGDoutput();
  283. $this->phpThumbDebug('8h');
  284. switch ($this->far) {
  285. case 'L':
  286. case 'TL':
  287. case 'BL':
  288. $destination_offset_x = 0;
  289. $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
  290. break;
  291. case 'R':
  292. case 'TR':
  293. case 'BR':
  294. $destination_offset_x = round($this->thumbnail_width - $this->thumbnail_image_width);
  295. $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
  296. break;
  297. case 'T':
  298. case 'TL':
  299. case 'TR':
  300. $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2);
  301. $destination_offset_y = 0;
  302. break;
  303. case 'B':
  304. case 'BL':
  305. case 'BR':
  306. $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2);
  307. $destination_offset_y = round($this->thumbnail_height - $this->thumbnail_image_height);
  308. break;
  309. case 'C':
  310. default:
  311. $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2);
  312. $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
  313. }
  314. // // copy/resize image to appropriate dimensions
  315. // $borderThickness = 0;
  316. // if (!empty($this->fltr)) {
  317. // foreach ($this->fltr as $key => $value) {
  318. // if (preg_match('#^bord\|([0-9]+)#', $value, $matches)) {
  319. // $borderThickness = $matches[1];
  320. // break;
  321. // }
  322. // }
  323. // }
  324. // if ($borderThickness > 0) {
  325. // //$this->DebugMessage('Skipping ImageResizeFunction() because BorderThickness="'.$borderThickness.'"', __FILE__, __LINE__);
  326. // $this->thumbnail_image_height /= 2;
  327. // }
  328. $this->ImageResizeFunction(
  329. $this->gdimg_output,
  330. $this->gdimg_source,
  331. $destination_offset_x,
  332. $destination_offset_y,
  333. $this->thumbnailCropX,
  334. $this->thumbnailCropY,
  335. $this->thumbnail_image_width,
  336. $this->thumbnail_image_height,
  337. $this->thumbnailCropW,
  338. $this->thumbnailCropH
  339. );
  340. $this->DebugMessage('memory_get_usage() after copy-resize = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
  341. ImageDestroy($this->gdimg_source);
  342. $this->DebugMessage('memory_get_usage() after ImageDestroy = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
  343. $this->phpThumbDebug('8i');
  344. $this->AntiOffsiteLinking();
  345. $this->phpThumbDebug('8j');
  346. $this->ApplyFilters();
  347. $this->phpThumbDebug('8k');
  348. $this->AlphaChannelFlatten();
  349. $this->phpThumbDebug('8l');
  350. $this->MaxFileSize();
  351. $this->phpThumbDebug('8m');
  352. $this->DebugMessage('GenerateThumbnail() completed successfully', __FILE__, __LINE__);
  353. return true;
  354. }
  355. // public:
  356. function RenderOutput() {
  357. if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) {
  358. $this->DebugMessage('RenderOutput() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
  359. return false;
  360. }
  361. if (!$this->thumbnailFormat) {
  362. $this->DebugMessage('RenderOutput() failed because $this->thumbnailFormat is empty', __FILE__, __LINE__);
  363. return false;
  364. }
  365. if ($this->useRawIMoutput) {
  366. $this->DebugMessage('RenderOutput copying $this->IMresizedData ('.strlen($this->IMresizedData).' bytes) to $this->outputImage', __FILE__, __LINE__);
  367. $this->outputImageData = $this->IMresizedData;
  368. return true;
  369. }
  370. $builtin_formats = array();
  371. if (function_exists('ImageTypes')) {
  372. $imagetypes = ImageTypes();
  373. $builtin_formats['wbmp'] = (bool) ($imagetypes & IMG_WBMP);
  374. $builtin_formats['jpg'] = (bool) ($imagetypes & IMG_JPG);
  375. $builtin_formats['gif'] = (bool) ($imagetypes & IMG_GIF);
  376. $builtin_formats['png'] = (bool) ($imagetypes & IMG_PNG);
  377. }
  378. $this->DebugMessage('RenderOutput() attempting Image'.strtoupper(@$this->thumbnailFormat).'($this->gdimg_output)', __FILE__, __LINE__);
  379. ob_start();
  380. switch ($this->thumbnailFormat) {
  381. case 'wbmp':
  382. if (!@$builtin_formats['wbmp']) {
  383. $this->DebugMessage('GD does not have required built-in support for WBMP output', __FILE__, __LINE__);
  384. ob_end_clean();
  385. return false;
  386. }
  387. ImageJPEG($this->gdimg_output, null, $this->thumbnailQuality);
  388. $this->outputImageData = ob_get_contents();
  389. break;
  390. case 'jpeg':
  391. case 'jpg': // should be "jpeg" not "jpg" but just in case...
  392. if (!@$builtin_formats['jpg']) {
  393. $this->DebugMessage('GD does not have required built-in support for JPEG output', __FILE__, __LINE__);
  394. ob_end_clean();
  395. return false;
  396. }
  397. ImageJPEG($this->gdimg_output, null, $this->thumbnailQuality);
  398. $this->outputImageData = ob_get_contents();
  399. break;
  400. case 'png':
  401. if (!@$builtin_formats['png']) {
  402. $this->DebugMessage('GD does not have required built-in support for PNG output', __FILE__, __LINE__);
  403. ob_end_clean();
  404. return false;
  405. }
  406. ImagePNG($this->gdimg_output);
  407. $this->outputImageData = ob_get_contents();
  408. break;
  409. case 'gif':
  410. if (!@$builtin_formats['gif']) {
  411. $this->DebugMessage('GD does not have required built-in support for GIF output', __FILE__, __LINE__);
  412. ob_end_clean();
  413. return false;
  414. }
  415. ImageGIF($this->gdimg_output);
  416. $this->outputImageData = ob_get_contents();
  417. break;
  418. case 'bmp':
  419. $ImageOutFunction = '"builtin BMP output"';
  420. if (!@include_once(dirname(__FILE__).'/phpthumb.bmp.php')) {
  421. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
  422. ob_end_clean();
  423. return false;
  424. }
  425. $phpthumb_bmp = new phpthumb_bmp();
  426. $this->outputImageData = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
  427. unset($phpthumb_bmp);
  428. break;
  429. case 'ico':
  430. $ImageOutFunction = '"builtin ICO output"';
  431. if (!@include_once(dirname(__FILE__).'/phpthumb.ico.php')) {
  432. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
  433. ob_end_clean();
  434. return false;
  435. }
  436. $phpthumb_ico = new phpthumb_ico();
  437. $arrayOfOutputImages = array($this->gdimg_output);
  438. $this->outputImageData = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
  439. unset($phpthumb_ico);
  440. break;
  441. default:
  442. $this->DebugMessage('RenderOutput failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
  443. ob_end_clean();
  444. return false;
  445. }
  446. ob_end_clean();
  447. if (!$this->outputImageData) {
  448. $this->DebugMessage('RenderOutput() for "'.$this->thumbnailFormat.'" failed', __FILE__, __LINE__);
  449. ob_end_clean();
  450. return false;
  451. }
  452. $this->DebugMessage('RenderOutput() completing with $this->outputImageData = '.strlen($this->outputImageData).' bytes', __FILE__, __LINE__);
  453. return true;
  454. }
  455. // public:
  456. function RenderToFile($filename) {
  457. if (preg_match('#^(f|ht)tps?\://#i', $filename)) {
  458. $this->DebugMessage('RenderToFile() failed because $filename ('.$filename.') is a URL', __FILE__, __LINE__);
  459. return false;
  460. }
  461. // render thumbnail to this file only, do not cache, do not output to browser
  462. //$renderfilename = $this->ResolveFilenameToAbsolute(dirname($filename)).DIRECTORY_SEPARATOR.basename($filename);
  463. $renderfilename = $filename;
  464. if (($filename{0} != '/') && ($filename{0} != '\\') && ($filename{1} != ':')) {
  465. $renderfilename = $this->ResolveFilenameToAbsolute($renderfilename);
  466. }
  467. if (!@is_writable(dirname($renderfilename))) {
  468. $this->DebugMessage('RenderToFile() failed because "'.dirname($renderfilename).'/" is not writable', __FILE__, __LINE__);
  469. return false;
  470. }
  471. if (@is_file($renderfilename) && !@is_writable($renderfilename)) {
  472. $this->DebugMessage('RenderToFile() failed because "'.$renderfilename.'" is not writable', __FILE__, __LINE__);
  473. return false;
  474. }
  475. if ($this->RenderOutput()) {
  476. if (file_put_contents($renderfilename, $this->outputImageData)) {
  477. $this->DebugMessage('RenderToFile('.$renderfilename.') succeeded', __FILE__, __LINE__);
  478. return true;
  479. }
  480. if (!@file_exists($renderfilename)) {
  481. $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] did not appear to fail, but the output image does not exist either...', __FILE__, __LINE__);
  482. }
  483. } else {
  484. $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] failed', __FILE__, __LINE__);
  485. }
  486. return false;
  487. }
  488. // public:
  489. function OutputThumbnail() {
  490. if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) {
  491. $this->DebugMessage('OutputThumbnail() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
  492. return false;
  493. }
  494. if (headers_sent()) {
  495. return $this->ErrorImage('OutputThumbnail() failed - headers already sent');
  496. exit;
  497. }
  498. $downloadfilename = phpthumb_functions::SanitizeFilename(is_string($this->sia) ? $this->sia : ($this->down ? $this->down : 'phpThumb_generated_thumbnail'.'.'.$this->thumbnailFormat));
  499. $this->DebugMessage('Content-Disposition header filename set to "'.$downloadfilename.'"', __FILE__, __LINE__);
  500. if ($downloadfilename) {
  501. header('Content-Disposition: '.($this->down ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"');
  502. } else {
  503. $this->DebugMessage('failed to send Content-Disposition header because $downloadfilename is empty', __FILE__, __LINE__);
  504. }
  505. if ($this->useRawIMoutput) {
  506. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  507. echo $this->IMresizedData;
  508. } else {
  509. $this->DebugMessage('ImageInterlace($this->gdimg_output, '.intval($this->config_output_interlace).')', __FILE__, __LINE__);
  510. ImageInterlace($this->gdimg_output, intval($this->config_output_interlace));
  511. switch ($this->thumbnailFormat) {
  512. case 'jpeg':
  513. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  514. $ImageOutFunction = 'image'.$this->thumbnailFormat;
  515. @$ImageOutFunction($this->gdimg_output, '', $this->thumbnailQuality);
  516. break;
  517. case 'png':
  518. case 'gif':
  519. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  520. $ImageOutFunction = 'image'.$this->thumbnailFormat;
  521. @$ImageOutFunction($this->gdimg_output);
  522. break;
  523. case 'bmp':
  524. if (!@include_once(dirname(__FILE__).'/phpthumb.bmp.php')) {
  525. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
  526. return false;
  527. }
  528. $phpthumb_bmp = new phpthumb_bmp();
  529. if (is_object($phpthumb_bmp)) {
  530. $bmp_data = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
  531. unset($phpthumb_bmp);
  532. if (!$bmp_data) {
  533. $this->DebugMessage('$phpthumb_bmp->GD2BMPstring() failed', __FILE__, __LINE__);
  534. return false;
  535. }
  536. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  537. echo $bmp_data;
  538. } else {
  539. $this->DebugMessage('new phpthumb_bmp() failed', __FILE__, __LINE__);
  540. return false;
  541. }
  542. break;
  543. case 'ico':
  544. if (!@include_once(dirname(__FILE__).'/phpthumb.ico.php')) {
  545. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
  546. return false;
  547. }
  548. $phpthumb_ico = new phpthumb_ico();
  549. if (is_object($phpthumb_ico)) {
  550. $arrayOfOutputImages = array($this->gdimg_output);
  551. $ico_data = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
  552. unset($phpthumb_ico);
  553. if (!$ico_data) {
  554. $this->DebugMessage('$phpthumb_ico->GD2ICOstring() failed', __FILE__, __LINE__);
  555. return false;
  556. }
  557. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  558. echo $ico_data;
  559. } else {
  560. $this->DebugMessage('new phpthumb_ico() failed', __FILE__, __LINE__);
  561. return false;
  562. }
  563. break;
  564. default:
  565. $this->DebugMessage('OutputThumbnail failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
  566. return false;
  567. break;
  568. }
  569. }
  570. return true;
  571. }
  572. // public:
  573. function CleanUpCacheDirectory() {
  574. $this->DebugMessage('CleanUpCacheDirectory() set to purge ('.number_format($this->config_cache_maxage / 86400, 1).' days; '.number_format($this->config_cache_maxsize / 1048576, 2).'MB; '.number_format($this->config_cache_maxfiles).' files)', __FILE__, __LINE__);
  575. $DeletedKeys = array();
  576. $AllFilesInCacheDirectory = array();
  577. if (($this->config_cache_maxage > 0) || ($this->config_cache_maxsize > 0) || ($this->config_cache_maxfiles > 0)) {
  578. $CacheDirOldFilesAge = array();
  579. $CacheDirOldFilesSize = array();
  580. $AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory);
  581. foreach ($AllFilesInCacheDirectory as $fullfilename) {
  582. if (preg_match('#^phpThumb_cache_#i', $fullfilename) && file_exists($fullfilename)) {
  583. $CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename);
  584. if ($CacheDirOldFilesAge[$fullfilename] == 0) {
  585. $CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename);
  586. }
  587. $CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename);
  588. }
  589. }
  590. if (empty($CacheDirOldFilesSize)) {
  591. return true;
  592. }
  593. $DeletedKeys['zerobyte'] = array();
  594. foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) {
  595. // purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files)
  596. $cutofftime = time() - 3600;
  597. if (($filesize == 0) && ($CacheDirOldFilesAge[$fullfilename] < $cutofftime)) {
  598. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  599. if (@unlink($fullfilename)) {
  600. $DeletedKeys['zerobyte'][] = $fullfilename;
  601. unset($CacheDirOldFilesSize[$fullfilename]);
  602. unset($CacheDirOldFilesAge[$fullfilename]);
  603. }
  604. }
  605. }
  606. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['zerobyte']).' zero-byte files', __FILE__, __LINE__);
  607. asort($CacheDirOldFilesAge);
  608. if ($this->config_cache_maxfiles > 0) {
  609. $TotalCachedFiles = count($CacheDirOldFilesAge);
  610. $DeletedKeys['maxfiles'] = array();
  611. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  612. if ($TotalCachedFiles > $this->config_cache_maxfiles) {
  613. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  614. if (@unlink($fullfilename)) {
  615. $TotalCachedFiles--;
  616. $DeletedKeys['maxfiles'][] = $fullfilename;
  617. }
  618. } else {
  619. // there are few enough files to keep the rest
  620. break;
  621. }
  622. }
  623. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxfiles']).' files based on (config_cache_maxfiles='.$this->config_cache_maxfiles.')', __FILE__, __LINE__);
  624. foreach ($DeletedKeys['maxfiles'] as $fullfilename) {
  625. unset($CacheDirOldFilesAge[$fullfilename]);
  626. unset($CacheDirOldFilesSize[$fullfilename]);
  627. }
  628. }
  629. if ($this->config_cache_maxage > 0) {
  630. $mindate = time() - $this->config_cache_maxage;
  631. $DeletedKeys['maxage'] = array();
  632. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  633. if ($filedate > 0) {
  634. if ($filedate < $mindate) {
  635. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  636. if (@unlink($fullfilename)) {
  637. $DeletedKeys['maxage'][] = $fullfilename;
  638. }
  639. } else {
  640. // the rest of the files are new enough to keep
  641. break;
  642. }
  643. }
  644. }
  645. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxage']).' files based on (config_cache_maxage='.$this->config_cache_maxage.')', __FILE__, __LINE__);
  646. foreach ($DeletedKeys['maxage'] as $fullfilename) {
  647. unset($CacheDirOldFilesAge[$fullfilename]);
  648. unset($CacheDirOldFilesSize[$fullfilename]);
  649. }
  650. }
  651. if ($this->config_cache_maxsize > 0) {
  652. $TotalCachedFileSize = array_sum($CacheDirOldFilesSize);
  653. $DeletedKeys['maxsize'] = array();
  654. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  655. if ($TotalCachedFileSize > $this->config_cache_maxsize) {
  656. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  657. if (@unlink($fullfilename)) {
  658. $TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename];
  659. $DeletedKeys['maxsize'][] = $fullfilename;
  660. }
  661. } else {
  662. // the total filesizes are small enough to keep the rest of the files
  663. break;
  664. }
  665. }
  666. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxsize']).' files based on (config_cache_maxsize='.$this->config_cache_maxsize.')', __FILE__, __LINE__);
  667. foreach ($DeletedKeys['maxsize'] as $fullfilename) {
  668. unset($CacheDirOldFilesAge[$fullfilename]);
  669. unset($CacheDirOldFilesSize[$fullfilename]);
  670. }
  671. }
  672. } else {
  673. $this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__);
  674. }
  675. $totalpurged = 0;
  676. foreach ($DeletedKeys as $key => $value) {
  677. $totalpurged += count($value);
  678. }
  679. $this->DebugMessage('CleanUpCacheDirectory() purged '.$totalpurged.' files (from '.count($AllFilesInCacheDirectory).') based on config settings', __FILE__, __LINE__);
  680. if ($totalpurged > 0) {
  681. $empty_dirs = array();
  682. foreach ($AllFilesInCacheDirectory as $fullfilename) {
  683. if (is_dir($fullfilename)) {
  684. $empty_dirs[realpath($fullfilename)] = 1;
  685. } else {
  686. unset($empty_dirs[realpath(dirname($fullfilename))]);
  687. }
  688. }
  689. krsort($empty_dirs);
  690. $totalpurgeddirs = 0;
  691. foreach ($empty_dirs as $empty_dir => $dummy) {
  692. if ($empty_dir == $this->config_cache_directory) {
  693. // shouldn't happen, but just in case, don't let it delete actual cache directory
  694. continue;
  695. } elseif (@rmdir($empty_dir)) {
  696. $totalpurgeddirs++;
  697. } else {
  698. $this->DebugMessage('failed to rmdir('.$empty_dir.')', __FILE__, __LINE__);
  699. }
  700. }
  701. $this->DebugMessage('purged '.$totalpurgeddirs.' empty directories', __FILE__, __LINE__);
  702. }
  703. return true;
  704. }
  705. //////////////////////////////////////////////////////////////////////
  706. // private: re-initializator (call between rendering multiple images with one object)
  707. function resetObject() {
  708. $class_vars = get_class_vars(get_class($this));
  709. foreach ($class_vars as $key => $value) {
  710. // do not clobber debug or config info
  711. if (!preg_match('#^(config_|debug|fatalerror)#i', $key)) {
  712. $this->$key = $value;
  713. }
  714. }
  715. $this->phpThumb(); // re-initialize some class variables
  716. return true;
  717. }
  718. //////////////////////////////////////////////////////////////////////
  719. function ResolveSource() {
  720. if (is_resource($this->gdimg_source)) {
  721. $this->DebugMessage('ResolveSource() exiting because is_resource($this->gdimg_source)', __FILE__, __LINE__);
  722. return true;
  723. }
  724. if ($this->rawImageData) {
  725. $this->sourceFilename = null;
  726. $this->DebugMessage('ResolveSource() exiting because $this->rawImageData is set ('.number_format(strlen($this->rawImageData)).' bytes)', __FILE__, __LINE__);
  727. return true;
  728. }
  729. if ($this->sourceFilename) {
  730. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->sourceFilename);
  731. $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
  732. } elseif ($this->src) {
  733. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
  734. $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'" from $this->src ('.$this->src.')', __FILE__, __LINE__);
  735. } else {
  736. return $this->ErrorImage('$this->sourceFilename and $this->src are both empty');
  737. }
  738. if ($this->iswindows && ((substr($this->sourceFilename, 0, 2) == '//') || (substr($this->sourceFilename, 0, 2) == '\\\\'))) {
  739. // Windows \\share\filename.ext
  740. } elseif (preg_match('#^(f|ht)tps?\://#i', $this->sourceFilename)) {
  741. // URL
  742. if ($this->config_http_user_agent) {
  743. ini_set('user_agent', $this->config_http_user_agent);
  744. }
  745. } elseif (!@file_exists($this->sourceFilename)) {
  746. return $this->ErrorImage('"'.$this->sourceFilename.'" does not exist');
  747. } elseif (!@is_file($this->sourceFilename)) {
  748. return $this->ErrorImage('"'.$this->sourceFilename.'" is not a file');
  749. }
  750. return true;
  751. }
  752. function setOutputFormat() {
  753. static $alreadyCalled = false;
  754. if ($this->thumbnailFormat && $alreadyCalled) {
  755. return true;
  756. }
  757. $alreadyCalled = true;
  758. $AvailableImageOutputFormats = array();
  759. $AvailableImageOutputFormats[] = 'text';
  760. if (@is_readable(dirname(__FILE__).'/phpthumb.ico.php')) {
  761. $AvailableImageOutputFormats[] = 'ico';
  762. }
  763. if (@is_readable(dirname(__FILE__).'/phpthumb.bmp.php')) {
  764. $AvailableImageOutputFormats[] = 'bmp';
  765. }
  766. $this->thumbnailFormat = 'ico';
  767. // Set default output format based on what image types are available
  768. if (function_exists('ImageTypes')) {
  769. $imagetypes = ImageTypes();
  770. if ($imagetypes & IMG_WBMP) {
  771. $this->thumbnailFormat = 'wbmp';
  772. $AvailableImageOutputFormats[] = 'wbmp';
  773. }
  774. if ($imagetypes & IMG_GIF) {
  775. $this->thumbnailFormat = 'gif';
  776. $AvailableImageOutputFormats[] = 'gif';
  777. }
  778. if ($imagetypes & IMG_PNG) {
  779. $this->thumbnailFormat = 'png';
  780. $AvailableImageOutputFormats[] = 'png';
  781. }
  782. if ($imagetypes & IMG_JPG) {
  783. $this->thumbnailFormat = 'jpeg';
  784. $AvailableImageOutputFormats[] = 'jpeg';
  785. }
  786. } else {
  787. //return $this->ErrorImage('ImageTypes() does not exist - GD support might not be enabled?');
  788. $this->DebugMessage('ImageTypes() does not exist - GD support might not be enabled?', __FILE__, __LINE__);
  789. }
  790. if ($this->ImageMagickVersion()) {
  791. $IMformats = array('jpeg', 'png', 'gif', 'bmp', 'ico', 'wbmp');
  792. $this->DebugMessage('Addding ImageMagick formats to $AvailableImageOutputFormats ('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
  793. foreach ($IMformats as $key => $format) {
  794. $AvailableImageOutputFormats[] = $format;
  795. }
  796. }
  797. $AvailableImageOutputFormats = array_unique($AvailableImageOutputFormats);
  798. $this->DebugMessage('$AvailableImageOutputFormats = array('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
  799. $this->f = preg_replace('#[^a-z]#', '', strtolower($this->f));
  800. if (strtolower($this->config_output_format) == 'jpg') {
  801. $this->config_output_format = 'jpeg';
  802. }
  803. if (strtolower($this->f) == 'jpg') {
  804. $this->f = 'jpeg';
  805. }
  806. if (phpthumb_functions::CaseInsensitiveInArray($this->config_output_format, $AvailableImageOutputFormats)) {
  807. // set output format to config default if that format is available
  808. $this->DebugMessage('$this->thumbnailFormat set to $this->config_output_format "'.strtolower($this->config_output_format).'"', __FILE__, __LINE__);
  809. $this->thumbnailFormat = strtolower($this->config_output_format);
  810. } elseif ($this->config_output_format) {
  811. $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->config_output_format ('.strtolower($this->config_output_format).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
  812. }
  813. if ($this->f && (phpthumb_functions::CaseInsensitiveInArray($this->f, $AvailableImageOutputFormats))) {
  814. // override output format if $this->f is set and that format is available
  815. $this->DebugMessage('$this->thumbnailFormat set to $this->f "'.strtolower($this->f).'"', __FILE__, __LINE__);
  816. $this->thumbnailFormat = strtolower($this->f);
  817. } elseif ($this->f) {
  818. $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->f ('.strtolower($this->f).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
  819. }
  820. // for JPEG images, quality 1 (worst) to 99 (best)
  821. // quality < 25 is nasty, with not much size savings - not recommended
  822. // problems with 100 - invalid JPEG?
  823. $this->thumbnailQuality = max(1, min(99, ($this->q ? intval($this->q) : 75)));
  824. $this->DebugMessage('$this->thumbnailQuality set to "'.$this->thumbnailQuality.'"', __FILE__, __LINE__);
  825. return true;
  826. }
  827. function setCacheDirectory() {
  828. // resolve cache directory to absolute pathname
  829. $this->DebugMessage('setCacheDirectory() starting with config_cache_directory = "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
  830. if (substr($this->config_cache_directory, 0, 1) == '.') {
  831. if (preg_match('#^(f|ht)tps?\://#i', $this->src)) {
  832. if (!$this->config_cache_disable_warning) {
  833. $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');
  834. }
  835. } elseif ($this->src) {
  836. // resolve relative cache directory to source image
  837. $this->config_cache_directory = dirname($this->ResolveFilenameToAbsolute($this->src)).DIRECTORY_SEPARATOR.$this->config_cache_directory;
  838. } else {
  839. // $this->new is probably set
  840. }
  841. }
  842. if (substr($this->config_cache_directory, -1) == '/') {
  843. $this->config_cache_directory = substr($this->config_cache_directory, 0, -1);
  844. }
  845. if ($this->iswindows) {
  846. $this->config_cache_directory = str_replace('/', DIRECTORY_SEPARATOR, $this->config_cache_directory);
  847. }
  848. if ($this->config_cache_directory) {
  849. $real_cache_path = realpath($this->config_cache_directory);
  850. if (!$real_cache_path) {
  851. $this->DebugMessage('realpath($this->config_cache_directory) failed for "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
  852. if (!is_dir($this->config_cache_directory)) {
  853. $this->DebugMessage('!is_dir('.$this->config_cache_directory.')', __FILE__, __LINE__);
  854. }
  855. }
  856. if ($real_cache_path) {
  857. $this->DebugMessage('setting config_cache_directory to realpath('.$this->config_cache_directory.') = "'.$real_cache_path.'"', __FILE__, __LINE__);
  858. $this->config_cache_directory = $real_cache_path;
  859. }
  860. }
  861. if (!is_dir($this->config_cache_directory)) {
  862. if (!$this->config_cache_disable_warning) {
  863. $this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') does not exist. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php');
  864. }
  865. $this->DebugMessage('$this->config_cache_directory ('.$this->config_cache_directory.') is not a directory', __FILE__, __LINE__);
  866. $this->config_cache_directory = null;
  867. } elseif (!@is_writable($this->config_cache_directory)) {
  868. $this->DebugMessage('$this->config_cache_directory is not writable ('.$this->config_cache_directory.')', __FILE__, __LINE__);
  869. }
  870. $this->InitializeTempDirSetting();
  871. 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)) {
  872. $this->DebugMessage('setting $this->config_temp_directory = $this->config_cache_directory ('.$this->config_cache_directory.')', __FILE__, __LINE__);
  873. $this->config_temp_directory = $this->config_cache_directory;
  874. }
  875. return true;
  876. }
  877. function ResolveFilenameToAbsolute($filename) {
  878. if (!$filename) {
  879. return false;
  880. }
  881. //if (preg_match('#^(f|ht)tps?\://#i', $filename)) {
  882. if (preg_match('#^[a-z0-9]+\:/{1,2}#i', $filename)) {
  883. // eg: http://host/path/file.jpg (HTTP URL)
  884. // eg: ftp://host/path/file.jpg (FTP URL)
  885. // eg: data1:/path/file.jpg (Netware path)
  886. //$AbsoluteFilename = $filename;
  887. return $filename;
  888. } elseif ($this->iswindows && ($filename{1} == ':')) {
  889. // absolute pathname (Windows)
  890. $AbsoluteFilename = $filename;
  891. } elseif ($this->iswindows && ((substr($filename, 0, 2) == '//') || (substr($filename, 0, 2) == '\\\\'))) {
  892. // absolute pathname (Windows)
  893. $AbsoluteFilename = $filename;
  894. } elseif ($filename{0} == '/') {
  895. if (@is_readable($filename) && !@is_readable($this->config_document_root.$filename)) {
  896. // absolute filename (*nix)
  897. $AbsoluteFilename = $filename;
  898. } elseif ($filename{1} == '~') {
  899. // /~user/path
  900. if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray($filename)) {
  901. $AbsoluteFilename = $ApacheLookupURIarray['filename'];
  902. } else {
  903. $AbsoluteFilename = realpath($filename);
  904. if (@is_readable($AbsoluteFilename)) {
  905. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.$filename.'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with realpath($filename)', __FILE__, __LINE__);
  906. } elseif (is_dir(dirname($AbsoluteFilename))) {
  907. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname($filename).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with realpath(.)', __FILE__, __LINE__);
  908. } else {
  909. 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")');
  910. }
  911. }
  912. } else {
  913. // relative filename (any OS)
  914. if (preg_match('#^'.preg_quote($this->config_document_root).'#', $filename)) {
  915. $AbsoluteFilename = $filename;
  916. $this->DebugMessage('ResolveFilenameToAbsolute() NOT prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__);
  917. } else {
  918. $AbsoluteFilename = $this->config_document_root.$filename;
  919. $this->DebugMessage('ResolveFilenameToAbsolute() prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__);
  920. }
  921. }
  922. } else {
  923. // relative to current directory (any OS)
  924. $AbsoluteFilename = $this->config_document_root.dirname(@$_SERVER['PHP_SELF']).DIRECTORY_SEPARATOR.$filename;
  925. //if (!@file_exists($AbsoluteFilename) && @file_exists(realpath($this->DotPadRelativeDirectoryPath($filename)))) {
  926. // $AbsoluteFilename = realpath($this->DotPadRelativeDirectoryPath($filename));
  927. //}
  928. if (substr(dirname(@$_SERVER['PHP_SELF']), 0, 2) == '/~') {
  929. if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) {
  930. $AbsoluteFilename = $ApacheLookupURIarray['filename'].DIRECTORY_SEPARATOR.$filename;
  931. } else {
  932. $AbsoluteFilename = realpath('.').DIRECTORY_SEPARATOR.$filename;
  933. if (@is_readable($AbsoluteFilename)) {
  934. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with realpath(.)/$filename', __FILE__, __LINE__);
  935. } elseif (is_dir(dirname($AbsoluteFilename))) {
  936. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with realpath(.)', __FILE__, __LINE__);
  937. } else {
  938. 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');
  939. }
  940. }
  941. }
  942. }
  943. if (is_link($AbsoluteFilename)) {
  944. $this->DebugMessage('is_link()==true, changing "'.$AbsoluteFilename.'" to "'.readlink($AbsoluteFilename).'"', __FILE__, __LINE__);
  945. $AbsoluteFilename = readlink($AbsoluteFilename);
  946. }
  947. if (realpath($AbsoluteFilename)) {
  948. $AbsoluteFilename = realpath($AbsoluteFilename);
  949. }
  950. if ($this->iswindows) {
  951. $AbsoluteFilename = preg_replace('#^'.preg_quote(realpath($this->config_document_root)).'#i', realpath($this->config_document_root), $AbsoluteFilename);
  952. $AbsoluteFilename = str_replace(DIRECTORY_SEPARATOR, '/', $AbsoluteFilename);
  953. }
  954. if (!$this->config_allow_src_above_docroot && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath($this->config_document_root))).'#', $AbsoluteFilename)) {
  955. $this->DebugMessage('!$this->config_allow_src_above_docroot therefore setting "'.$AbsoluteFilename.'" (outside "'.realpath($this->config_document_root).'") to null', __FILE__, __LINE__);
  956. return false;
  957. }
  958. if (!$this->config_allow_src_above_phpthumb && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__))).'#', $AbsoluteFilename)) {
  959. $this->DebugMessage('!$this->config_allow_src_above_phpthumb therefore setting "'.$AbsoluteFilename.'" (outside "'.dirname(__FILE__).'") to null', __FILE__, __LINE__);
  960. return false;
  961. }
  962. return $AbsoluteFilename;
  963. }
  964. function ImageMagickWhichConvert() {
  965. static $WhichConvert = null;
  966. if (is_null($WhichConvert)) {
  967. if ($this->iswindows) {
  968. $WhichConvert = false;
  969. } else {
  970. $WhichConvert = trim(phpthumb_functions::SafeExec('which convert'));
  971. }
  972. }
  973. return $WhichConvert;
  974. }
  975. function ImageMagickCommandlineBase() {
  976. static $commandline = null;
  977. if (is_null($commandline)) {
  978. if ($this->issafemode) {
  979. $commandline = '';
  980. return $commandline;
  981. }
  982. $commandline = (!is_null($this->config_imagemagick_path) ? $this->config_imagemagick_path : '');
  983. if ($this->config_imagemagick_path && ($this->config_imagemagick_path != realpath($this->config_imagemagick_path))) {
  984. if (@is_executable(realpath($this->config_imagemagick_path))) {
  985. $this->DebugMessage('Changing $this->config_imagemagick_path ('.$this->config_imagemagick_path.') to realpath($this->config_imagemagick_path) ('.realpath($this->config_imagemagick_path).')', __FILE__, __LINE__);
  986. $this->config_imagemagick_path = realpath($this->config_imagemagick_path);
  987. } else {
  988. $this->DebugMessage('Leaving $this->config_imagemagick_path as ('.$this->config_imagemagick_path.') because !is_execuatable(realpath($this->config_imagemagick_path)) ('.realpath($this->config_imagemagick_path).')', __FILE__, __LINE__);
  989. }
  990. }
  991. $this->DebugMessage(' file_exists('.$this->config_imagemagick_path.') = '.intval( @file_exists($this->config_imagemagick_path)), __FILE__, __LINE__);
  992. $this->DebugMessage('is_executable('.$this->config_imagemagick_path.') = '.intval(@is_executable($this->config_imagemagick_path)), __FILE__, __LINE__);
  993. if (@file_exists($this->config_imagemagick_path)) {
  994. $this->DebugMessage('using ImageMagick path from $this->config_imagemagick_path ('.$this->config_imagemagick_path.')', __FILE__, __LINE__);
  995. if ($this->iswindows) {
  996. $commandline = substr($this->config_imagemagick_path, 0, 2).' && cd "'.str_replace('/', DIRECTORY_SEPARATOR, substr(dirname($this->config_imagemagick_path), 2)).'" && '.basename($this->config_imagemagick_path);
  997. } else {
  998. $commandline = '"'.$this->config_imagemagick_path.'"';
  999. }
  1000. return $commandline;
  1001. }
  1002. $which_convert = $this->ImageMagickWhichConvert();
  1003. $IMversion = $this->ImageMagickVersion();
  1004. if ($which_convert && ($which_convert{0} == '/') && @file_exists($which_convert)) {
  1005. // `which convert` *should* return the path if "convert" exist, or nothing if it doesn't
  1006. // other things *may* get returned, like "sh: convert: not found" or "no convert in /usr/local/bin /usr/sbin /usr/bin /usr/ccs/bin"
  1007. // so only do this if the value returned exists as a file
  1008. $this->DebugMessage('using ImageMagick path from `which convert` ('.$which_convert.')', __FILE__, __LINE__);
  1009. $commandline = 'convert';
  1010. } elseif ($IMversion) {
  1011. $this->DebugMessage('setting ImageMagick path to $this->config_imagemagick_path ('.$this->config_imagemagick_path.') ['.$IMversion.']', __FILE__, __LINE__);
  1012. $commandline = $this->config_imagemagick_path;
  1013. } else {
  1014. $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__);
  1015. $commandline = '';
  1016. }
  1017. }
  1018. return $commandline;
  1019. }
  1020. function ImageMagickVersion($returnRAW=false) {
  1021. static $versionstring = null;
  1022. if (is_null($versionstring)) {
  1023. $commandline = $this->ImageMagickCommandlineBase();
  1024. $commandline = (!is_null($commandline) ? $commandline : '');
  1025. $versionstring = array(0=>'', 1=>'');
  1026. if ($commandline) {
  1027. $commandline .= ' --version';
  1028. $this->DebugMessage('ImageMagick version checked with "'.$commandline.'"', __FILE__, __LINE__);
  1029. $versionstring[1] = trim(phpthumb_functions::SafeExec($commandline));
  1030. if (preg_match('#^Version: [^0-9]*([ 0-9\\.\\:Q/\\-]+) (http|file)\:#i', $versionstring[1], $matches)) {
  1031. $versionstring[0] = $matches[1];
  1032. } else {
  1033. $versionstring[0] = false;
  1034. $this->DebugMessage('ImageMagick did not return recognized version string ('.$versionstring[1].')', __FILE__, __LINE__);
  1035. }
  1036. $this->DebugMessage('ImageMagick convert --version says "'.@$matches[0].'"', __FILE__, __LINE__);
  1037. }
  1038. }
  1039. return @$versionstring[intval($returnRAW)];
  1040. }
  1041. function ImageMagickSwitchAvailable($switchname) {
  1042. static $IMoptions = null;
  1043. if (is_null($IMoptions)) {
  1044. $IMoptions = array();
  1045. $commandline = $this->ImageMagickCommandlineBase();
  1046. if (!is_null($commandline)) {
  1047. $commandline .= ' -help';
  1048. $IMhelp_lines = explode("\n", phpthumb_functions::SafeExec($commandline));
  1049. foreach ($IMhelp_lines as $line) {
  1050. if (preg_match('#^[\+\-]([a-z\-]+) #', trim($line), $matches)) {
  1051. $IMoptions[$matches[1]] = true;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. if (is_array($switchname)) {
  1057. $allOK = true;
  1058. foreach ($switchname as $key => $value) {
  1059. if (!isset($IMoptions[$value])) {
  1060. $allOK = false;
  1061. break;
  1062. }
  1063. }
  1064. $this->DebugMessage('ImageMagickSwitchAvailable('.implode(';', $switchname).') = '.intval($allOK).'', __FILE__, __LINE__);
  1065. } else {
  1066. $allOK = isset($IMoptions[$switchname]);
  1067. $this->DebugMessage('ImageMagickSwitchAvailable('.$switchname.') = '.intval($allOK).'', __FILE__, __LINE__);
  1068. }
  1069. return $allOK;
  1070. }
  1071. function ImageMagickFormatsList() {
  1072. static $IMformatsList = null;
  1073. if (is_null($IMformatsList)) {
  1074. $IMformatsList = '';
  1075. $commandline = $this->ImageMagickCommandlineBase();
  1076. if (!is_null($commandline)) {
  1077. $commandline = dirname($commandline).DIRECTORY_SEPARATOR.str_replace('convert', 'identify', basename($commandline));
  1078. $commandline .= ' -list format';
  1079. $IMformatsList = phpthumb_functions::SafeExec($commandline);
  1080. }
  1081. }
  1082. return $IMformatsList;
  1083. }
  1084. function SourceDataToTempFile() {
  1085. if ($IMtempSourceFilename = $this->phpThumb_tempnam()) {
  1086. $IMtempSourceFilename = realpath($IMtempSourceFilename);
  1087. ob_start();
  1088. $fp_tempfile = fopen($IMtempSourceFilename, 'wb');
  1089. $tempfile_open_error = ob_get_contents();
  1090. ob_end_clean();
  1091. if ($fp_tempfile) {
  1092. fwrite($fp_tempfile, $this->rawImageData);
  1093. fclose($fp_tempfile);
  1094. $this->sourceFilename = $IMtempSourceFilename;
  1095. $this->DebugMessage('ImageMagickThumbnailToGD() setting $this->sourceFilename to "'.$IMtempSourceFilename.'" from $this->rawImageData ('.strlen($this->rawImageData).' bytes)', __FILE__, __LINE__);
  1096. } else {
  1097. $this->DebugMessage('ImageMagickThumbnailToGD() FAILED setting $this->sourceFilename to "'.$IMtempSourceFilename.'" (failed to open for writing: "'.$tempfile_open_error.'")', __FILE__, __LINE__);
  1098. }
  1099. unset($tempfile_open_error, $IMtempSourceFilename);
  1100. return true;
  1101. }
  1102. $this->DebugMessage('SourceDataToTempFile() FAILED because $this->phpThumb_tempnam() failed', __FILE__, __LINE__);
  1103. return false;
  1104. }
  1105. function ImageMagickThumbnailToGD() {
  1106. // http://www.imagemagick.org/script/command-line-options.php
  1107. $this->useRawIMoutput = true;
  1108. if (phpthumb_functions::gd_version()) {
  1109. // if GD is not available, must use whatever ImageMagick can output
  1110. // $UnAllowedParameters contains options that can only be processed in GD, not ImageMagick
  1111. // note: 'fltr' *may* need to be processed by GD, but we'll check that in more detail below
  1112. $UnAllowedParameters = array('xto', 'ar', 'bg', 'bc');
  1113. // 'ra' may be part of this list, if not a multiple of 90°
  1114. foreach ($UnAllowedParameters as $parameter) {
  1115. if (isset($this->$parameter)) {
  1116. $this->DebugMessage('$this->useRawIMoutput=false because "'.$parameter.'" is set', __FILE__, __LINE__);
  1117. $this->useRawIMoutput = false;
  1118. break;
  1119. }
  1120. }
  1121. }
  1122. $this->DebugMessage('$this->useRawIMoutput='.($this->useRawIMoutput ? 'true' : 'false').' after checking $UnAllowedParameters', __FILE__, __LINE__);
  1123. $outputFormat = $this->thumbnailFormat;
  1124. if (phpthumb_functions::gd_version()) {
  1125. if ($this->useRawIMoutput) {
  1126. switch ($this->thumbnailFormat) {
  1127. case 'gif':
  1128. $ImageCreateFunction = 'ImageCreateFromGIF';
  1129. $this->is_alpha = true;
  1130. break;
  1131. case 'png':
  1132. $ImageCreateFunction = 'ImageCreateFromPNG';
  1133. $this->is_alpha = true;
  1134. break;
  1135. case 'jpg':
  1136. case 'jpeg':
  1137. $ImageCreateFunction = 'ImageCreateFromJPEG';
  1138. break;
  1139. default:
  1140. $this->DebugMessage('Forcing output to PNG because $this->thumbnailFormat ('.$this->thumbnailFormat.' is not a GD-supported format)', __FILE__, __LINE__);
  1141. $outputFormat = 'png';
  1142. $ImageCreateFunction = 'ImageCreateFromPNG';
  1143. $this->is_alpha = true;
  1144. $this->useRawIMoutput = false;
  1145. break;
  1146. }
  1147. if (!function_exists(@$ImageCreateFunction)) {
  1148. // ImageMagickThumbnailToGD() depends on ImageCreateFromPNG/ImageCreateFromGIF
  1149. //$this->DebugMessage('ImageMagickThumbnailToGD() aborting because '.@$ImageCreateFunction.'() is not available', __FILE__, __LINE__);
  1150. $this->useRawIMoutput = true;
  1151. //return false;
  1152. }
  1153. } else {
  1154. $outputFormat = 'png';
  1155. $ImageCreateFunction = 'ImageCreateFromPNG';
  1156. $this->is_alpha = true;
  1157. $this->useRawIMoutput = false;
  1158. }
  1159. }
  1160. // http://freealter.org/doc_distrib/ImageMagick-5.1.1/www/convert.html
  1161. if (!$this->sourceFilename && $this->rawImageData) {
  1162. $this->SourceDataToTempFile();
  1163. }
  1164. if (!$this->sourceFilename) {
  1165. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because $this->sourceFilename is empty', __FILE__, __LINE__);
  1166. $this->useRawIMoutput = false;
  1167. return false;
  1168. }
  1169. if ($this->issafemode) {
  1170. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because safe_mode is enabled', __FILE__, __LINE__);
  1171. $this->useRawIMoutput = false;
  1172. return false;
  1173. }
  1174. $commandline = $this->ImageMagickCommandlineBase();
  1175. if ($commandline) {
  1176. if ($IMtempfilename = $this->phpThumb_tempnam()) {
  1177. $IMtempfilename = realpath($IMtempfilename);
  1178. $IMuseExplicitImageOutputDimensions = false;
  1179. if ($this->ImageMagickSwitchAvailable('thumbnail') && $this->config_imagemagick_use_thumbnail) {
  1180. $IMresizeParameter = 'thumbnail';
  1181. } else {
  1182. $IMresizeParameter = 'resize';
  1183. // some (older? around 2002) versions of IM won't accept "-resize 100x" but require "-resize 100x100"
  1184. $commandline_test = $this->ImageMagickCommandlineBase().' logo: -resize 1x "'.$IMtempfilename.'" 2>&1';
  1185. $IMresult_test = phpthumb_functions::SafeExec($commandline_test);
  1186. $IMuseExplicitImageOutputDimensions = preg_match('#image dimensions are zero#i', $IMresult_test);
  1187. $this->DebugMessage('IMuseExplicitImageOutputDimensions = '.intval($IMuseExplicitImageOutputDimensions), __FILE__, __LINE__);
  1188. if ($fp_im_temp = @fopen($IMtempfilename, 'wb')) {
  1189. // erase temp image so ImageMagick logo doesn't get output if other processing fails
  1190. fclose($fp_im_temp);
  1191. }
  1192. }
  1193. if (!is_null($this->dpi) && $this->ImageMagickSwitchAvailable('density')) {
  1194. // for raster source formats only (WMF, PDF, etc)
  1195. $commandline .= ' -density '.$this->dpi;
  1196. }
  1197. ob_start();
  1198. $getimagesize = GetImageSize($this->sourceFilename);
  1199. $GetImageSizeError = ob_get_contents();
  1200. ob_end_clean();
  1201. if (is_array($getimagesize)) {
  1202. $this->DebugMessage('GetImageSize('.$this->sourceFilename.') SUCCEEDED: '.print_r($getimagesize, true), __FILE__, __LINE__);
  1203. } else {
  1204. $this->DebugMessage('GetImageSize('.$this->sourceFilename.') FAILED with error "'.$GetImageSizeError.'"', __FILE__, __LINE__);
  1205. }
  1206. if (is_array($getimagesize)) {
  1207. $this->DebugMessage('GetImageSize('.$this->sourceFilename.') returned [w='.$getimagesize[0].';h='.$getimagesize[1].';f='.$getimagesize[2].']', __FILE__, __LINE__);
  1208. $this->source_width = $getimagesize[0];
  1209. $this->source_height = $getimagesize[1];
  1210. $this->DebugMessage('source dimensions set to '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__);
  1211. $this->SetOrientationDependantWidthHeight();
  1212. if (!preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat)) {
  1213. // not a transparency-capable format
  1214. $commandline .= ' -background "#'.($this->bg ? $this->bg : 'FFFFFF').'"';
  1215. if ($getimagesize[2] == IMAGETYPE_GIF) {
  1216. $commandline .= ' -flatten';
  1217. }
  1218. }
  1219. if ($getimagesize[2] == IMAGETYPE_GIF) {
  1220. $commandline .= ' -coalesce'; // may be needed for animated GIFs
  1221. }
  1222. if ($this->source_width || $this->source_height) {
  1223. if ($this->zc) {
  1224. $borderThickness = 0;
  1225. if (!empty($this->fltr)) {
  1226. foreach ($this->fltr as $key => $value) {
  1227. if (preg_match('#^bord\|([0-9]+)#', $value, $matches)) {
  1228. $borderThickness = $matches[1];
  1229. break;
  1230. }
  1231. }
  1232. }
  1233. $wAll = intval(max($this->w, $this->wp, $this->wl, $this->ws)) - (2 * $borderThickness);
  1234. $hAll = intval(max($this->h, $this->hp, $this->hl, $this->hs)) - (2 * $borderThickness);
  1235. $imAR = $this->source_width / $this->source_height;
  1236. $zcAR = (($wAll && $hAll) ? $wAll / $hAll : 1);
  1237. $side = phpthumb_functions::nonempty_min($this->source_width, $this->source_height, max($wAll, $hAll));
  1238. $sideX = phpthumb_functions::nonempty_min($this->source_width, $wAll, round($hAll * $zcAR));
  1239. $sideY = phpthumb_functions::nonempty_min( $this->source_height, $hAll, round($wAll / $zcAR));
  1240. $thumbnailH = round(max($sideY, ($sideY * $zcAR) / $imAR));
  1241. if ($IMuseExplicitImageOutputDimensions) {
  1242. $commandline .= ' -'.$IMresizeParameter.' '.$thumbnailH.'x'.$thumbnailH;
  1243. } else {
  1244. $commandline .= ' -'.$IMresizeParameter.' x'.$thumbnailH;
  1245. }
  1246. switch (strtoupper($this->zc)) {
  1247. case 'T':
  1248. $commandline .= ' -gravity north';
  1249. break;
  1250. case 'B':
  1251. $commandline .= ' -gravity south';
  1252. break;
  1253. case 'L':
  1254. $commandline .= ' -gravity west';
  1255. break;
  1256. case 'R':
  1257. $commandline .= ' -gravity east';
  1258. break;
  1259. case 'TL':
  1260. $commandline .= ' -gravity northwest';
  1261. break;
  1262. case 'TR':
  1263. $commandline .= ' -gravity northeast';
  1264. break;
  1265. case 'BL':
  1266. $commandline .= ' -gravity southwest';
  1267. break;
  1268. case 'BR':
  1269. $commandline .= ' -gravity southeast';
  1270. break;
  1271. case '1':
  1272. case 'C':
  1273. default:
  1274. $commandline .= ' -gravity center';
  1275. break;
  1276. }
  1277. if (($wAll > 0) && ($hAll > 0)) {
  1278. $commandline .= ' -crop '.$wAll.'x'.$hAll.'+0+0';
  1279. } else {
  1280. $commandline .= ' -crop '.$side.'x'.$side.'+0+0';
  1281. }
  1282. if ($this->ImageMagickSwitchAvailable('repage')) {
  1283. $commandline .= ' +repage';
  1284. } else {
  1285. $this->DebugMessage('Skipping "+repage" because ImageMagick (v'.$this->ImageMagickVersion().') does not support it', __FILE__, __LINE__);
  1286. }
  1287. } elseif ($this->sw || $this->sh || $this->sx || $this->sy) {
  1288. $commandline .= ' -crop '.($this->sw ? $this->sw : $this->source_width).'x'.($this->sh ? $this->sh : $this->source_height).'+'.$this->sx.'+'.$this->sy;
  1289. // this is broken for aoe=1, but unsure how to fix. Send advice to info@silisoftware.com
  1290. if ($this->w || $this->h) {
  1291. if ($this->ImageMagickSwitchAvailable('repage')) {
  1292. $commandline .= ' -repage';
  1293. } else {
  1294. $this->DebugMessage('Skipping "-repage" because ImageMagick (v'.$this->ImageMagickVersion().') does not support it', __FILE__, __LINE__);
  1295. }
  1296. if ($IMuseExplicitImageOutputDimensions) {
  1297. if ($this->w && !$this->h) {
  1298. $this->h = ceil($this->w / ($this->source_width / $this->source_height));
  1299. } elseif ($this->h && !$this->w) {
  1300. $this->w = ceil($this->h * ($this->source_width / $this->source_height));
  1301. }
  1302. }
  1303. $commandline .= ' -'.$IMresizeParameter.' '.$this->w.'x'.$this->h;
  1304. }
  1305. } else {
  1306. if ($this->iar && (intval($this->w) > 0) && (intval($this->h) > 0)) {
  1307. list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra);
  1308. $nw = ((round($nw) != 0) ? round($nw) : '');
  1309. $nh = ((round($nh) != 0) ? round($nh) : '');
  1310. $commandline .= ' -'.$IMresizeParameter.' '.$nw.'x'.$nh.'!';
  1311. } else {
  1312. $this->w = ((($this->aoe || $this->far) && $this->w) ? $this->w : ($this->w ? phpthumb_functions::nonempty_min($this->w, $getimagesize[0]) : ''));
  1313. $this->h = ((($this->aoe || $this->far) && $this->h) ? $this->h : ($this->h ? phpthumb_functions::nonempty_min($this->h, $getimagesize[1]) : ''));
  1314. if ($this->w || $this->h) {
  1315. if ($IMuseExplicitImageOutputDimensions) {
  1316. if ($this->w && !$this->h) {
  1317. $this->h = ceil($this->w / ($this->source_width / $this->source_height));
  1318. } elseif ($this->h && !$this->w) {
  1319. $this->w = ceil($this->h * ($this->source_width / $this->source_height));
  1320. }
  1321. }
  1322. list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra);
  1323. $nw = ((round($nw) != 0) ? round($nw) : '');
  1324. $nh = ((round($nh) != 0) ? round($nh) : '');
  1325. $commandline .= ' -'.$IMresizeParameter.' '.$nw.'x'.$nh;
  1326. }
  1327. }
  1328. }
  1329. }
  1330. } else {
  1331. $this->DebugMessage('GetImageSize('.$this->sourceFilename.') failed', __FILE__, __LINE__);
  1332. if ($this->w || $this->h) {
  1333. if ($IMuseExplicitImageOutputDimensions) {
  1334. // unknown source aspect ration, just put large number and hope IM figures it out
  1335. $commandline .= ' -'.$IMresizeParameter.' '.($this->w ? $this->w : '9999').'x'.($this->h ? $this->h : '9999');
  1336. } else {
  1337. $commandline .= ' -'.$IMresizeParameter.' '.$this->w.'x'.$this->h;
  1338. }
  1339. if ($this->iar && (intval($this->w) > 0) && (intval($this->h) > 0)) {
  1340. $commandline .= '!';
  1341. }
  1342. }
  1343. }
  1344. if ($this->ra) {
  1345. $this->ra = intval($this->ra);
  1346. if ($this->ImageMagickSwitchAvailable('rotate')) {
  1347. if (!eregi('('.implode('|', $this->AlphaCapableFormats).')', $outputFormat) || phpthumb_functions::version_compare_replacement($this->ImageMagickVersion(), '6.3.7', '>=')) {
  1348. $this->DebugMessage('Using ImageMagick rotate', __FILE__, __LINE__);
  1349. $commandline .= ' -rotate '.$this->ra;
  1350. if (($this->ra % 90) != 0) {
  1351. if (eregi('('.implode('|', $this->AlphaCapableFormats).')', $outputFormat)) {
  1352. // alpha-capable format
  1353. $commandline .= ' -background rgba(255,255,255,0)';
  1354. } else {
  1355. $commandline .= ' -background "#'.($this->bg ? $this->bg : 'FFFFFF').'"';
  1356. }
  1357. }
  1358. $this->ra = 0;
  1359. } else {
  1360. $this->DebugMessage('Not using ImageMagick rotate because alpha background buggy before v6.3.7', __FILE__, __LINE__);
  1361. }
  1362. } else {
  1363. $this->DebugMessage('Not using ImageMagick rotate because not supported', __FILE__, __LINE__);
  1364. }
  1365. }
  1366. $successfullyProcessedFilters = array();
  1367. foreach ($this->fltr as $filterkey => $filtercommand) {
  1368. @list($command, $parameter) = explode('|', $filtercommand, 2);
  1369. switch ($command) {
  1370. case 'brit':
  1371. if ($this->ImageMagickSwitchAvailable('modulate')) {
  1372. $commandline .= ' -modulate '.(100 + intval($parameter)).',100,100';
  1373. $successfullyProcessedFilters[] = $filterkey;
  1374. }
  1375. break;
  1376. case 'cont':
  1377. if ($this->ImageMagickSwitchAvailable('contrast')) {
  1378. $contDiv10 = round(intval($parameter) / 10);
  1379. if ($contDiv10 > 0) {
  1380. $contDiv10 = min($contDiv10, 100);
  1381. for ($i = 0; $i < $contDiv10; $i++) {
  1382. $commandline .= ' -contrast'; // increase contrast by 10%
  1383. }
  1384. } elseif ($contDiv10 < 0) {
  1385. $contDiv10 = max($contDiv10, -100);
  1386. for ($i = $contDiv10; $i < 0; $i++) {
  1387. $commandline .= ' +contrast'; // decrease contrast by 10%
  1388. }
  1389. } else {
  1390. // do nothing
  1391. }
  1392. $successfullyProcessedFilters[] = $filterkey;
  1393. }
  1394. break;
  1395. case 'ds':
  1396. if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
  1397. if ($parameter == 100) {
  1398. $commandline .= ' -colorspace GRAY -modulate 100,0,100';
  1399. } else {
  1400. $commandline .= ' -modulate 100,'.(100 - intval($parameter)).',100';
  1401. }
  1402. $successfullyProcessedFilters[] = $filterkey;
  1403. }
  1404. break;
  1405. case 'sat':
  1406. if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
  1407. if ($parameter == -100) {
  1408. $commandline .= ' -colorspace GRAY -modulate 100,0,100';
  1409. } else {
  1410. $commandline .= ' -modulate 100,'.(100 + intval($parameter)).',100';
  1411. }
  1412. $successfullyProcessedFilters[] = $filterkey;
  1413. }
  1414. break;
  1415. case 'gray':
  1416. if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
  1417. $commandline .= ' -colorspace GRAY -modulate 100,0,100';
  1418. //$commandline .= ' -colorspace GRAY';
  1419. $successfullyProcessedFilters[] = $filterkey;
  1420. }
  1421. break;
  1422. case 'clr':
  1423. if ($this->ImageMagickSwitchAvailable(array('fill', 'colorize'))) {
  1424. @list($amount, $color) = explode('|', $parameter);
  1425. $commandline .= ' -fill '.escapeshellarg('#'.$color).' -colorize '.min(max(intval($amount), 0), 100);
  1426. }
  1427. break;
  1428. case 'sep':
  1429. if ($this->ImageMagickSwitchAvailable('sepia-tone')) {
  1430. @list($amount, $color) = explode('|', $parameter);
  1431. $amount = ($amount ? $amount : 80);
  1432. if (!$color) {
  1433. $commandline .= ' -sepia-tone '.min(max(intval($amount), 0), 100).'%';
  1434. $successfullyProcessedFilters[] = $filterkey;
  1435. }
  1436. }
  1437. break;
  1438. case 'gam':
  1439. if ($this->ImageMagickSwitchAvailable('gamma')) {
  1440. $commandline .= ' -gamma '.min(max(floatval($amount), -10), 10);
  1441. $successfullyProcessedFilters[] = $filterkey;
  1442. }
  1443. break;
  1444. case 'neg':
  1445. if ($this->ImageMagickSwitchAvailable('negate')) {
  1446. $commandline .= ' -negate';
  1447. $successfullyProcessedFilters[] = $filterkey;
  1448. }
  1449. break;
  1450. case 'th':
  1451. if ($this->ImageMagickSwitchAvailable(array('threshold', 'dither', 'monochrome'))) {
  1452. $commandline .= ' -threshold '.round(min(max(intval($amount), 0), 255) / 2.55).'% -dither -monochrome';
  1453. $successfullyProcessedFilters[] = $filterkey;
  1454. }
  1455. break;
  1456. case 'rcd':
  1457. if ($this->ImageMagickSwitchAvailable(array('colors', 'dither'))) {
  1458. @list($colors, $dither) = explode('|', $parameter);
  1459. $colors = ($colors ? (int) $colors : 256);
  1460. $dither = ((strlen($dither) > 0) ? (bool) $dither : true);
  1461. $commandline .= ' -colors '.max($colors, 8); // ImageMagick will otherwise fail with "cannot quantize to fewer than 8 colors"
  1462. $commandline .= ($dither ? ' -dither' : ' +dither');
  1463. $successfullyProcessedFilters[] = $filterkey;
  1464. }
  1465. break;
  1466. case 'flip':
  1467. if ($this->ImageMagickSwitchAvailable(array('flip', 'flop'))) {
  1468. if (strpos(strtolower($parameter), 'x') !== false) {
  1469. $commandline .= ' -flop';
  1470. }
  1471. if (strpos(strtolower($parameter), 'y') !== false) {
  1472. $commandline .= ' -flip';
  1473. }
  1474. $successfullyProcessedFilters[] = $filterkey;
  1475. }
  1476. break;
  1477. case 'edge':
  1478. if ($this->ImageMagickSwitchAvailable('edge')) {
  1479. $parameter = (!empty($parameter) ? $parameter : 2);
  1480. $commandline .= ' -edge '.(!empty($parameter) ? intval($parameter) : 1);
  1481. $successfullyProcessedFilters[] = $filterkey;
  1482. }
  1483. break;
  1484. case 'emb':
  1485. if ($this->ImageMagickSwitchAvailable(array('emboss', 'negate'))) {
  1486. $parameter = (!empty($parameter) ? $parameter : 2);
  1487. $commandline .= ' -emboss '.intval($parameter);
  1488. if ($parameter < 2) {
  1489. $commandline .= ' -negate'; // ImageMagick negates the image for some reason with '-emboss 1';
  1490. }
  1491. $successfullyProcessedFilters[] = $filterkey;
  1492. }
  1493. break;
  1494. case 'lvl':
  1495. @list($band, $method, $threshold) = explode('|', $parameter);
  1496. $band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*');
  1497. $method = ((strlen($method) > 0) ? intval($method) : 2);
  1498. $threshold = ((strlen($threshold) > 0) ? min(max(floatval($threshold), 0), 100) : 0.1);
  1499. $band = preg_replace('#[^RGBA\\*]#', '', strtoupper($band));
  1500. if (($method > 1) && !$this->ImageMagickSwitchAvailable(array('channel', 'contrast-stretch'))) {
  1501. // Because ImageMagick processing happens before PHP-GD filters, and because some
  1502. // clipping is involved in the "lvl" filter, if "lvl" happens before "wb" then the
  1503. // "wb" filter will have (almost) no effect. Therefore, if "wb" is enabled then
  1504. // force the "lvl" filter to be processed by GD, not ImageMagick.
  1505. foreach ($this->fltr as $fltr_key => $fltr_value) {
  1506. list($fltr_cmd) = explode('|', $fltr_value);
  1507. if ($fltr_cmd == 'wb') {
  1508. $this->DebugMessage('Setting "lvl" filter method to "0" (from "'.$method.'") because white-balance filter also enabled', __FILE__, __LINE__);
  1509. $method = 0;
  1510. }
  1511. }
  1512. }
  1513. switch ($method) {
  1514. case 0: // internal RGB
  1515. case 1: // internal grayscale
  1516. break;
  1517. case 2: // ImageMagick "contrast-stretch"
  1518. if ($this->ImageMagickSwitchAvailable('contrast-stretch')) {
  1519. $thiscommand = ' -contrast-stretch '.$threshold.'%';
  1520. $commandline .= (($band == '*') ? $thiscommand : ' -channel '.strtoupper($band).$thiscommand.' +channel');
  1521. $successfullyProcessedFilters[] = $filterkey;
  1522. }
  1523. break;
  1524. case 3: // ImageMagick "normalize"
  1525. if ($this->ImageMagickSwitchAvailable('normalize')) {
  1526. $thiscommand = ' -normalize';
  1527. $commandline .= (($band == '*') ? $thiscommand : ' -channel '.strtoupper($band).$thiscommand.' +channel');
  1528. $successfullyProcessedFilters[] = $filterkey;
  1529. }
  1530. break;
  1531. default:
  1532. $this->DebugMessage('unsupported method ('.$method.') for "lvl" filter', __FILE__, __LINE__);
  1533. break;
  1534. }
  1535. if (isset($this->fltr[$filterkey]) && ($method > 1)) {
  1536. $this->fltr[$filterkey] = $command.'|'.$band.'|0|'.$threshold;
  1537. $this->DebugMessage('filter "lvl" remapped from method "'.$method.'" to method "0" because ImageMagick support is missing', __FILE__, __LINE__);
  1538. }
  1539. break;
  1540. case 'wb':
  1541. if ($this->ImageMagickSwitchAvailable(array('channel', 'contrast-stretch'))) {
  1542. @list($threshold) = explode('|', $parameter);
  1543. $threshold = (!empty($threshold) ? min(max(floatval($threshold), 0), 100) : 0.1);
  1544. $commandline .= ' -channel R -contrast-stretch '.$threshold.'%';
  1545. $commandline .= ' -channel G -contrast-stretch '.$threshold.'%';
  1546. $commandline .= ' -channel B -contrast-stretch '.$threshold.'%';
  1547. $commandline .= ' +channel';
  1548. $successfullyProcessedFilters[] = $filterkey;
  1549. }
  1550. break;
  1551. case 'blur':
  1552. if ($this->ImageMagickSwitchAvailable('blur')) {
  1553. @list($radius) = explode('|', $parameter);
  1554. $radius = (!empty($radius) ? min(max(intval($radius), 0), 25) : 1);
  1555. $commandline .= ' -blur '.$radius;
  1556. $successfullyProcessedFilters[] = $filterkey;
  1557. }
  1558. break;
  1559. case 'gblr':
  1560. if ($this->ImageMagickSwitchAvailable('gaussian')) {
  1561. @list($radius) = explode('|', $parameter);
  1562. $radius = (!empty($radius) ? min(max(intval($radius), 0), 25) : 1);
  1563. $commandline .= ' -gaussian '.$radius;
  1564. $successfullyProcessedFilters[] = $filterkey;
  1565. }
  1566. break;
  1567. case 'usm':
  1568. if ($this->ImageMagickSwitchAvailable('unsharp')) {
  1569. @list($amount, $radius, $threshold) = explode('|', $parameter);
  1570. $amount = ($amount ? min(max(intval($radius), 0), 255) : 80);
  1571. $radius = ($radius ? min(max(intval($radius), 0), 10) : 0.5);
  1572. $threshold = (strlen($threshold) ? min(max(intval($radius), 0), 50) : 3);
  1573. $commandline .= ' -unsharp '.number_format(($radius * 2) - 1, 2).'x1+'.number_format($amount / 100, 2).'+'.number_format($threshold / 100, 2);
  1574. $successfullyProcessedFilters[] = $filterkey;
  1575. }
  1576. break;
  1577. case 'bord':
  1578. if ($this->ImageMagickSwitchAvailable(array('border', 'bordercolor', 'thumbnail', 'crop'))) {
  1579. if (!$this->zc) {
  1580. @list($width, $rX, $rY, $color) = explode('|', $parameter);
  1581. $width = intval($width);
  1582. $rX = intval($rX);
  1583. $rY = intval($rY);
  1584. if ($width && !$rX && !$rY) {
  1585. if (!phpthumb_functions::IsHexColor($color)) {
  1586. $color = ($this->bc ? $this->bc : '000000');
  1587. }
  1588. $commandline .= ' -border '.intval($width).' -bordercolor '.escapeshellarg('#'.$color);
  1589. if (preg_match('# \-crop ([0-9]+)x([0-9]+)\+0\+0 #', $commandline, $matches)) {
  1590. $commandline = str_replace(' -crop '.$matches[1].'x'.$matches[2].'+0+0 ', ' -crop '.($matches[1] - (2 * $width)).'x'.($matches[2] - (2 * $width)).'+0+0 ', $commandline);
  1591. } elseif (preg_match('# \-'.$IMresizeParameter.' ([0-9]+)x([0-9]+) #', $commandline, $matches)) {
  1592. $commandline = str_replace(' -'.$IMresizeParameter.' '.$matches[1].'x'.$matches[2].' ', ' -'.$IMresizeParameter.' '.($matches[1] - (2 * $width)).'x'.($matches[2] - (2 * $width)).' ', $commandline);
  1593. }
  1594. $successfullyProcessedFilters[] = $filterkey;
  1595. }
  1596. }
  1597. }
  1598. break;
  1599. case 'crop':
  1600. break;
  1601. case 'sblr':
  1602. break;
  1603. case 'mean':
  1604. break;
  1605. case 'smth':
  1606. break;
  1607. case 'bvl':
  1608. break;
  1609. case 'wmi':
  1610. break;
  1611. case 'wmt':
  1612. break;
  1613. case 'over':
  1614. break;
  1615. case 'hist':
  1616. break;
  1617. case 'fram':
  1618. break;
  1619. case 'drop':
  1620. break;
  1621. case 'mask':
  1622. break;
  1623. case 'elip':
  1624. break;
  1625. case 'ric':
  1626. break;
  1627. case 'stc':
  1628. break;
  1629. case 'size':
  1630. break;
  1631. default:
  1632. $this->DebugMessage('Unknown $this->fltr['.$filterkey.'] ('.$filtercommand.') -- deleting filter command', __FILE__, __LINE__);
  1633. $successfullyProcessedFilters[] = $filterkey;
  1634. break;
  1635. }
  1636. if (!isset($this->fltr[$filterkey])) {
  1637. $this->DebugMessage('Processed $this->fltr['.$filterkey.'] ('.$filtercommand.') with ImageMagick', __FILE__, __LINE__);
  1638. } else {
  1639. $this->DebugMessage('Skipping $this->fltr['.$filterkey.'] ('.$filtercommand.') with ImageMagick', __FILE__, __LINE__);
  1640. }
  1641. }
  1642. $this->DebugMessage('Remaining $this->fltr after ImageMagick: ('.$this->phpThumbDebugVarDump($this->fltr).')', __FILE__, __LINE__);
  1643. if (count($this->fltr) > 0) {
  1644. $this->useRawIMoutput = false;
  1645. }
  1646. if (preg_match('#jpe?g#i', $outputFormat) && $this->q) {
  1647. if ($this->ImageMagickSwitchAvailable(array('quality', 'interlace'))) {
  1648. $commandline .= ' -quality '.$this->thumbnailQuality;
  1649. if ($this->config_output_interlace) {
  1650. // causes weird things with animated GIF... leave for JPEG only
  1651. $commandline .= ' -interlace line '; // Use Line or Plane to create an interlaced PNG or GIF or progressive JPEG image
  1652. }
  1653. }
  1654. }
  1655. $commandline .= ' "'.str_replace('/', DIRECTORY_SEPARATOR, $this->sourceFilename).(($outputFormat == 'gif') ? '' : '['.intval($this->sfn).']').'"'; // [0] means first frame of (GIF) animation, can be ignored
  1656. $commandline .= ' '.$outputFormat.':"'.$IMtempfilename.'"';
  1657. if (!$this->iswindows) {
  1658. $commandline .= ' 2>&1';
  1659. }
  1660. $this->DebugMessage('ImageMagick called as ('.$commandline.')', __FILE__, __LINE__);
  1661. $IMresult = phpthumb_functions::SafeExec($commandline);
  1662. clearstatcache();
  1663. if (@$IMtempSourceFilename && file_exists($IMtempSourceFilename)) {
  1664. $this->DebugMessage('deleting "'.$IMtempSourceFilename.'"', __FILE__, __LINE__);
  1665. @unlink($IMtempSourceFilename);
  1666. }
  1667. if (!@file_exists($IMtempfilename) || !@filesize($IMtempfilename)) {
  1668. $this->FatalError('ImageMagick failed with message ('.trim($IMresult).')');
  1669. $this->DebugMessage('ImageMagick failed with message ('.trim($IMresult).')', __FILE__, __LINE__);
  1670. if ($this->iswindows && !$IMresult) {
  1671. $this->DebugMessage('Check to make sure that PHP has read+write permissions to "'.dirname($IMtempfilename).'"', __FILE__, __LINE__);
  1672. }
  1673. } else {
  1674. foreach ($successfullyProcessedFilters as $dummy => $filterkey) {
  1675. unset($this->fltr[$filterkey]);
  1676. }
  1677. $this->IMresizedData = file_get_contents($IMtempfilename);
  1678. $getimagesize_imresized = @GetImageSize($IMtempfilename);
  1679. $this->DebugMessage('GetImageSize('.$IMtempfilename.') returned [w='.$getimagesize_imresized[0].';h='.$getimagesize_imresized[1].';f='.$getimagesize_imresized[2].']', __FILE__, __LINE__);
  1680. if (($this->config_max_source_pixels > 0) && (($getimagesize_imresized[0] * $getimagesize_imresized[1]) > $this->config_max_source_pixels)) {
  1681. $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__);
  1682. } elseif (function_exists(@$ImageCreateFunction) && ($this->gdimg_source = @$ImageCreateFunction($IMtempfilename))) {
  1683. $this->source_width = ImageSX($this->gdimg_source);
  1684. $this->source_height = ImageSY($this->gdimg_source);
  1685. $this->DebugMessage('ImageMagickThumbnailToGD::'.$ImageCreateFunction.'() succeeded, $this->gdimg_source is now ('.$this->source_width.'x'.$this->source_height.')', __FILE__, __LINE__);
  1686. $this->DebugMessage('ImageMagickThumbnailToGD() returning $this->IMresizedData ('.strlen($this->IMresizedData).' bytes)', __FILE__, __LINE__);
  1687. } else {
  1688. $this->useRawIMoutput = true;
  1689. $this->DebugMessage('$this->useRawIMoutput set to TRUE because '.@$ImageCreateFunction.'('.$IMtempfilename.') failed', __FILE__, __LINE__);
  1690. }
  1691. $this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__);
  1692. @unlink($IMtempfilename);
  1693. return true;
  1694. }
  1695. $this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__);
  1696. unlink($IMtempfilename);
  1697. } elseif ($this->issafemode) {
  1698. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because PHP safe_mode is enabled and phpThumb_tempnam() failed', __FILE__, __LINE__);
  1699. $this->useRawIMoutput = false;
  1700. } else {
  1701. $this->DebugMessage('ImageMagickThumbnailToGD() aborting, phpThumb_tempnam() failed', __FILE__, __LINE__);
  1702. }
  1703. } else {
  1704. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because ImageMagickCommandlineBase() failed', __FILE__, __LINE__);
  1705. }
  1706. $this->useRawIMoutput = false;
  1707. return false;
  1708. }
  1709. function Rotate() {
  1710. if ($this->ra || $this->ar) {
  1711. if (!function_exists('ImageRotate')) {
  1712. $this->DebugMessage('!function_exists(ImageRotate)', __FILE__, __LINE__);
  1713. return false;
  1714. }
  1715. if (!include_once(dirname(__FILE__).'/phpthumb.filters.php')) {
  1716. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.filters.php" which is required for applying filters ('.implode(';', $this->fltr).')', __FILE__, __LINE__);
  1717. return false;
  1718. }
  1719. $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
  1720. if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
  1721. return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
  1722. }
  1723. $rotate_angle = 0;
  1724. if ($this->ra) {
  1725. $rotate_angle = floatval($this->ra);
  1726. } else {
  1727. if ($this->ar == 'x') {
  1728. if (phpthumb_functions::version_compare_replacement(phpversion(), '4.2.0', '>=')) {
  1729. if ($this->sourceFilename) {
  1730. if (function_exists('exif_read_data')) {
  1731. if ($exif_data = @exif_read_data($this->sourceFilename, 'IFD0')) {
  1732. // http://sylvana.net/jpegcrop/exif_orientation.html
  1733. switch (@$exif_data['Orientation']) {
  1734. case 1:
  1735. $rotate_angle = 0;
  1736. break;
  1737. case 3:
  1738. $rotate_angle = 180;
  1739. break;
  1740. case 6:
  1741. $rotate_angle = 270;
  1742. break;
  1743. case 8:
  1744. $rotate_angle = 90;
  1745. break;
  1746. default:
  1747. $this->DebugMessage('EXIF auto-rotate failed because unknown $exif_data[Orientation] "'.@$exif_data['Orientation'].'"', __FILE__, __LINE__);
  1748. return false;
  1749. break;
  1750. }
  1751. $this->DebugMessage('EXIF auto-rotate set to '.$rotate_angle.' degrees ($exif_data[Orientation] = "'.@$exif_data['Orientation'].'")', __FILE__, __LINE__);
  1752. } else {
  1753. $this->DebugMessage('failed: exif_read_data('.$this->sourceFilename.')', __FILE__, __LINE__);
  1754. return false;
  1755. }
  1756. } else {
  1757. $this->DebugMessage('!function_exists(exif_read_data)', __FILE__, __LINE__);
  1758. return false;
  1759. }
  1760. } else {
  1761. $this->DebugMessage('Cannot auto-rotate from EXIF data because $this->sourceFilename is empty', __FILE__, __LINE__);
  1762. return false;
  1763. }
  1764. } else {
  1765. $this->DebugMessage('Cannot auto-rotate from EXIF data because PHP is less than v4.2.0 ('.phpversion().')', __FILE__, __LINE__);
  1766. return false;
  1767. }
  1768. } elseif (($this->ar == 'l') && ($this->source_height > $this->source_width)) {
  1769. $rotate_angle = 270;
  1770. } elseif (($this->ar == 'L') && ($this->source_height > $this->source_width)) {
  1771. $rotate_angle = 90;
  1772. } elseif (($this->ar == 'p') && ($this->source_width > $this->source_height)) {
  1773. $rotate_angle = 90;
  1774. } elseif (($this->ar == 'P') && ($this->source_width > $this->source_height)) {
  1775. $rotate_angle = 270;
  1776. }
  1777. }
  1778. if ($rotate_angle % 90) {
  1779. $this->is_alpha = true;
  1780. }
  1781. phpthumb_filters::ImprovedImageRotate($this->gdimg_source, $rotate_angle, $this->config_background_hexcolor, $this->bg);
  1782. $this->source_width = ImageSX($this->gdimg_source);
  1783. $this->source_height = ImageSY($this->gdimg_source);
  1784. }
  1785. return true;
  1786. }
  1787. function FixedAspectRatio() {
  1788. // optional fixed-dimension images (regardless of aspect ratio)
  1789. if (!$this->far) {
  1790. // do nothing
  1791. return true;
  1792. }
  1793. if (!$this->w || !$this->h) {
  1794. return false;
  1795. }
  1796. $this->thumbnail_width = $this->w;
  1797. $this->thumbnail_height = $this->h;
  1798. $this->is_alpha = true;
  1799. if ($this->thumbnail_image_width >= $this->thumbnail_width) {
  1800. if ($this->w) {
  1801. $aspectratio = $this->thumbnail_image_height / $this->thumbnail_image_width;
  1802. $this->thumbnail_image_height = round($this->thumbnail_image_width * $aspectratio);
  1803. $this->thumbnail_height = ($this->h ? $this->h : $this->thumbnail_image_height);
  1804. } elseif ($this->thumbnail_image_height < $this->thumbnail_height) {
  1805. $this->thumbnail_image_height = $this->thumbnail_height;
  1806. $this->thumbnail_image_width = round($this->thumbnail_image_height / $aspectratio);
  1807. }
  1808. } else {
  1809. if ($this->h) {
  1810. $aspectratio = $this->thumbnail_image_width / $this->thumbnail_image_height;
  1811. $this->thumbnail_image_width = round($this->thumbnail_image_height * $aspectratio);
  1812. } elseif ($this->thumbnail_image_width < $this->thumbnail_width) {
  1813. $this->thumbnail_image_width = $this->thumbnail_width;
  1814. $this->thumbnail_image_height = round($this->thumbnail_image_width / $aspectratio);
  1815. }
  1816. }
  1817. return true;
  1818. }
  1819. function OffsiteDomainIsAllowed($hostname, $allowed_domains) {
  1820. static $domain_is_allowed = array();
  1821. $hostname = strtolower($hostname);
  1822. if (!isset($domain_is_allowed[$hostname])) {
  1823. $domain_is_allowed[$hostname] = false;
  1824. foreach ($allowed_domains as $valid_domain) {
  1825. $starpos = strpos($valid_domain, '*');
  1826. if ($starpos !== false) {
  1827. $valid_domain = substr($valid_domain, $starpos + 1);
  1828. if (preg_match('#'.preg_quote($valid_domain).'$#', $hostname)) {
  1829. $domain_is_allowed[$hostname] = true;
  1830. break;
  1831. }
  1832. } else {
  1833. if (strtolower($valid_domain) === $hostname) {
  1834. $domain_is_allowed[$hostname] = true;
  1835. break;
  1836. }
  1837. }
  1838. }
  1839. }
  1840. return $domain_is_allowed[$hostname];
  1841. }
  1842. function AntiOffsiteLinking() {
  1843. // Optional anti-offsite hijacking of the thumbnail script
  1844. $allow = true;
  1845. if ($allow && $this->config_nooffsitelink_enabled && (@$_SERVER['HTTP_REFERER'] || $this->config_nooffsitelink_require_refer)) {
  1846. $this->DebugMessage('AntiOffsiteLinking() checking $_SERVER[HTTP_REFERER] "'.@$_SERVER['HTTP_REFERER'].'"', __FILE__, __LINE__);
  1847. foreach ($this->config_nooffsitelink_valid_domains as $key => $valid_domain) {
  1848. // $_SERVER['HTTP_HOST'] contains the port number, so strip it out here to make default configuration work
  1849. list($clean_domain) = explode(':', $valid_domain);
  1850. $this->config_nooffsitelink_valid_domains[$key] = $clean_domain;
  1851. }
  1852. $parsed_url = phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
  1853. if (!$this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nooffsitelink_valid_domains)) {
  1854. $allow = false;
  1855. $erase = $this->config_nooffsitelink_erase_image;
  1856. $message = $this->config_nooffsitelink_text_message;
  1857. $this->ErrorImage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is NOT in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')');
  1858. exit;
  1859. $this->DebugMessage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is NOT in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__);
  1860. } else {
  1861. $this->DebugMessage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__);
  1862. }
  1863. }
  1864. if ($allow && $this->config_nohotlink_enabled && preg_match('#^(f|ht)tps?\://#i', $this->src)) {
  1865. $parsed_url = phpthumb_functions::ParseURLbetter($this->src);
  1866. //if (!phpthumb_functions::CaseInsensitiveInArray(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) {
  1867. if ($this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) {
  1868. // This domain is not allowed
  1869. $allow = false;
  1870. $erase = $this->config_nohotlink_erase_image;
  1871. $message = $this->config_nohotlink_text_message;
  1872. $this->DebugMessage('AntiOffsiteLinking() - "'.$parsed_url['host'].'" is NOT in $this->config_nohotlink_valid_domains ('.implode(';', $this->config_nohotlink_valid_domains).')', __FILE__, __LINE__);
  1873. } else {
  1874. $this->DebugMessage('AntiOffsiteLinking() - "'.$parsed_url['host'].'" is in $this->config_nohotlink_valid_domains ('.implode(';', $this->config_nohotlink_valid_domains).')', __FILE__, __LINE__);
  1875. }
  1876. }
  1877. if ($allow) {
  1878. $this->DebugMessage('AntiOffsiteLinking() says this is allowed', __FILE__, __LINE__);
  1879. return true;
  1880. }
  1881. if (!phpthumb_functions::IsHexColor($this->config_error_bgcolor)) {
  1882. return $this->ErrorImage('Invalid hex color string "'.$this->config_error_bgcolor.'" for $this->config_error_bgcolor');
  1883. }
  1884. if (!phpthumb_functions::IsHexColor($this->config_error_textcolor)) {
  1885. return $this->ErrorImage('Invalid hex color string "'.$this->config_error_textcolor.'" for $this->config_error_textcolor');
  1886. }
  1887. if ($erase) {
  1888. return $this->ErrorImage($message, $this->thumbnail_width, $this->thumbnail_height, $this->config_error_bgcolor, $this->config_error_textcolor, $this->config_error_fontsize);
  1889. } else {
  1890. $this->config_nooffsitelink_watermark_src = $this->ResolveFilenameToAbsolute($this->config_nooffsitelink_watermark_src);
  1891. if (is_file($this->config_nooffsitelink_watermark_src)) {
  1892. if (!include_once(dirname(__FILE__).'/phpthumb.filters.php')) {
  1893. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.filters.php" which is required for applying watermark', __FILE__, __LINE__);
  1894. return false;
  1895. }
  1896. $watermark_img = $this->ImageCreateFromStringReplacement(file_get_contents($this->config_nooffsitelink_watermark_src));
  1897. $phpthumbFilters = new phpthumb_filters();
  1898. $phpthumbFilters->phpThumbObject = &$this;
  1899. $opacity = 50;
  1900. $margin = 5;
  1901. $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $watermark_img, '*', $opacity, $margin);
  1902. ImageDestroy($watermark_img);
  1903. unset($phpthumbFilters);
  1904. } else {
  1905. $nohotlink_text_array = explode("\n", wordwrap($message, floor($this->thumbnail_width / ImageFontWidth($this->config_error_fontsize)), "\n"));
  1906. $nohotlink_text_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_error_textcolor);
  1907. $topoffset = round(($this->thumbnail_height - (count($nohotlink_text_array) * ImageFontHeight($this->config_error_fontsize))) / 2);
  1908. $rowcounter = 0;
  1909. $this->DebugMessage('AntiOffsiteLinking() writing '.count($nohotlink_text_array).' lines of text "'.$message.'" (in #'.$this->config_error_textcolor.') on top of image', __FILE__, __LINE__);
  1910. foreach ($nohotlink_text_array as $textline) {
  1911. $leftoffset = max(0, round(($this->thumbnail_width - (strlen($textline) * ImageFontWidth($this->config_error_fontsize))) / 2));
  1912. ImageString($this->gdimg_output, $this->config_error_fontsize, $leftoffset, $topoffset + ($rowcounter++ * ImageFontHeight($this->config_error_fontsize)), $textline, $nohotlink_text_color);
  1913. }
  1914. }
  1915. }
  1916. return true;
  1917. }
  1918. function AlphaChannelFlatten() {
  1919. if (!$this->is_alpha) {
  1920. // image doesn't have alpha transparency, no need to flatten
  1921. $this->DebugMessage('skipping AlphaChannelFlatten() because !$this->is_alpha', __FILE__, __LINE__);
  1922. return false;
  1923. }
  1924. switch ($this->thumbnailFormat) {
  1925. case 'png':
  1926. case 'ico':
  1927. // image has alpha transparency, but output as PNG or ICO which can handle it
  1928. $this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "'.$this->thumbnailFormat.'")', __FILE__, __LINE__);
  1929. return false;
  1930. break;
  1931. case 'gif':
  1932. // image has alpha transparency, but output as GIF which can handle only single-color transparency
  1933. $CurrentImageColorTransparent = ImageColorTransparent($this->gdimg_output);
  1934. if ($CurrentImageColorTransparent == -1) {
  1935. // no transparent color defined
  1936. if (phpthumb_functions::gd_version() < 2.0) {
  1937. $this->DebugMessage('AlphaChannelFlatten() failed because GD version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  1938. return false;
  1939. }
  1940. if ($img_alpha_mixdown_dither = @ImageCreateTrueColor(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output))) {
  1941. for ($i = 0; $i <= 255; $i++) {
  1942. $dither_color[$i] = ImageColorAllocate($img_alpha_mixdown_dither, $i, $i, $i);
  1943. }
  1944. // scan through current truecolor image copy alpha channel to temp image as grayscale
  1945. for ($x = 0; $x < $this->thumbnail_width; $x++) {
  1946. for ($y = 0; $y < $this->thumbnail_height; $y++) {
  1947. $PixelColor = phpthumb_functions::GetPixelColor($this->gdimg_output, $x, $y);
  1948. ImageSetPixel($img_alpha_mixdown_dither, $x, $y, $dither_color[($PixelColor['alpha'] * 2)]);
  1949. }
  1950. }
  1951. // dither alpha channel grayscale version down to 2 colors
  1952. ImageTrueColorToPalette($img_alpha_mixdown_dither, true, 2);
  1953. // reduce color palette to 256-1 colors (leave one palette position for transparent color)
  1954. ImageTrueColorToPalette($this->gdimg_output, true, 255);
  1955. // allocate a new color for transparent color index
  1956. $TransparentColor = ImageColorAllocate($this->gdimg_output, 1, 254, 253);
  1957. ImageColorTransparent($this->gdimg_output, $TransparentColor);
  1958. // scan through alpha channel image and note pixels with >50% transparency
  1959. $TransparentPixels = array();
  1960. for ($x = 0; $x < $this->thumbnail_width; $x++) {
  1961. for ($y = 0; $y < $this->thumbnail_height; $y++) {
  1962. $AlphaChannelPixel = phpthumb_functions::GetPixelColor($img_alpha_mixdown_dither, $x, $y);
  1963. if ($AlphaChannelPixel['red'] > 127) {
  1964. ImageSetPixel($this->gdimg_output, $x, $y, $TransparentColor);
  1965. }
  1966. }
  1967. }
  1968. ImageDestroy($img_alpha_mixdown_dither);
  1969. $this->DebugMessage('AlphaChannelFlatten() set image to 255+1 colors with transparency for GIF output', __FILE__, __LINE__);
  1970. return true;
  1971. } else {
  1972. $this->DebugMessage('AlphaChannelFlatten() failed ImageCreate('.ImageSX($this->gdimg_output).', '.ImageSY($this->gdimg_output).')', __FILE__, __LINE__);
  1973. return false;
  1974. }
  1975. } else {
  1976. // a single transparent color already defined, leave as-is
  1977. $this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "'.$this->thumbnailFormat.'") and ImageColorTransparent returned "'.$CurrentImageColorTransparent.'"', __FILE__, __LINE__);
  1978. return true;
  1979. }
  1980. break;
  1981. }
  1982. $this->DebugMessage('continuing AlphaChannelFlatten() for output format "'.$this->thumbnailFormat.'"', __FILE__, __LINE__);
  1983. // image has alpha transparency, and is being output in a format that doesn't support it -- flatten
  1984. if ($gdimg_flatten_temp = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height)) {
  1985. $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
  1986. if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
  1987. return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
  1988. }
  1989. $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor);
  1990. ImageFilledRectangle($gdimg_flatten_temp, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color);
  1991. ImageCopy($gdimg_flatten_temp, $this->gdimg_output, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height);
  1992. ImageAlphaBlending($this->gdimg_output, true);
  1993. ImageSaveAlpha($this->gdimg_output, false);
  1994. ImageColorTransparent($this->gdimg_output, -1);
  1995. ImageCopy($this->gdimg_output, $gdimg_flatten_temp, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height);
  1996. ImageDestroy($gdimg_flatten_temp);
  1997. return true;
  1998. } else {
  1999. $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
  2000. }
  2001. return false;
  2002. }
  2003. function ApplyFilters() {
  2004. if ($this->fltr && is_array($this->fltr)) {
  2005. if (!include_once(dirname(__FILE__).'/phpthumb.filters.php')) {
  2006. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.filters.php" which is required for applying filters ('.implode(';', $this->fltr).')', __FILE__, __LINE__);
  2007. return false;
  2008. }
  2009. $phpthumbFilters = new phpthumb_filters();
  2010. $phpthumbFilters->phpThumbObject = &$this;
  2011. foreach ($this->fltr as $filtercommand) {
  2012. @list($command, $parameter) = explode('|', $filtercommand, 2);
  2013. $this->DebugMessage('Attempting to process filter command "'.$command.'('.$parameter.')"', __FILE__, __LINE__);
  2014. switch ($command) {
  2015. case 'brit': // Brightness
  2016. $phpthumbFilters->Brightness($this->gdimg_output, $parameter);
  2017. break;
  2018. case 'cont': // Contrast
  2019. $phpthumbFilters->Contrast($this->gdimg_output, $parameter);
  2020. break;
  2021. case 'ds': // Desaturation
  2022. $phpthumbFilters->Desaturate($this->gdimg_output, $parameter, '');
  2023. break;
  2024. case 'sat': // Saturation
  2025. $phpthumbFilters->Saturation($this->gdimg_output, $parameter, '');
  2026. break;
  2027. case 'gray': // Grayscale
  2028. $phpthumbFilters->Grayscale($this->gdimg_output);
  2029. break;
  2030. case 'clr': // Colorize
  2031. if (phpthumb_functions::gd_version() < 2) {
  2032. $this->DebugMessage('Skipping Colorize() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2033. break;
  2034. }
  2035. @list($amount, $color) = explode('|', $parameter, 2);
  2036. $phpthumbFilters->Colorize($this->gdimg_output, $amount, $color);
  2037. break;
  2038. case 'sep': // Sepia
  2039. if (phpthumb_functions::gd_version() < 2) {
  2040. $this->DebugMessage('Skipping Sepia() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2041. break;
  2042. }
  2043. @list($amount, $color) = explode('|', $parameter, 2);
  2044. $phpthumbFilters->Sepia($this->gdimg_output, $amount, $color);
  2045. break;
  2046. case 'gam': // Gamma correction
  2047. $phpthumbFilters->Gamma($this->gdimg_output, $parameter);
  2048. break;
  2049. case 'neg': // Negative colors
  2050. $phpthumbFilters->Negative($this->gdimg_output);
  2051. break;
  2052. case 'th': // Threshold
  2053. $phpthumbFilters->Threshold($this->gdimg_output, $parameter);
  2054. break;
  2055. case 'rcd': // ReduceColorDepth
  2056. if (phpthumb_functions::gd_version() < 2) {
  2057. $this->DebugMessage('Skipping ReduceColorDepth() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2058. break;
  2059. }
  2060. @list($colors, $dither) = explode('|', $parameter, 2);
  2061. $colors = ($colors ? (int) $colors : 256);
  2062. $dither = ((strlen($dither) > 0) ? (bool) $dither : true);
  2063. $phpthumbFilters->ReduceColorDepth($this->gdimg_output, $colors, $dither);
  2064. break;
  2065. case 'flip': // Flip
  2066. $phpthumbFilters->Flip($this->gdimg_output, (strpos(strtolower($parameter), 'x') !== false), (strpos(strtolower($parameter), 'y') !== false));
  2067. break;
  2068. case 'edge': // EdgeDetect
  2069. $phpthumbFilters->EdgeDetect($this->gdimg_output);
  2070. break;
  2071. case 'emb': // Emboss
  2072. $phpthumbFilters->Emboss($this->gdimg_output);
  2073. break;
  2074. case 'bvl': // Bevel
  2075. @list($width, $color1, $color2) = explode('|', $parameter, 3);
  2076. $phpthumbFilters->Bevel($this->gdimg_output, $width, $color1, $color2);
  2077. break;
  2078. case 'lvl': // autoLevels
  2079. @list($band, $method, $threshold) = explode('|', $parameter, 3);
  2080. $band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*');
  2081. $method = ((strlen($method) > 0) ? intval($method) : 2);
  2082. $threshold = ((strlen($threshold) > 0) ? floatval($threshold) : 0.1);
  2083. $phpthumbFilters->HistogramStretch($this->gdimg_output, $band, $method, $threshold);
  2084. break;
  2085. case 'wb': // WhiteBalance
  2086. $phpthumbFilters->WhiteBalance($this->gdimg_output, $parameter);
  2087. break;
  2088. case 'hist': // Histogram overlay
  2089. if (phpthumb_functions::gd_version() < 2) {
  2090. $this->DebugMessage('Skipping HistogramOverlay() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2091. break;
  2092. }
  2093. @list($bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y) = explode('|', $parameter, 8);
  2094. $bands = ($bands ? $bands : '*');
  2095. $colors = ($colors ? $colors : '');
  2096. $width = ($width ? $width : 0.25);
  2097. $height = ($height ? $height : 0.25);
  2098. $alignment = ($alignment ? $alignment : 'BR');
  2099. $opacity = ($opacity ? $opacity : 50);
  2100. $margin_x = ($margin_x ? $margin_x : 5);
  2101. $margin_y = $margin_y; // just to note it wasn't forgotten, but let the value always pass unchanged
  2102. $phpthumbFilters->HistogramOverlay($this->gdimg_output, $bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y);
  2103. break;
  2104. case 'fram': // Frame
  2105. @list($frame_width, $edge_width, $color_frame, $color1, $color2) = explode('|', $parameter, 5);
  2106. $phpthumbFilters->Frame($this->gdimg_output, $frame_width, $edge_width, $color_frame, $color1, $color2);
  2107. break;
  2108. case 'drop': // DropShadow
  2109. if (phpthumb_functions::gd_version() < 2) {
  2110. $this->DebugMessage('Skipping DropShadow() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2111. return false;
  2112. }
  2113. $this->is_alpha = true;
  2114. @list($distance, $width, $color, $angle, $fade) = explode('|', $parameter, 5);
  2115. $phpthumbFilters->DropShadow($this->gdimg_output, $distance, $width, $color, $angle, $fade);
  2116. break;
  2117. case 'mask': // Mask cropping
  2118. if (phpthumb_functions::gd_version() < 2) {
  2119. $this->DebugMessage('Skipping Mask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2120. return false;
  2121. }
  2122. $mask_filename = $this->ResolveFilenameToAbsolute($parameter);
  2123. if (@is_readable($mask_filename) && ($fp_mask = @fopen($mask_filename, 'rb'))) {
  2124. $MaskImageData = '';
  2125. do {
  2126. $buffer = fread($fp_mask, 8192);
  2127. $MaskImageData .= $buffer;
  2128. } while (strlen($buffer) > 0);
  2129. fclose($fp_mask);
  2130. if ($gdimg_mask = $this->ImageCreateFromStringReplacement($MaskImageData)) {
  2131. $this->is_alpha = true;
  2132. $phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output);
  2133. ImageDestroy($gdimg_mask);
  2134. } else {
  2135. $this->DebugMessage('ImageCreateFromStringReplacement() failed for "'.$mask_filename.'"', __FILE__, __LINE__);
  2136. }
  2137. } else {
  2138. $this->DebugMessage('Cannot open mask file "'.$mask_filename.'"', __FILE__, __LINE__);
  2139. }
  2140. break;
  2141. case 'elip': // Elipse cropping
  2142. if (phpthumb_functions::gd_version() < 2) {
  2143. $this->DebugMessage('Skipping Elipse() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2144. return false;
  2145. }
  2146. $this->is_alpha = true;
  2147. $phpthumbFilters->Elipse($this->gdimg_output);
  2148. break;
  2149. case 'ric': // RoundedImageCorners
  2150. if (phpthumb_functions::gd_version() < 2) {
  2151. $this->DebugMessage('Skipping RoundedImageCorners() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2152. return false;
  2153. }
  2154. @list($radius_x, $radius_y) = explode('|', $parameter, 2);
  2155. if (($radius_x < 1) || ($radius_y < 1)) {
  2156. $this->DebugMessage('Skipping RoundedImageCorners('.$radius_x.', '.$radius_y.') because x/y radius is less than 1', __FILE__, __LINE__);
  2157. break;
  2158. }
  2159. $this->is_alpha = true;
  2160. $phpthumbFilters->RoundedImageCorners($this->gdimg_output, $radius_x, $radius_y);
  2161. break;
  2162. case 'crop': // Crop
  2163. @list($left, $right, $top, $bottom) = explode('|', $parameter, 4);
  2164. $phpthumbFilters->Crop($this->gdimg_output, $left, $right, $top, $bottom);
  2165. break;
  2166. case 'bord': // Border
  2167. @list($border_width, $radius_x, $radius_y, $hexcolor_border) = explode('|', $parameter, 4);
  2168. $this->is_alpha = true;
  2169. $phpthumbFilters->ImageBorder($this->gdimg_output, $border_width, $radius_x, $radius_y, $hexcolor_border);
  2170. break;
  2171. case 'over': // Overlay
  2172. @list($filename, $underlay, $margin, $opacity) = explode('|', $parameter, 4);
  2173. $underlay = (bool) ($underlay ? $underlay : false);
  2174. $margin = ((strlen($margin) > 0) ? $margin : ($underlay ? 0.1 : 0.0));
  2175. $opacity = ((strlen($opacity) > 0) ? $opacity : 100);
  2176. if (($margin > 0) && ($margin < 1)) {
  2177. $margin = min(0.499, $margin);
  2178. } elseif (($margin > -1) && ($margin < 0)) {
  2179. $margin = max(-0.499, $margin);
  2180. }
  2181. $filename = $this->ResolveFilenameToAbsolute($filename);
  2182. if (@is_readable($filename) && ($fp_watermark = @fopen($filename, 'rb'))) {
  2183. $WatermarkImageData = '';
  2184. do {
  2185. $buffer = fread($fp_watermark, 8192);
  2186. $WatermarkImageData .= $buffer;
  2187. } while (strlen($buffer) > 0);
  2188. fclose($fp_watermark);
  2189. if ($img_watermark = $this->ImageCreateFromStringReplacement($WatermarkImageData)) {
  2190. if ($margin < 1) {
  2191. $resized_x = max(1, ImageSX($this->gdimg_output) - round(2 * (ImageSX($this->gdimg_output) * $margin)));
  2192. $resized_y = max(1, ImageSY($this->gdimg_output) - round(2 * (ImageSY($this->gdimg_output) * $margin)));
  2193. } else {
  2194. $resized_x = max(1, ImageSX($this->gdimg_output) - round(2 * $margin));
  2195. $resized_y = max(1, ImageSY($this->gdimg_output) - round(2 * $margin));
  2196. }
  2197. if ($underlay) {
  2198. if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output))) {
  2199. ImageAlphaBlending($img_watermark_resized, false);
  2200. ImageSaveAlpha($img_watermark_resized, true);
  2201. $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));
  2202. if ($img_source_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
  2203. ImageAlphaBlending($img_source_resized, false);
  2204. ImageSaveAlpha($img_source_resized, true);
  2205. $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));
  2206. $phpthumbFilters->WatermarkOverlay($img_watermark_resized, $img_source_resized, 'C', $opacity, $margin);
  2207. ImageCopy($this->gdimg_output, $img_watermark_resized, 0, 0, 0, 0, ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
  2208. } else {
  2209. $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.$resized_x.', '.$resized_y.')', __FILE__, __LINE__);
  2210. }
  2211. ImageDestroy($img_watermark_resized);
  2212. } else {
  2213. $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.ImageSX($this->gdimg_output).', '.ImageSY($this->gdimg_output).')', __FILE__, __LINE__);
  2214. }
  2215. } else { // overlay
  2216. if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
  2217. ImageAlphaBlending($img_watermark_resized, false);
  2218. ImageSaveAlpha($img_watermark_resized, true);
  2219. $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));
  2220. $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark_resized, 'C', $opacity, $margin);
  2221. ImageDestroy($img_watermark_resized);
  2222. } else {
  2223. $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.$resized_x.', '.$resized_y.')', __FILE__, __LINE__);
  2224. }
  2225. }
  2226. ImageDestroy($img_watermark);
  2227. } else {
  2228. $this->DebugMessage('ImageCreateFromStringReplacement() failed for "'.$filename.'"', __FILE__, __LINE__);
  2229. }
  2230. } else {
  2231. $this->DebugMessage('Cannot open overlay file "'.$filename.'"', __FILE__, __LINE__);
  2232. }
  2233. break;
  2234. case 'wmi': // WaterMarkImage
  2235. @list($filename, $alignment, $opacity, $margin['x'], $margin['y'], $rotate_angle) = explode('|', $parameter, 6);
  2236. // $margin can be pixel margin or percent margin if $alignment is text, or max width/height if $alignment is position like "50x75"
  2237. $alignment = ($alignment ? $alignment : 'BR');
  2238. $opacity = (strlen($opacity) ? intval($opacity) : 50);
  2239. $rotate_angle = (strlen($rotate_angle) ? intval($rotate_angle) : 0);
  2240. if (!preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
  2241. $margins = array('x', 'y');
  2242. foreach ($margins as $xy) {
  2243. $margin[$xy] = (strlen($margin[$xy]) ? $margin[$xy] : 5);
  2244. if (($margin[$xy] > 0) && ($margin[$xy] < 1)) {
  2245. $margin[$xy] = min(0.499, $margin[$xy]);
  2246. } elseif (($margin[$xy] > -1) && ($margin[$xy] < 0)) {
  2247. $margin[$xy] = max(-0.499, $margin[$xy]);
  2248. }
  2249. }
  2250. }
  2251. $filename = $this->ResolveFilenameToAbsolute($filename);
  2252. if (@is_readable($filename)) {
  2253. if ($img_watermark = $this->ImageCreateFromFilename($filename)) {
  2254. if ($rotate_angle !== 0) {
  2255. $phpthumbFilters->ImprovedImageRotate($img_watermark, $rotate_angle);
  2256. }
  2257. if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
  2258. $watermark_max_width = intval($margin['x'] ? $margin['x'] : ImageSX($img_watermark));
  2259. $watermark_max_height = intval($margin['y'] ? $margin['y'] : ImageSY($img_watermark));
  2260. $scale = phpthumb_functions::ScaleToFitInBox(ImageSX($img_watermark), ImageSY($img_watermark), $watermark_max_width, $watermark_max_height, true, true);
  2261. $this->DebugMessage('Scaling watermark by a factor of '.number_format($scale, 4), __FILE__, __LINE__);
  2262. if (($scale > 1) || ($scale < 1)) {
  2263. if ($img_watermark2 = phpthumb_functions::ImageCreateFunction($scale * ImageSX($img_watermark), $scale * ImageSY($img_watermark))) {
  2264. ImageAlphaBlending($img_watermark2, false);
  2265. ImageSaveAlpha($img_watermark2, true);
  2266. $this->ImageResizeFunction($img_watermark2, $img_watermark, 0, 0, 0, 0, ImageSX($img_watermark2), ImageSY($img_watermark2), ImageSX($img_watermark), ImageSY($img_watermark));
  2267. $img_watermark = $img_watermark2;
  2268. } else {
  2269. $this->DebugMessage('ImageCreateFunction('.($scale * ImageSX($img_watermark)).', '.($scale * ImageSX($img_watermark)).') failed', __FILE__, __LINE__);
  2270. }
  2271. }
  2272. $watermark_dest_x = round($matches[1] - (ImageSX($img_watermark) / 2));
  2273. $watermark_dest_y = round($matches[2] - (ImageSY($img_watermark) / 2));
  2274. $alignment = $watermark_dest_x.'x'.$watermark_dest_y;
  2275. }
  2276. $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark, $alignment, $opacity, $margin['x'], $margin['y']);
  2277. ImageDestroy($img_watermark);
  2278. if (isset($img_watermark2) && is_resource($img_watermark2)) {
  2279. ImageDestroy($img_watermark2);
  2280. }
  2281. } else {
  2282. $this->DebugMessage('ImageCreateFromFilename() failed for "'.$filename.'"', __FILE__, __LINE__);
  2283. }
  2284. } else {
  2285. $this->DebugMessage('!is_readable('.$filename.')', __FILE__, __LINE__);
  2286. }
  2287. break;
  2288. case 'wmt': // WaterMarkText
  2289. @list($text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend) = explode('|', $parameter, 11);
  2290. $text = ($text ? $text : '');
  2291. $size = ($size ? $size : 3);
  2292. $alignment = ($alignment ? $alignment : 'BR');
  2293. $hex_color = ($hex_color ? $hex_color : '000000');
  2294. $ttffont = ($ttffont ? $ttffont : '');
  2295. $opacity = (strlen($opacity) ? $opacity : 50);
  2296. $margin = (strlen($margin) ? $margin : 5);
  2297. $angle = (strlen($angle) ? $angle : 0);
  2298. $bg_color = ($bg_color ? $bg_color : false);
  2299. $bg_opacity = ($bg_opacity ? $bg_opacity : 0);
  2300. $fillextend = ($fillextend ? $fillextend : '');
  2301. if (basename($ttffont) == $ttffont) {
  2302. $ttffont = realpath($this->config_ttf_directory.DIRECTORY_SEPARATOR.$ttffont);
  2303. } else {
  2304. $ttffont = $this->ResolveFilenameToAbsolute($ttffont);
  2305. }
  2306. $phpthumbFilters->WatermarkText($this->gdimg_output, $text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend);
  2307. break;
  2308. case 'blur': // Blur
  2309. @list($radius) = explode('|', $parameter, 1);
  2310. $radius = ($radius ? $radius : 1);
  2311. if (phpthumb_functions::gd_version() >= 2) {
  2312. $phpthumbFilters->Blur($this->gdimg_output, $radius);
  2313. } else {
  2314. $this->DebugMessage('Skipping Blur() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2315. }
  2316. break;
  2317. case 'gblr': // Gaussian Blur
  2318. $phpthumbFilters->BlurGaussian($this->gdimg_output);
  2319. break;
  2320. case 'sblr': // Selective Blur
  2321. $phpthumbFilters->BlurSelective($this->gdimg_output);
  2322. break;
  2323. case 'mean': // MeanRemoval blur
  2324. $phpthumbFilters->MeanRemoval($this->gdimg_output);
  2325. break;
  2326. case 'smth': // Smooth blur
  2327. $phpthumbFilters->Smooth($this->gdimg_output, $parameter);
  2328. break;
  2329. case 'usm': // UnSharpMask sharpening
  2330. @list($amount, $radius, $threshold) = explode('|', $parameter, 3);
  2331. $amount = ($amount ? $amount : 80);
  2332. $radius = ($radius ? $radius : 0.5);
  2333. $threshold = (strlen($threshold) ? $threshold : 3);
  2334. if (phpthumb_functions::gd_version() >= 2.0) {
  2335. ob_start();
  2336. if (!@include_once(dirname(__FILE__).'/phpthumb.unsharp.php')) {
  2337. $include_error = ob_get_contents();
  2338. if ($include_error) {
  2339. $this->DebugMessage('include_once("'.dirname(__FILE__).'/phpthumb.unsharp.php") generated message: "'.$include_error.'"', __FILE__, __LINE__);
  2340. }
  2341. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.unsharp.php" which is required for unsharp masking', __FILE__, __LINE__);
  2342. ob_end_clean();
  2343. return false;
  2344. }
  2345. ob_end_clean();
  2346. phpUnsharpMask::applyUnsharpMask($this->gdimg_output, $amount, $radius, $threshold);
  2347. } else {
  2348. $this->DebugMessage('Skipping unsharp mask because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2349. return false;
  2350. }
  2351. break;
  2352. case 'size': // Resize
  2353. @list($newwidth, $newheight, $stretch) = explode('|', $parameter);
  2354. $newwidth = (!$newwidth ? ImageSX($this->gdimg_output) : ((($newwidth > 0) && ($newwidth < 1)) ? round($newwidth * ImageSX($this->gdimg_output)) : round($newwidth)));
  2355. $newheight = (!$newheight ? ImageSY($this->gdimg_output) : ((($newheight > 0) && ($newheight < 1)) ? round($newheight * ImageSY($this->gdimg_output)) : round($newheight)));
  2356. $stretch = ($stretch ? true : false);
  2357. if ($stretch) {
  2358. $scale_x = phpthumb_functions::ScaleToFitInBox(ImageSX($this->gdimg_output), ImageSX($this->gdimg_output), $newwidth, $newwidth, true, true);
  2359. $scale_y = phpthumb_functions::ScaleToFitInBox(ImageSY($this->gdimg_output), ImageSY($this->gdimg_output), $newheight, $newheight, true, true);
  2360. } else {
  2361. $scale_x = phpthumb_functions::ScaleToFitInBox(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output), $newwidth, $newheight, true, true);
  2362. $scale_y = $scale_x;
  2363. }
  2364. $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__);
  2365. if (($scale_x > 1) || ($scale_x < 1) || ($scale_y > 1) || ($scale_y < 1)) {
  2366. if ($img_temp = phpthumb_functions::ImageCreateFunction(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output))) {
  2367. ImageCopy($img_temp, $this->gdimg_output, 0, 0, 0, 0, ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
  2368. //ImageDestroy($this->gdimg_output);
  2369. if ($this->gdimg_output = phpthumb_functions::ImageCreateFunction($scale_x * ImageSX($img_temp), $scale_y * ImageSY($img_temp))) {
  2370. ImageAlphaBlending($this->gdimg_output, false);
  2371. ImageSaveAlpha($this->gdimg_output, true);
  2372. $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));
  2373. } else {
  2374. $this->DebugMessage('ImageCreateFunction('.($scale_x * ImageSX($img_temp)).', '.($scale_y * ImageSY($img_temp)).') failed', __FILE__, __LINE__);
  2375. }
  2376. ImageDestroy($img_temp);
  2377. } else {
  2378. $this->DebugMessage('ImageCreateFunction('.ImageSX($this->gdimg_output).', '.ImageSY($this->gdimg_output).') failed', __FILE__, __LINE__);
  2379. }
  2380. }
  2381. break;
  2382. case 'rot': // ROTate
  2383. @list($angle, $bgcolor) = explode('|', $parameter, 2);
  2384. $phpthumbFilters->ImprovedImageRotate($this->gdimg_output, $angle, $bgcolor);
  2385. break;
  2386. case 'stc': // Source Transparent Color
  2387. @list($hexcolor, $min_limit, $max_limit) = explode('|', $parameter, 3);
  2388. if (!phpthumb_functions::IsHexColor($hexcolor)) {
  2389. $this->DebugMessage('Skipping SourceTransparentColor hex color is invalid ('.$hexcolor.')', __FILE__, __LINE__);
  2390. return false;
  2391. }
  2392. $min_limit = (strlen($min_limit) ? $min_limit : 5);
  2393. $max_limit = (strlen($max_limit) ? $max_limit : 10);
  2394. if ($gdimg_mask = $phpthumbFilters->SourceTransparentColorMask($this->gdimg_output, $hexcolor, $min_limit, $max_limit)) {
  2395. $this->is_alpha = true;
  2396. $phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output);
  2397. ImageDestroy($gdimg_mask);
  2398. } else {
  2399. $this->DebugMessage('SourceTransparentColorMask() failed for "'.$mask_filename.'"', __FILE__, __LINE__);
  2400. }
  2401. break;
  2402. }
  2403. $this->DebugMessage('Finished processing filter command "'.$command.'('.$parameter.')"', __FILE__, __LINE__);
  2404. }
  2405. }
  2406. return true;
  2407. }
  2408. function MaxFileSize() {
  2409. if (phpthumb_functions::gd_version() < 2) {
  2410. $this->DebugMessage('Skipping MaxFileSize() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2411. return false;
  2412. }
  2413. if ($this->maxb > 0) {
  2414. switch ($this->thumbnailFormat) {
  2415. case 'png':
  2416. case 'gif':
  2417. $imgRenderFunction = 'image'.$this->thumbnailFormat;
  2418. ob_start();
  2419. $imgRenderFunction($this->gdimg_output);
  2420. $imgdata = ob_get_contents();
  2421. ob_end_clean();
  2422. if (strlen($imgdata) > $this->maxb) {
  2423. for ($i = 8; $i >= 1; $i--) {
  2424. $tempIMG = ImageCreateTrueColor(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
  2425. ImageCopy($tempIMG, $this->gdimg_output, 0, 0, 0, 0, ImageSX($this->gdimg_output), ImageSY($this->gdimg_output));
  2426. ImageTrueColorToPalette($tempIMG, true, pow(2, $i));
  2427. ob_start();
  2428. $imgRenderFunction($tempIMG);
  2429. $imgdata = ob_get_contents();
  2430. ob_end_clean();
  2431. if (strlen($imgdata) <= $this->maxb) {
  2432. ImageTrueColorToPalette($this->gdimg_output, true, pow(2, $i));
  2433. break;
  2434. }
  2435. }
  2436. }
  2437. if (strlen($imgdata) > $this->maxb) {
  2438. ImageTrueColorToPalette($this->gdimg_output, true, pow(2, $i));
  2439. return false;
  2440. }
  2441. break;
  2442. case 'jpeg':
  2443. ob_start();
  2444. ImageJPEG($this->gdimg_output);
  2445. $imgdata = ob_get_contents();
  2446. ob_end_clean();
  2447. $OriginalJPEGquality = $this->thumbnailQuality;
  2448. if (strlen($imgdata) > $this->maxb) {
  2449. for ($i = 3; $i < 20; $i++) {
  2450. $q = round(100 * (1 - log10($i / 2)));
  2451. ob_start();
  2452. ImageJPEG($this->gdimg_output, '', $q);
  2453. $imgdata = ob_get_contents();
  2454. ob_end_clean();
  2455. $this->thumbnailQuality = $q;
  2456. if (strlen($imgdata) <= $this->maxb) {
  2457. break;
  2458. }
  2459. }
  2460. }
  2461. if (strlen($imgdata) > $this->maxb) {
  2462. return false;
  2463. }
  2464. break;
  2465. default:
  2466. return false;
  2467. break;
  2468. }
  2469. }
  2470. return true;
  2471. }
  2472. function CalculateThumbnailDimensions() {
  2473. //echo $this->source_width.'x'.$this->source_height.'<hr>';
  2474. $this->thumbnailCropX = ($this->sx ? (($this->sx >= 1) ? $this->sx : round($this->sx * $this->source_width)) : 0);
  2475. //echo $this->thumbnailCropX.'<br>';
  2476. $this->thumbnailCropY = ($this->sy ? (($this->sy >= 1) ? $this->sy : round($this->sy * $this->source_height)) : 0);
  2477. //echo $this->thumbnailCropY.'<br>';
  2478. $this->thumbnailCropW = ($this->sw ? (($this->sw >= 1) ? $this->sw : round($this->sw * $this->source_width)) : $this->source_width);
  2479. //echo $this->thumbnailCropW.'<br>';
  2480. $this->thumbnailCropH = ($this->sh ? (($this->sh >= 1) ? $this->sh : round($this->sh * $this->source_height)) : $this->source_height);
  2481. //echo $this->thumbnailCropH.'<hr>';
  2482. // limit source area to original image area
  2483. $this->thumbnailCropW = max(1, min($this->thumbnailCropW, $this->source_width - $this->thumbnailCropX));
  2484. $this->thumbnailCropH = max(1, min($this->thumbnailCropH, $this->source_height - $this->thumbnailCropY));
  2485. $this->DebugMessage('CalculateThumbnailDimensions() [x,y,w,h] initially set to ['.$this->thumbnailCropX.','.$this->thumbnailCropY.','.$this->thumbnailCropW.','.$this->thumbnailCropH.']', __FILE__, __LINE__);
  2486. if ($this->zc && $this->w && $this->h) {
  2487. // Zoom Crop
  2488. // retain proportional resizing we did above, but crop off larger dimension so smaller
  2489. // dimension fully fits available space
  2490. $scaling_X = $this->source_width / $this->w;
  2491. $scaling_Y = $this->source_height / $this->h;
  2492. if ($scaling_X > $scaling_Y) {
  2493. // some of the width will need to be cropped
  2494. $allowable_width = $this->source_width / $scaling_X * $scaling_Y;
  2495. $this->thumbnailCropW = round($allowable_width);
  2496. $this->thumbnailCropX = round(($this->source_width - $allowable_width) / 2);
  2497. } elseif ($scaling_Y > $scaling_X) {
  2498. // some of the height will need to be cropped
  2499. $allowable_height = $this->source_height / $scaling_Y * $scaling_X;
  2500. $this->thumbnailCropH = round($allowable_height);
  2501. $this->thumbnailCropY = round(($this->source_height - $allowable_height) / 2);
  2502. } else {
  2503. // image fits perfectly, no cropping needed
  2504. }
  2505. $this->thumbnail_width = $this->w;
  2506. $this->thumbnail_height = $this->h;
  2507. $this->thumbnail_image_width = $this->thumbnail_width;
  2508. $this->thumbnail_image_height = $this->thumbnail_height;
  2509. } elseif ($this->iar && $this->w && $this->h) {
  2510. // Ignore Aspect Ratio
  2511. // stretch image to fit exactly 'w' x 'h'
  2512. $this->thumbnail_width = $this->w;
  2513. $this->thumbnail_height = $this->h;
  2514. $this->thumbnail_image_width = $this->thumbnail_width;
  2515. $this->thumbnail_image_height = $this->thumbnail_height;
  2516. } else {
  2517. $original_aspect_ratio = $this->thumbnailCropW / $this->thumbnailCropH;
  2518. if ($this->aoe) {
  2519. if ($this->w && $this->h) {
  2520. $maxwidth = min($this->w, $this->h * $original_aspect_ratio);
  2521. $maxheight = min($this->h, $this->w / $original_aspect_ratio);
  2522. } elseif ($this->w) {
  2523. $maxwidth = $this->w;
  2524. $maxheight = $this->w / $original_aspect_ratio;
  2525. } elseif ($this->h) {
  2526. $maxwidth = $this->h * $original_aspect_ratio;
  2527. $maxheight = $this->h;
  2528. } else {
  2529. $maxwidth = $this->thumbnailCropW;
  2530. $maxheight = $this->thumbnailCropH;
  2531. }
  2532. } else {
  2533. $maxwidth = phpthumb_functions::nonempty_min($this->w, $this->thumbnailCropW, $this->config_output_maxwidth);
  2534. $maxheight = phpthumb_functions::nonempty_min($this->h, $this->thumbnailCropH, $this->config_output_maxheight);
  2535. //echo $maxwidth.'x'.$maxheight.'<br>';
  2536. $maxwidth = min($maxwidth, $maxheight * $original_aspect_ratio);
  2537. $maxheight = min($maxheight, $maxwidth / $original_aspect_ratio);
  2538. //echo $maxwidth.'x'.$maxheight.'<hr>';
  2539. }
  2540. $this->thumbnail_image_width = $maxwidth;
  2541. $this->thumbnail_image_height = $maxheight;
  2542. $this->thumbnail_width = $maxwidth;
  2543. $this->thumbnail_height = $maxheight;
  2544. $this->FixedAspectRatio();
  2545. }
  2546. $this->thumbnail_width = max(1, floor($this->thumbnail_width));
  2547. $this->thumbnail_height = max(1, floor($this->thumbnail_height));
  2548. return true;
  2549. }
  2550. function CreateGDoutput() {
  2551. $this->CalculateThumbnailDimensions();
  2552. // Create the GD image (either true-color or 256-color, depending on GD version)
  2553. $this->gdimg_output = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height);
  2554. // Images that have transparency must have the background filled with the configured 'bg' color
  2555. // otherwise the transparent color will appear as black
  2556. ImageSaveAlpha($this->gdimg_output, true);
  2557. if ($this->is_alpha && phpthumb_functions::gd_version() >= 2) {
  2558. ImageAlphaBlending($this->gdimg_output, false);
  2559. $output_full_alpha = phpthumb_functions::ImageColorAllocateAlphaSafe($this->gdimg_output, 255, 255, 255, 127);
  2560. ImageFilledRectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $output_full_alpha);
  2561. } else {
  2562. $current_transparent_color = ImageColorTransparent($this->gdimg_source);
  2563. if ($this->bg || (@$current_transparent_color >= 0)) {
  2564. $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
  2565. if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
  2566. return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
  2567. }
  2568. $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor);
  2569. ImageFilledRectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color);
  2570. }
  2571. }
  2572. $this->DebugMessage('CreateGDoutput() returning canvas "'.$this->thumbnail_width.'x'.$this->thumbnail_height.'"', __FILE__, __LINE__);
  2573. return true;
  2574. }
  2575. function SetOrientationDependantWidthHeight() {
  2576. $this->DebugMessage('SetOrientationDependantWidthHeight() starting with "'.$this->source_width.'"x"'.$this->source_height.'"', __FILE__, __LINE__);
  2577. if ($this->source_height > $this->source_width) {
  2578. // portrait
  2579. $this->w = phpthumb_functions::OneOfThese($this->wp, $this->w, $this->ws, $this->wl);
  2580. $this->h = phpthumb_functions::OneOfThese($this->hp, $this->h, $this->hs, $this->hl);
  2581. } elseif ($this->source_height < $this->source_width) {
  2582. // landscape
  2583. $this->w = phpthumb_functions::OneOfThese($this->wl, $this->w, $this->ws, $this->wp);
  2584. $this->h = phpthumb_functions::OneOfThese($this->hl, $this->h, $this->hs, $this->hp);
  2585. } else {
  2586. // square
  2587. $this->w = phpthumb_functions::OneOfThese($this->ws, $this->w, $this->wl, $this->wp);
  2588. $this->h = phpthumb_functions::OneOfThese($this->hs, $this->h, $this->hl, $this->hp);
  2589. }
  2590. //$this->w = round($this->w ? $this->w : (($this->h && $this->source_height) ? $this->h * $this->source_width / $this->source_height : $this->w));
  2591. //$this->h = round($this->h ? $this->h : (($this->w && $this->source_width) ? $this->w * $this->source_height / $this->source_width : $this->h));
  2592. $this->DebugMessage('SetOrientationDependantWidthHeight() setting w="'.intval($this->w).'", h="'.intval($this->h).'"', __FILE__, __LINE__);
  2593. return true;
  2594. }
  2595. function ExtractEXIFgetImageSize() {
  2596. $this->DebugMessage('starting ExtractEXIFgetImageSize()', __FILE__, __LINE__);
  2597. if (preg_match('#^http:#i', $this->src) && !$this->sourceFilename && $this->rawImageData) {
  2598. !$this->SourceDataToTempFile();
  2599. }
  2600. if (is_null($this->getimagesizeinfo)) {
  2601. if ($this->sourceFilename) {
  2602. $this->getimagesizeinfo = @GetImageSize($this->sourceFilename);
  2603. $this->source_width = $this->getimagesizeinfo[0];
  2604. $this->source_height = $this->getimagesizeinfo[1];
  2605. $this->DebugMessage('GetImageSize('.$this->sourceFilename.') says image is '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__);
  2606. } else {
  2607. $this->DebugMessage('skipping GetImageSize() because $this->sourceFilename is empty', __FILE__, __LINE__);
  2608. }
  2609. } else {
  2610. $this->DebugMessage('skipping GetImageSize() because !is_null($this->getimagesizeinfo)', __FILE__, __LINE__);
  2611. }
  2612. if (is_resource($this->gdimg_source)) {
  2613. $this->source_width = ImageSX($this->gdimg_source);
  2614. $this->source_height = ImageSY($this->gdimg_source);
  2615. $this->SetOrientationDependantWidthHeight();
  2616. } elseif ($this->rawImageData && !$this->sourceFilename) {
  2617. if ($this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
  2618. $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__);
  2619. } else {
  2620. $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__);
  2621. }
  2622. }
  2623. if (is_null($this->getimagesizeinfo)) {
  2624. $this->getimagesizeinfo = @GetImageSize($this->sourceFilename);
  2625. }
  2626. if (!empty($this->getimagesizeinfo)) {
  2627. // great
  2628. $this->getimagesizeinfo['filesize'] = @filesize($this->sourceFilename);
  2629. } elseif (!$this->rawImageData) {
  2630. $this->DebugMessage('GetImageSize("'.$this->sourceFilename.'") failed', __FILE__, __LINE__);
  2631. }
  2632. if ($this->config_prefer_imagemagick) {
  2633. if ($this->ImageMagickThumbnailToGD()) {
  2634. return true;
  2635. }
  2636. $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
  2637. }
  2638. $this->source_width = $this->getimagesizeinfo[0];
  2639. $this->source_height = $this->getimagesizeinfo[1];
  2640. $this->SetOrientationDependantWidthHeight();
  2641. if (phpthumb_functions::version_compare_replacement(phpversion(), '4.2.0', '>=') && function_exists('exif_read_data')) {
  2642. $this->exif_raw_data = @exif_read_data($this->sourceFilename, 0, true);
  2643. }
  2644. if (function_exists('exif_thumbnail') && ($this->getimagesizeinfo[2] == 2)) {
  2645. // Extract EXIF info from JPEGs
  2646. $this->exif_thumbnail_width = '';
  2647. $this->exif_thumbnail_height = '';
  2648. $this->exif_thumbnail_type = '';
  2649. // The parameters width, height and imagetype are available since PHP v4.3.0
  2650. if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.0', '>=')) {
  2651. $this->exif_thumbnail_data = @exif_thumbnail($this->sourceFilename, $this->exif_thumbnail_width, $this->exif_thumbnail_height, $this->exif_thumbnail_type);
  2652. } else {
  2653. // older versions of exif_thumbnail output an error message but NOT return false on failure
  2654. ob_start();
  2655. $this->exif_thumbnail_data = exif_thumbnail($this->sourceFilename);
  2656. $exit_thumbnail_error = ob_get_contents();
  2657. ob_end_clean();
  2658. if (!$exit_thumbnail_error && $this->exif_thumbnail_data) {
  2659. if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
  2660. $this->exif_thumbnail_width = ImageSX($gdimg_exif_temp);
  2661. $this->exif_thumbnail_height = ImageSY($gdimg_exif_temp);
  2662. $this->exif_thumbnail_type = 2; // (2 == JPEG) before PHP v4.3.0 only JPEG format EXIF thumbnails are returned
  2663. unset($gdimg_exif_temp);
  2664. } else {
  2665. return $this->ErrorImage('Failed - $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data) in '.__FILE__.' on line '.__LINE__);
  2666. }
  2667. }
  2668. }
  2669. } elseif (!function_exists('exif_thumbnail')) {
  2670. $this->DebugMessage('exif_thumbnail() does not exist, cannot extract EXIF thumbnail', __FILE__, __LINE__);
  2671. }
  2672. $this->DebugMessage('EXIF thumbnail extraction: (size='.strlen($this->exif_thumbnail_data).'; type="'.$this->exif_thumbnail_type.'"; '.intval($this->exif_thumbnail_width).'x'.intval($this->exif_thumbnail_height).')', __FILE__, __LINE__);
  2673. // see if EXIF thumbnail can be used directly with no processing
  2674. if ($this->config_use_exif_thumbnail_for_speed && $this->exif_thumbnail_data) {
  2675. while (true) {
  2676. if (!$this->xto) {
  2677. $source_ar = $this->source_width / $this->source_height;
  2678. $exif_ar = $this->exif_thumbnail_width / $this->exif_thumbnail_height;
  2679. if (number_format($source_ar, 2) != number_format($exif_ar, 2)) {
  2680. $this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar ('.$source_ar.' != '.$exif_ar.')', __FILE__, __LINE__);
  2681. break;
  2682. }
  2683. if ($this->w && ($this->w != $this->exif_thumbnail_width)) {
  2684. $this->DebugMessage('not using EXIF thumbnail because $this->w != $this->exif_thumbnail_width ('.$this->w.' != '.$this->exif_thumbnail_width.')', __FILE__, __LINE__);
  2685. break;
  2686. }
  2687. if ($this->h && ($this->h != $this->exif_thumbnail_height)) {
  2688. $this->DebugMessage('not using EXIF thumbnail because $this->h != $this->exif_thumbnail_height ('.$this->h.' != '.$this->exif_thumbnail_height.')', __FILE__, __LINE__);
  2689. break;
  2690. }
  2691. $CannotBeSetParameters = array('sx', 'sy', 'sh', 'sw', 'far', 'bg', 'bc', 'fltr', 'phpThumbDebug');
  2692. foreach ($CannotBeSetParameters as $parameter) {
  2693. if ($this->$parameter) {
  2694. break 2;
  2695. }
  2696. }
  2697. }
  2698. $this->DebugMessage('setting $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data)', __FILE__, __LINE__);
  2699. $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data);
  2700. $this->source_width = ImageSX($this->gdimg_source);
  2701. $this->source_height = ImageSY($this->gdimg_source);
  2702. return true;
  2703. }
  2704. }
  2705. if (($this->config_max_source_pixels > 0) && (($this->source_width * $this->source_height) > $this->config_max_source_pixels)) {
  2706. // Source image is larger than would fit in available PHP memory.
  2707. // If ImageMagick is installed, use it to generate the thumbnail.
  2708. // Else, if an EXIF thumbnail is available, use that as the source image.
  2709. // Otherwise, no choice but to fail with an error message
  2710. $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__);
  2711. if (!$this->config_prefer_imagemagick && $this->ImageMagickThumbnailToGD()) {
  2712. // excellent, we have a thumbnailed source image
  2713. return true;
  2714. }
  2715. }
  2716. return true;
  2717. }
  2718. function SetCacheFilename() {
  2719. if (!is_null($this->cache_filename)) {
  2720. $this->DebugMessage('$this->cache_filename already set, skipping SetCacheFilename()', __FILE__, __LINE__);
  2721. return true;
  2722. }
  2723. $this->setOutputFormat();
  2724. $this->setCacheDirectory();
  2725. if (!$this->config_cache_directory) {
  2726. $this->DebugMessage('SetCacheFilename() failed because $this->config_cache_directory is empty', __FILE__, __LINE__);
  2727. return false;
  2728. }
  2729. if (!$this->sourceFilename && !$this->rawImageData && $this->src) {
  2730. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
  2731. }
  2732. if ($this->config_cache_default_only_suffix && $this->sourceFilename) {
  2733. // simplified cache filenames:
  2734. // only use default parameters in phpThumb.config.php
  2735. // substitute source filename into * in $this->config_cache_default_only_suffix
  2736. // (eg: '*_thumb' becomes 'picture_thumb.jpg')
  2737. if (strpos($this->config_cache_default_only_suffix, '*') === false) {
  2738. $this->DebugMessage('aborting simplified caching filename because no * in "'.$this->config_cache_default_only_suffix.'"', __FILE__, __LINE__);
  2739. } else {
  2740. preg_match('#(.+)(\\.[a-z0-9]+)?$#i', basename($this->sourceFilename), $matches);
  2741. $this->cache_filename = $this->config_cache_directory.DIRECTORY_SEPARATOR.rawurlencode(str_replace('*', @$matches[1], $this->config_cache_default_only_suffix)).'.'.strtolower($this->thumbnailFormat);
  2742. return true;
  2743. }
  2744. }
  2745. $this->cache_filename = '';
  2746. $broad_directory_name = '';
  2747. if ($this->new) {
  2748. $broad_directory_name = strtolower(md5($this->new));
  2749. $this->cache_filename .= '_new'.$broad_directory_name;
  2750. } elseif ($this->md5s) {
  2751. // source image MD5 hash provided
  2752. $this->DebugMessage('SetCacheFilename() _raw set from $this->md5s = "'.$this->md5s.'"', __FILE__, __LINE__);
  2753. $broad_directory_name = $this->md5s;
  2754. $this->cache_filename .= '_raw'.$this->md5s;
  2755. } elseif (!$this->src && $this->rawImageData) {
  2756. $this->DebugMessage('SetCacheFilename() _raw set from md5($this->rawImageData) = "'.md5($this->rawImageData).'"', __FILE__, __LINE__);
  2757. $broad_directory_name = strtolower(md5($this->rawImageData));
  2758. $this->cache_filename .= '_raw'.$broad_directory_name;
  2759. } else {
  2760. $this->DebugMessage('SetCacheFilename() _src set from md5($this->sourceFilename) "'.$this->sourceFilename.'" = "'.md5($this->sourceFilename).'"', __FILE__, __LINE__);
  2761. $broad_directory_name = strtolower(md5($this->sourceFilename));
  2762. $this->cache_filename .= '_src'.$broad_directory_name;
  2763. }
  2764. if (@$_SERVER['HTTP_REFERER'] && $this->config_nooffsitelink_enabled) {
  2765. $parsed_url1 = @phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
  2766. $parsed_url2 = @phpthumb_functions::ParseURLbetter('http://'.@$_SERVER['HTTP_HOST']);
  2767. if (@$parsed_url1['host'] && @$parsed_url2['host'] && ($parsed_url1['host'] != $parsed_url2['host'])) {
  2768. // include "_offsite" only if nooffsitelink_enabled and if referrer doesn't match the domain of the current server
  2769. $this->cache_filename .= '_offsite';
  2770. }
  2771. }
  2772. $ParametersString = '';
  2773. if ($this->fltr && is_array($this->fltr)) {
  2774. $ParametersString .= '_fltr'.implode('_fltr', $this->fltr);
  2775. }
  2776. $FilenameParameters1 = array('ar', 'bg', 'bc', 'far', 'sx', 'sy', 'sw', 'sh', 'zc');
  2777. foreach ($FilenameParameters1 as $key) {
  2778. if ($this->$key) {
  2779. $ParametersString .= '_'.$key.$this->$key;
  2780. }
  2781. }
  2782. $FilenameParameters2 = array('h', 'w', 'wl', 'wp', 'ws', 'hp', 'hs', 'xto', 'ra', 'iar', 'aoe', 'maxb', 'sfn', 'dpi');
  2783. foreach ($FilenameParameters2 as $key) {
  2784. if ($this->$key) {
  2785. $ParametersString .= '_'.$key.intval($this->$key);
  2786. }
  2787. }
  2788. if ($this->thumbnailFormat == 'jpeg') {
  2789. // only JPEG output has variable quality option
  2790. $ParametersString .= '_q'.intval($this->thumbnailQuality);
  2791. }
  2792. $this->DebugMessage('SetCacheFilename() _par set from md5('.$ParametersString.')', __FILE__, __LINE__);
  2793. $this->cache_filename .= '_par'.strtolower(md5($ParametersString));
  2794. if ($this->md5s) {
  2795. // source image MD5 hash provided
  2796. // do not source image modification date --
  2797. // cached image will be used even if file was modified or removed
  2798. } elseif (!$this->config_cache_source_filemtime_ignore_remote && preg_match('#^(f|ht)tps?\://#i', $this->src)) {
  2799. $this->cache_filename .= '_dat'.intval(phpthumb_functions::filedate_remote($this->src));
  2800. } elseif (!$this->config_cache_source_filemtime_ignore_local && $this->src && !$this->rawImageData) {
  2801. $this->cache_filename .= '_dat'.intval(@filemtime($this->sourceFilename));
  2802. }
  2803. $this->cache_filename .= '.'.strtolower($this->thumbnailFormat);
  2804. $broad_directories = '';
  2805. for ($i = 0; $i < $this->config_cache_directory_depth; $i++) {
  2806. $broad_directories .= DIRECTORY_SEPARATOR.substr($broad_directory_name, 0, $i + 1);
  2807. }
  2808. $this->cache_filename = $this->config_cache_directory.$broad_directories.DIRECTORY_SEPARATOR.$this->config_cache_prefix.rawurlencode($this->cache_filename);
  2809. return true;
  2810. }
  2811. function SourceImageIsTooLarge($width, $height) {
  2812. if (!$this->config_max_source_pixels) {
  2813. return false;
  2814. }
  2815. if (function_exists('memory_get_usage')) {
  2816. $available_memory = max(intval(ini_get('memory_limit')), intval(get_cfg_var('memory_limit'))) * 1048576;
  2817. $available_memory -= memory_get_usage();
  2818. return (bool) (($width * $height * 5) > $available_memory);
  2819. }
  2820. return (bool) (($width * $height) > $this->config_max_source_pixels);
  2821. }
  2822. function ImageCreateFromFilename($filename) {
  2823. // try to create GD image source directly via GD, if possible,
  2824. // rather than buffering to memory and creating with ImageCreateFromString
  2825. $ImageCreateWasAttempted = false;
  2826. $gd_image = false;
  2827. $this->DebugMessage('starting ImageCreateFromFilename('.$filename.')', __FILE__, __LINE__);
  2828. if ($filename && ($getimagesizeinfo = @GetImageSize($filename))) {
  2829. if (!$this->SourceImageIsTooLarge($getimagesizeinfo[0], $getimagesizeinfo[1])) {
  2830. $ImageCreateFromFunction = array(
  2831. 1 => 'ImageCreateFromGIF',
  2832. 2 => 'ImageCreateFromJPEG',
  2833. 3 => 'ImageCreateFromPNG',
  2834. 15 => 'ImageCreateFromWBMP',
  2835. );
  2836. $this->DebugMessage('ImageCreateFromFilename found ($getimagesizeinfo[2]=='.@$getimagesizeinfo[2].')', __FILE__, __LINE__);
  2837. switch (@$getimagesizeinfo[2]) {
  2838. case 1: // GIF
  2839. case 2: // JPEG
  2840. case 3: // PNG
  2841. case 15: // WBMP
  2842. $ImageCreateFromFunctionName = $ImageCreateFromFunction[$getimagesizeinfo[2]];
  2843. if (function_exists($ImageCreateFromFunctionName)) {
  2844. $this->DebugMessage('Calling '.$ImageCreateFromFunctionName.'('.$filename.')', __FILE__, __LINE__);
  2845. $ImageCreateWasAttempted = true;
  2846. $gd_image = $ImageCreateFromFunctionName($filename);
  2847. } else {
  2848. $this->DebugMessage('NOT calling '.$ImageCreateFromFunctionName.'('.$filename.') because !function_exists('.$ImageCreateFromFunctionName.')', __FILE__, __LINE__);
  2849. }
  2850. break;
  2851. case 4: // SWF
  2852. case 5: // PSD
  2853. case 6: // BMP
  2854. case 7: // TIFF (LE)
  2855. case 8: // TIFF (BE)
  2856. case 9: // JPC
  2857. case 10: // JP2
  2858. case 11: // JPX
  2859. case 12: // JB2
  2860. case 13: // SWC
  2861. case 14: // IFF
  2862. case 16: // XBM
  2863. $this->DebugMessage('No built-in image creation function for image type "'.@$getimagesizeinfo[2].'" ($getimagesizeinfo[2])', __FILE__, __LINE__);
  2864. break;
  2865. default:
  2866. $this->DebugMessage('Unknown value for $getimagesizeinfo[2]: "'.@$getimagesizeinfo[2].'"', __FILE__, __LINE__);
  2867. break;
  2868. }
  2869. } else {
  2870. $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__);
  2871. return false;
  2872. }
  2873. } else {
  2874. $this->DebugMessage('empty $filename or GetImageSize('.$filename.') failed', __FILE__, __LINE__);
  2875. }
  2876. if (!$gd_image) {
  2877. // cannot create from filename, attempt to create source image with ImageCreateFromString, if possible
  2878. if ($ImageCreateWasAttempted) {
  2879. $this->DebugMessage(@$ImageCreateFromFunctionName.'() was attempted but FAILED', __FILE__, __LINE__);
  2880. }
  2881. $this->DebugMessage('Populating $rawimagedata', __FILE__, __LINE__);
  2882. $rawimagedata = '';
  2883. if ($fp = @fopen($filename, 'rb')) {
  2884. $filesize = filesize($filename);
  2885. $blocksize = 8192;
  2886. $blockreads = ceil($filesize / $blocksize);
  2887. for ($i = 0; $i < $blockreads; $i++) {
  2888. $rawimagedata .= fread($fp, $blocksize);
  2889. }
  2890. fclose($fp);
  2891. } else {
  2892. $this->DebugMessage('cannot fopen('.$filename.')', __FILE__, __LINE__);
  2893. }
  2894. if ($rawimagedata) {
  2895. $this->DebugMessage('attempting ImageCreateFromStringReplacement($rawimagedata ('.strlen($rawimagedata).' bytes), true)', __FILE__, __LINE__);
  2896. $gd_image = $this->ImageCreateFromStringReplacement($rawimagedata, true);
  2897. }
  2898. }
  2899. return $gd_image;
  2900. }
  2901. function SourceImageToGD() {
  2902. if (is_resource($this->gdimg_source)) {
  2903. $this->source_width = ImageSX($this->gdimg_source);
  2904. $this->source_height = ImageSY($this->gdimg_source);
  2905. $this->DebugMessage('skipping SourceImageToGD() because $this->gdimg_source is already a resource ('.$this->source_width.'x'.$this->source_height.')', __FILE__, __LINE__);
  2906. return true;
  2907. }
  2908. $this->DebugMessage('starting SourceImageToGD()', __FILE__, __LINE__);
  2909. if ($this->ImageMagickThumbnailToGD()) {
  2910. // excellent, we have a thumbnailed source image
  2911. $this->DebugMessage('ImageMagickThumbnailToGD() succeeded', __FILE__, __LINE__);
  2912. } elseif (!$this->gdimg_source && $this->rawImageData) {
  2913. if ($this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
  2914. $memory_get_usage = (function_exists('memory_get_usage') ? memory_get_usage() : 0);
  2915. 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).');
  2916. }
  2917. if ($this->md5s && ($this->md5s != md5($this->rawImageData))) {
  2918. return $this->ErrorImage('$this->md5s != md5($this->rawImageData)'."\n".'"'.$this->md5s.'" != '."\n".'"'.md5($this->rawImageData).'"');
  2919. }
  2920. //if ($this->issafemode) {
  2921. // return $this->ErrorImage('Cannot generate thumbnails from raw image data when PHP SAFE_MODE enabled');
  2922. //}
  2923. $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->rawImageData);
  2924. if (!$this->gdimg_source) {
  2925. if (substr($this->rawImageData, 0, 2) === 'BM') {
  2926. $this->getimagesizeinfo[2] = 6; // BMP
  2927. } elseif (substr($this->rawImageData, 0, 4) === 'II'."\x2A\x00") {
  2928. $this->getimagesizeinfo[2] = 7; // TIFF (littlendian)
  2929. } elseif (substr($this->rawImageData, 0, 4) === 'MM'."\x00\x2A") {
  2930. $this->getimagesizeinfo[2] = 8; // TIFF (bigendian)
  2931. }
  2932. $this->DebugMessage('SourceImageToGD.ImageCreateFromStringReplacement() failed with unknown image type "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).')', __FILE__, __LINE__);
  2933. // return $this->ErrorImage('Unknown image type identified by "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).') in SourceImageToGD()['.__LINE__.']');
  2934. }
  2935. } elseif (!$this->gdimg_source && $this->sourceFilename) {
  2936. if ($this->md5s && ($this->md5s != phpthumb_functions::md5_file_safe($this->sourceFilename))) {
  2937. return $this->ErrorImage('$this->md5s != md5(sourceFilename)'."\n".'"'.$this->md5s.'" != '."\n".'"'.phpthumb_functions::md5_file_safe($this->sourceFilename).'"');
  2938. }
  2939. switch (@$this->getimagesizeinfo[2]) {
  2940. case 1:
  2941. case 3:
  2942. // GIF or PNG input file may have transparency
  2943. $this->is_alpha = true;
  2944. break;
  2945. }
  2946. if (!$this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
  2947. $this->gdimg_source = $this->ImageCreateFromFilename($this->sourceFilename);
  2948. }
  2949. }
  2950. while (true) {
  2951. if ($this->gdimg_source) {
  2952. $this->DebugMessage('Not using EXIF thumbnail data because $this->gdimg_source is already set', __FILE__, __LINE__);
  2953. break;
  2954. }
  2955. if (!$this->exif_thumbnail_data) {
  2956. $this->DebugMessage('Not using EXIF thumbnail data because $this->exif_thumbnail_data is empty', __FILE__, __LINE__);
  2957. break;
  2958. }
  2959. if (ini_get('safe_mode')) {
  2960. if (!$this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
  2961. $this->DebugMessage('Using EXIF thumbnail data because source image too large and safe_mode enabled', __FILE__, __LINE__);
  2962. $this->aoe = true;
  2963. } else {
  2964. break;
  2965. }
  2966. } else {
  2967. if (!$this->config_use_exif_thumbnail_for_speed) {
  2968. $this->DebugMessage('Not using EXIF thumbnail data because $this->config_use_exif_thumbnail_for_speed is FALSE', __FILE__, __LINE__);
  2969. break;
  2970. }
  2971. if (($this->thumbnailCropX != 0) || ($this->thumbnailCropY != 0)) {
  2972. $this->DebugMessage('Not using EXIF thumbnail data because source cropping is enabled ('.$this->thumbnailCropX.','.$this->thumbnailCropY.')', __FILE__, __LINE__);
  2973. break;
  2974. }
  2975. if (($this->w > $this->exif_thumbnail_width) || ($this->h > $this->exif_thumbnail_height)) {
  2976. $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__);
  2977. break;
  2978. }
  2979. $source_ar = $this->source_width / $this->source_height;
  2980. $exif_ar = $this->exif_thumbnail_width / $this->exif_thumbnail_height;
  2981. if (number_format($source_ar, 2) != number_format($exif_ar, 2)) {
  2982. $this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar ('.$source_ar.' != '.$exif_ar.')', __FILE__, __LINE__);
  2983. break;
  2984. }
  2985. }
  2986. // EXIF thumbnail exists, and is equal to or larger than destination thumbnail, and will be use as source image
  2987. $this->DebugMessage('Trying to use EXIF thumbnail as source image', __FILE__, __LINE__);
  2988. if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
  2989. $this->DebugMessage('Successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
  2990. $this->gdimg_source = $gdimg_exif_temp;
  2991. $this->source_width = $this->exif_thumbnail_width;
  2992. $this->source_height = $this->exif_thumbnail_height;
  2993. $this->thumbnailCropW = $this->source_width;
  2994. $this->thumbnailCropH = $this->source_height;
  2995. return true;
  2996. } else {
  2997. $this->DebugMessage('$this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false) failed', __FILE__, __LINE__);
  2998. }
  2999. break;
  3000. }
  3001. if (!$this->gdimg_source) {
  3002. $this->DebugMessage('$this->gdimg_source is still empty', __FILE__, __LINE__);
  3003. $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
  3004. $imageHeader = '';
  3005. $gd_info = gd_info();
  3006. $GDreadSupport = false;
  3007. switch (@$this->getimagesizeinfo[2]) {
  3008. case 1:
  3009. $imageHeader = 'Content-Type: image/gif';
  3010. $GDreadSupport = (bool) @$gd_info['GIF Read Support'];
  3011. break;
  3012. case 2:
  3013. $imageHeader = 'Content-Type: image/jpeg';
  3014. $GDreadSupport = (bool) @$gd_info['JPG Support'];
  3015. break;
  3016. case 3:
  3017. $imageHeader = 'Content-Type: image/png';
  3018. $GDreadSupport = (bool) @$gd_info['PNG Support'];
  3019. break;
  3020. }
  3021. if ($imageHeader) {
  3022. // cannot create image for whatever reason (maybe ImageCreateFromJPEG et al are not available?)
  3023. // and ImageMagick is not available either, no choice but to output original (not resized/modified) data and exit
  3024. if ($this->config_error_die_on_source_failure) {
  3025. $errormessages = array();
  3026. $errormessages[] = 'All attempts to create GD image source failed.';
  3027. if ($this->fatalerror) {
  3028. $errormessages[] = $this->fatalerror;
  3029. }
  3030. if ($this->issafemode) {
  3031. $errormessages[] = 'Safe Mode enabled, therefore ImageMagick is unavailable. (disable Safe Mode if possible)';
  3032. } elseif (!$this->ImageMagickVersion()) {
  3033. $errormessages[] = 'ImageMagick is not installed (it is highly recommended that you install it).';
  3034. }
  3035. if ($this->SourceImageIsTooLarge($this->getimagesizeinfo[0], $this->getimagesizeinfo[1])) {
  3036. $memory_get_usage = (function_exists('memory_get_usage') ? memory_get_usage() : 0);
  3037. $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).';
  3038. } elseif (!$GDreadSupport) {
  3039. $errormessages[] = 'GD does not have read support for "'.$imageHeader.'".';
  3040. } else {
  3041. $errormessages[] = 'Source image probably corrupt.';
  3042. }
  3043. $this->ErrorImage(implode("\n", $errormessages));
  3044. } else {
  3045. $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');
  3046. //$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__);
  3047. //if (!$this->phpThumbDebug) {
  3048. // header($imageHeader);
  3049. // echo $this->rawImageData;
  3050. // exit;
  3051. //}
  3052. return false;
  3053. }
  3054. }
  3055. //switch (substr($this->rawImageData, 0, 2)) {
  3056. // case 'BM':
  3057. switch (@$this->getimagesizeinfo[2]) {
  3058. case 6:
  3059. ob_start();
  3060. if (!@include_once(dirname(__FILE__).'/phpthumb.bmp.php')) {
  3061. ob_end_clean();
  3062. return $this->ErrorImage('include_once('.dirname(__FILE__).'/phpthumb.bmp.php) failed');
  3063. }
  3064. ob_end_clean();
  3065. if ($fp = @fopen($this->sourceFilename, 'rb')) {
  3066. $this->rawImageData = '';
  3067. while (!feof($fp)) {
  3068. $this->rawImageData .= fread($fp, 32768);
  3069. }
  3070. fclose($fp);
  3071. }
  3072. $phpthumb_bmp = new phpthumb_bmp();
  3073. $this->gdimg_source = $phpthumb_bmp->phpthumb_bmp2gd($this->rawImageData, (phpthumb_functions::gd_version() >= 2.0));
  3074. unset($phpthumb_bmp);
  3075. if ($this->gdimg_source) {
  3076. $this->DebugMessage('$phpthumb_bmp->phpthumb_bmp2gd() succeeded', __FILE__, __LINE__);
  3077. } else {
  3078. return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on BMP source conversion' : 'phpthumb_bmp2gd() failed');
  3079. }
  3080. break;
  3081. //}
  3082. //switch (substr($this->rawImageData, 0, 4)) {
  3083. // case 'II'."\x2A\x00":
  3084. // case 'MM'."\x00\x2A":
  3085. case 7:
  3086. case 8:
  3087. return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on TIFF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support TIFF source images without it');
  3088. break;
  3089. //case "\xD7\xCD\xC6\x9A":
  3090. // return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on WMF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support WMF source images without it');
  3091. // break;
  3092. }
  3093. if (!$this->gdimg_source) {
  3094. $HeaderFourBytes = '';
  3095. if ($this->rawImageData) {
  3096. $HeaderFourBytes = substr($this->rawImageData, 0, 4);
  3097. } elseif ($this->sourceFilename) {
  3098. if ($fp = @fopen($this->sourceFilename, 'rb')) {
  3099. $HeaderFourBytes = fread($fp, 4);
  3100. fclose($fp);
  3101. } else {
  3102. return $this->ErrorImage('failed to open "'.$this->sourceFilename.'" SourceImageToGD() ['.__LINE__.']');
  3103. }
  3104. } else {
  3105. return $this->ErrorImage('Unable to create image, neither filename nor image data suppplied in SourceImageToGD() ['.__LINE__.']');
  3106. }
  3107. if (!$this->ImageMagickVersion() && !phpthumb_functions::gd_version()) {
  3108. 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.');
  3109. } elseif ($HeaderFourBytes == "\xD7\xCD\xC6\x9A") { // WMF
  3110. return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on WMF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support WMF source images without it');
  3111. } elseif ($HeaderFourBytes == '%PDF') { // "%PDF"
  3112. 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');
  3113. } elseif (substr($HeaderFourBytes, 0, 3) == "\xFF\xD8\xFF") { // JPEG
  3114. return $this->ErrorImage('Image (JPEG) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
  3115. } elseif ($HeaderFourBytes == '%PNG') { // "%PNG"
  3116. return $this->ErrorImage('Image (PNG) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
  3117. } elseif (substr($HeaderFourBytes, 0, 3) == 'GIF') { // GIF
  3118. return $this->ErrorImage('Image (GIF) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
  3119. }
  3120. return $this->ErrorImage('Unknown image type identified by "'.$HeaderFourBytes.'" ('.phpthumb_functions::HexCharDisplay($HeaderFourBytes).') in SourceImageToGD() ['.__LINE__.']');
  3121. }
  3122. }
  3123. if (!$this->gdimg_source) {
  3124. if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
  3125. $this->DebugMessage('All other attempts failed, but successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
  3126. $this->gdimg_source = $gdimg_exif_temp;
  3127. // override allow-enlarging setting if EXIF thumbnail is the only source available
  3128. // otherwise thumbnails larger than the EXIF thumbnail will be created at EXIF size
  3129. $this->aoe = true;
  3130. return true;
  3131. }
  3132. return false;
  3133. }
  3134. $this->source_width = ImageSX($this->gdimg_source);
  3135. $this->source_height = ImageSY($this->gdimg_source);
  3136. return true;
  3137. }
  3138. function phpThumbDebugVarDump($var) {
  3139. if (is_null($var)) {
  3140. return 'NULL';
  3141. } elseif (is_bool($var)) {
  3142. return ($var ? 'TRUE' : 'FALSE');
  3143. } elseif (is_string($var)) {
  3144. return 'string('.strlen($var).')'.str_repeat(' ', max(0, 3 - strlen(strlen($var)))).' "'.$var.'"';
  3145. } elseif (is_int($var)) {
  3146. return 'integer '.$var;
  3147. } elseif (is_float($var)) {
  3148. return 'float '.$var;
  3149. } elseif (is_array($var)) {
  3150. ob_start();
  3151. var_dump($var);
  3152. $vardumpoutput = ob_get_contents();
  3153. ob_end_clean();
  3154. return strtr($vardumpoutput, "\n\r\t", ' ');
  3155. }
  3156. return gettype($var);
  3157. }
  3158. function phpThumbDebug($level='') {
  3159. if ($level && ($this->phpThumbDebug !== $level)) {
  3160. return true;
  3161. }
  3162. if ($this->config_disable_debug) {
  3163. return $this->ErrorImage('phpThumbDebug disabled');
  3164. }
  3165. $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');
  3166. $ParameterNames = array('src', 'new', 'w', 'h', 'f', 'q', 'sx', 'sy', 'sw', 'sh', 'far', 'bg', 'bc', 'file', 'goto', 'err', 'xto', 'ra', 'ar', 'aoe', 'iar', 'maxb');
  3167. $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', 'allow_parameter_file', 'allow_parameter_goto', 'max_source_pixels', 'use_exif_thumbnail_for_speed', 'border_hexcolor', 'background_hexcolor', 'ttf_directory', 'disable_pathinfo_parsing', 'disable_imagecopyresampled');
  3168. $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');
  3169. $DebugOutput = array();
  3170. $DebugOutput[] = 'phpThumb() version = '.$this->phpthumb_version;
  3171. $DebugOutput[] = 'phpversion() = '.@phpversion();
  3172. $DebugOutput[] = 'PHP_OS = '.PHP_OS;
  3173. $DebugOutput[] = '$_SERVER[SERVER_SOFTWARE] = '.@$_SERVER['SERVER_SOFTWARE'];
  3174. $DebugOutput[] = '__FILE__ = '.__FILE__;
  3175. $DebugOutput[] = 'realpath(.) = '.@realpath('.');
  3176. $DebugOutput[] = '$_SERVER[PHP_SELF] = '.@$_SERVER['PHP_SELF'];
  3177. $DebugOutput[] = '$_SERVER[HOST_NAME] = '.@$_SERVER['HOST_NAME'];
  3178. $DebugOutput[] = '$_SERVER[HTTP_REFERER] = '.@$_SERVER['HTTP_REFERER'];
  3179. $DebugOutput[] = '$_SERVER[QUERY_STRING] = '.@$_SERVER['QUERY_STRING'];
  3180. $DebugOutput[] = '$_SERVER[PATH_INFO] = '.@$_SERVER['PATH_INFO'];
  3181. $DebugOutput[] = '$_SERVER[DOCUMENT_ROOT] = '.@$_SERVER['DOCUMENT_ROOT'];
  3182. $DebugOutput[] = 'getenv(DOCUMENT_ROOT) = '.@getenv('DOCUMENT_ROOT');
  3183. $DebugOutput[] = '';
  3184. $DebugOutput[] = 'get_magic_quotes_gpc() = '.$this->phpThumbDebugVarDump(@get_magic_quotes_gpc());
  3185. $DebugOutput[] = 'get_magic_quotes_runtime() = '.$this->phpThumbDebugVarDump(@get_magic_quotes_runtime());
  3186. $DebugOutput[] = 'error_reporting() = '.$this->phpThumbDebugVarDump(error_reporting());
  3187. $DebugOutput[] = 'ini_get(error_reporting) = '.$this->phpThumbDebugVarDump(@ini_get('error_reporting'));
  3188. $DebugOutput[] = 'ini_get(display_errors) = '.$this->phpThumbDebugVarDump(@ini_get('display_errors'));
  3189. $DebugOutput[] = 'ini_get(allow_url_fopen) = '.$this->phpThumbDebugVarDump(@ini_get('allow_url_fopen'));
  3190. $DebugOutput[] = 'ini_get(disable_functions) = '.$this->phpThumbDebugVarDump(@ini_get('disable_functions'));
  3191. $DebugOutput[] = 'get_cfg_var(disable_functions) = '.$this->phpThumbDebugVarDump(@get_cfg_var('disable_functions'));
  3192. $DebugOutput[] = 'ini_get(safe_mode) = '.$this->phpThumbDebugVarDump(@ini_get('safe_mode'));
  3193. $DebugOutput[] = 'ini_get(open_basedir) = '.$this->phpThumbDebugVarDump(@ini_get('open_basedir'));
  3194. $DebugOutput[] = 'ini_get(max_execution_time) = '.$this->phpThumbDebugVarDump(@ini_get('max_execution_time'));
  3195. $DebugOutput[] = 'ini_get(memory_limit) = '.$this->phpThumbDebugVarDump(@ini_get('memory_limit'));
  3196. $DebugOutput[] = 'get_cfg_var(memory_limit) = '.$this->phpThumbDebugVarDump(@get_cfg_var('memory_limit'));
  3197. $DebugOutput[] = 'memory_get_usage() = '.(function_exists('memory_get_usage') ? $this->phpThumbDebugVarDump(@memory_get_usage()) : 'n/a');
  3198. $DebugOutput[] = '';
  3199. $DebugOutput[] = '$this->config_prefer_imagemagick = '.$this->phpThumbDebugVarDump($this->config_prefer_imagemagick);
  3200. $DebugOutput[] = '$this->config_imagemagick_path = '.$this->phpThumbDebugVarDump($this->config_imagemagick_path);
  3201. $DebugOutput[] = '$this->ImageMagickWhichConvert() = '.$this->ImageMagickWhichConvert();
  3202. $IMpathUsed = ($this->config_imagemagick_path ? $this->config_imagemagick_path : $this->ImageMagickWhichConvert());
  3203. $DebugOutput[] = '[actual ImageMagick path used] = '.$this->phpThumbDebugVarDump($IMpathUsed);
  3204. $DebugOutput[] = 'file_exists([actual ImageMagick path used]) = '.$this->phpThumbDebugVarDump(@file_exists($IMpathUsed));
  3205. $DebugOutput[] = 'ImageMagickVersion(false) = '.$this->ImageMagickVersion(false);
  3206. $DebugOutput[] = 'ImageMagickVersion(true) = '.$this->ImageMagickVersion(true);
  3207. $DebugOutput[] = '';
  3208. $DebugOutput[] = '$this->config_cache_directory = '.$this->phpThumbDebugVarDump($this->config_cache_directory);
  3209. $DebugOutput[] = '$this->config_cache_directory_depth = '.$this->phpThumbDebugVarDump($this->config_cache_directory_depth);
  3210. $DebugOutput[] = '$this->config_cache_disable_warning = '.$this->phpThumbDebugVarDump($this->config_cache_disable_warning);
  3211. $DebugOutput[] = '$this->config_cache_maxage = '.$this->phpThumbDebugVarDump($this->config_cache_maxage);
  3212. $DebugOutput[] = '$this->config_cache_maxsize = '.$this->phpThumbDebugVarDump($this->config_cache_maxsize);
  3213. $DebugOutput[] = '$this->config_cache_maxfiles = '.$this->phpThumbDebugVarDump($this->config_cache_maxfiles);
  3214. $DebugOutput[] = '$this->config_cache_force_passthru = '.$this->phpThumbDebugVarDump($this->config_cache_force_passthru);
  3215. $DebugOutput[] = '$this->cache_filename = '.$this->phpThumbDebugVarDump($this->cache_filename);
  3216. $DebugOutput[] = 'is_readable($this->config_cache_directory) = '.$this->phpThumbDebugVarDump(@is_readable($this->config_cache_directory));
  3217. $DebugOutput[] = 'is_writable($this->config_cache_directory) = '.$this->phpThumbDebugVarDump(@is_writable($this->config_cache_directory));
  3218. $DebugOutput[] = 'is_readable($this->cache_filename) = '.$this->phpThumbDebugVarDump(@is_readable($this->cache_filename));
  3219. $DebugOutput[] = 'is_writable($this->cache_filename) = '.(@file_exists($this->cache_filename) ? $this->phpThumbDebugVarDump(@is_writable($this->cache_filename)) : 'n/a');
  3220. $DebugOutput[] = '';
  3221. foreach ($ConfigVariableNames as $varname) {
  3222. $varname = 'config_'.$varname;
  3223. $value = $this->$varname;
  3224. $DebugOutput[] = '$this->'.str_pad($varname, 37, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3225. }
  3226. $DebugOutput[] = '';
  3227. foreach ($OtherVariableNames as $varname) {
  3228. $value = $this->$varname;
  3229. $DebugOutput[] = '$this->'.str_pad($varname, 27, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3230. }
  3231. $DebugOutput[] = 'strlen($this->rawImageData) = '.strlen(@$this->rawImageData);
  3232. $DebugOutput[] = 'strlen($this->exif_thumbnail_data) = '.strlen(@$this->exif_thumbnail_data);
  3233. $DebugOutput[] = '';
  3234. foreach ($ParameterNames as $varname) {
  3235. $value = $this->$varname;
  3236. $DebugOutput[] = '$this->'.str_pad($varname, 4, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3237. }
  3238. $DebugOutput[] = '';
  3239. foreach ($FunctionsExistance as $functionname) {
  3240. $DebugOutput[] = 'builtin_function_exists('.$functionname.')'.str_repeat(' ', 23 - strlen($functionname)).' = '.$this->phpThumbDebugVarDump(phpthumb_functions::builtin_function_exists($functionname));
  3241. }
  3242. $DebugOutput[] = '';
  3243. $gd_info = gd_info();
  3244. foreach ($gd_info as $key => $value) {
  3245. $DebugOutput[] = 'gd_info.'.str_pad($key, 34, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3246. }
  3247. $DebugOutput[] = '';
  3248. $exif_info = phpthumb_functions::exif_info();
  3249. foreach ($exif_info as $key => $value) {
  3250. $DebugOutput[] = 'exif_info.'.str_pad($key, 26, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3251. }
  3252. $DebugOutput[] = '';
  3253. if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) {
  3254. foreach ($ApacheLookupURIarray as $key => $value) {
  3255. $DebugOutput[] = 'ApacheLookupURIarray.'.str_pad($key, 15, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3256. }
  3257. } else {
  3258. $DebugOutput[] = 'ApacheLookupURIarray() -- FAILED';
  3259. }
  3260. $DebugOutput[] = '';
  3261. if (isset($_GET) && is_array($_GET)) {
  3262. foreach ($_GET as $key => $value) {
  3263. $DebugOutput[] = '$_GET['.$key.']'.str_repeat(' ', 30 - strlen($key)).'= '.$this->phpThumbDebugVarDump($value);
  3264. }
  3265. }
  3266. if (isset($_POST) && is_array($_POST)) {
  3267. foreach ($_POST as $key => $value) {
  3268. $DebugOutput[] = '$_POST['.$key.']'.str_repeat(' ', 29 - strlen($key)).'= '.$this->phpThumbDebugVarDump($value);
  3269. }
  3270. }
  3271. $DebugOutput[] = '';
  3272. $DebugOutput[] = '$this->debugmessages:';
  3273. foreach ($this->debugmessages as $errorstring) {
  3274. $DebugOutput[] = ' * '.$errorstring;
  3275. }
  3276. $DebugOutput[] = '';
  3277. $DebugOutput[] = '$this->debugtiming:';
  3278. foreach ($this->debugtiming as $timestamp => $timingstring) {
  3279. $DebugOutput[] = ' * '.$timestamp.' '.$timingstring;
  3280. }
  3281. $DebugOutput[] = ' * Total processing time: '.number_format(max(array_keys($this->debugtiming)) - min(array_keys($this->debugtiming)), 6);
  3282. $this->f = (isset($_GET['f']) ? $_GET['f'] : $this->f); // debug modes 0-2 don't recognize text mode otherwise
  3283. return $this->ErrorImage(implode("\n", $DebugOutput), 700, 500, true);
  3284. }
  3285. function FatalError($text) {
  3286. if (is_null($this->fatalerror)) {
  3287. $this->fatalerror = $text;
  3288. }
  3289. return true;
  3290. }
  3291. function ErrorImage($text, $width=0, $height=0, $forcedisplay=false) {
  3292. $width = ($width ? $width : $this->config_error_image_width);
  3293. $height = ($height ? $height : $this->config_error_image_height);
  3294. $text = 'phpThumb() v'.$this->phpthumb_version."\n\n".$text;
  3295. if ($this->config_disable_debug) {
  3296. $text = 'Error messages disabled';
  3297. }
  3298. $this->FatalError($text);
  3299. $this->DebugMessage($text, __FILE__, __LINE__);
  3300. if ($this->phpThumbDebug && !$forcedisplay) {
  3301. return false;
  3302. }
  3303. if (!$this->config_error_die_on_error && !$forcedisplay) {
  3304. return false;
  3305. }
  3306. if ($this->config_error_silent_die_on_error) {
  3307. exit;
  3308. }
  3309. if ($this->err || $this->config_error_message_image_default) {
  3310. // Show generic custom error image instead of error message
  3311. // for use on production sites where you don't want debug messages
  3312. if (($this->err == 'showerror') || $this->phpThumbDebug) {
  3313. // fall through and actually show error message even if default error image is set
  3314. } else {
  3315. header('Location: '.($this->err ? $this->err : $this->config_error_message_image_default));
  3316. exit;
  3317. }
  3318. }
  3319. $this->setOutputFormat();
  3320. if (!$this->thumbnailFormat || (phpthumb_functions::gd_version() < 1)) {
  3321. $this->thumbnailFormat = 'text';
  3322. }
  3323. if (@$this->thumbnailFormat == 'text') {
  3324. // bypass all GD functions and output text error message
  3325. if (!headers_sent()) {
  3326. header('Content-type: text/plain');
  3327. echo $text;
  3328. } else {
  3329. echo '<pre>'.htmlspecialchars($text).'</pre>';
  3330. }
  3331. exit;
  3332. }
  3333. $FontWidth = ImageFontWidth($this->config_error_fontsize);
  3334. $FontHeight = ImageFontHeight($this->config_error_fontsize);
  3335. $LinesOfText = explode("\n", @wordwrap($text, floor($width / $FontWidth), "\n", true));
  3336. $height = max($height, count($LinesOfText) * $FontHeight);
  3337. $headers_file = '';
  3338. $headers_line = '';
  3339. if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.0', '>=') && headers_sent($headers_file, $headers_line)) {
  3340. 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>';
  3341. } elseif (headers_sent()) {
  3342. echo "\n".'**Headers already sent, dumping error message as text:**<br><pre>'."\n\n".$text."\n".'</pre>';
  3343. } elseif ($gdimg_error = ImageCreate($width, $height)) {
  3344. $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_bgcolor, true);
  3345. $text_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_textcolor, true);
  3346. ImageFilledRectangle($gdimg_error, 0, 0, $width, $height, $background_color);
  3347. $lineYoffset = 0;
  3348. foreach ($LinesOfText as $line) {
  3349. ImageString($gdimg_error, $this->config_error_fontsize, 2, $lineYoffset, $line, $text_color);
  3350. $lineYoffset += $FontHeight;
  3351. }
  3352. if (function_exists('ImageTypes')) {
  3353. $imagetypes = ImageTypes();
  3354. if ($imagetypes & IMG_PNG) {
  3355. header('Content-Type: image/png');
  3356. ImagePNG($gdimg_error);
  3357. } elseif ($imagetypes & IMG_GIF) {
  3358. header('Content-Type: image/gif');
  3359. ImageGIF($gdimg_error);
  3360. } elseif ($imagetypes & IMG_JPG) {
  3361. header('Content-Type: image/jpeg');
  3362. ImageJPEG($gdimg_error);
  3363. } elseif ($imagetypes & IMG_WBMP) {
  3364. header('Content-Type: image/vnd.wap.wbmp');
  3365. ImageWBMP($gdimg_error);
  3366. }
  3367. }
  3368. ImageDestroy($gdimg_error);
  3369. }
  3370. if (!headers_sent()) {
  3371. echo "\n".'**Failed to send graphical error image, dumping error message as text:**<br>'."\n\n".$text;
  3372. }
  3373. exit;
  3374. return true;
  3375. }
  3376. function ImageCreateFromStringReplacement(&$RawImageData, $DieOnErrors=false) {
  3377. // there are serious bugs in the non-bundled versions of GD which may cause
  3378. // PHP to segfault when calling ImageCreateFromString() - avoid if at all possible
  3379. // when not using a bundled version of GD2
  3380. if (!phpthumb_functions::gd_version()) {
  3381. if ($DieOnErrors) {
  3382. if (!headers_sent()) {
  3383. // base64-encoded error image in GIF format
  3384. $ERROR_NOGD = 'R0lGODlhIAAgALMAAAAAABQUFCQkJDY2NkZGRldXV2ZmZnJycoaGhpSUlKWlpbe3t8XFxdXV1eTk5P7+/iwAAAAAIAAgAAAE/vDJSau9WILtTAACUinDNijZtAHfCojS4W5H+qxD8xibIDE9h0OwWaRWDIljJSkUJYsN4bihMB8th3IToAKs1VtYM75cyV8sZ8vygtOE5yMKmGbO4jRdICQCjHdlZzwzNW4qZSQmKDaNjhUMBX4BBAlmMywFSRWEmAI6b5gAlhNxokGhooAIK5o/pi9vEw4Lfj4OLTAUpj6IabMtCwlSFw0DCKBoFqwAB04AjI54PyZ+yY3TD0ss2YcVmN/gvpcu4TOyFivWqYJlbAHPpOntvxNAACcmGHjZzAZqzSzcq5fNjxFmAFw9iFRunD1epU6tsIPmFCAJnWYE0FURk7wJDA0MTKpEzoWAAskiAAA7';
  3385. header('Content-Type: image/gif');
  3386. echo base64_decode($ERROR_NOGD);
  3387. } else {
  3388. echo '*** ERROR: No PHP-GD support available ***';
  3389. }
  3390. exit;
  3391. } else {
  3392. $this->DebugMessage('ImageCreateFromStringReplacement() failed: gd_version says "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  3393. return false;
  3394. }
  3395. }
  3396. if (phpthumb_functions::gd_is_bundled()) {
  3397. $this->DebugMessage('ImageCreateFromStringReplacement() calling built-in ImageCreateFromString()', __FILE__, __LINE__);
  3398. return @ImageCreateFromString($RawImageData);
  3399. }
  3400. if ($this->issafemode) {
  3401. $this->DebugMessage('ImageCreateFromStringReplacement() failed: cannot create temp file in SAFE_MODE', __FILE__, __LINE__);
  3402. return false;
  3403. }
  3404. switch (substr($RawImageData, 0, 3)) {
  3405. case 'GIF':
  3406. $ICFSreplacementFunctionName = 'ImageCreateFromGIF';
  3407. break;
  3408. case "\xFF\xD8\xFF":
  3409. $ICFSreplacementFunctionName = 'ImageCreateFromJPEG';
  3410. break;
  3411. case "\x89".'PN':
  3412. $ICFSreplacementFunctionName = 'ImageCreateFromPNG';
  3413. break;
  3414. default:
  3415. $this->DebugMessage('ImageCreateFromStringReplacement() failed: unknown fileformat signature "'.phpthumb_functions::HexCharDisplay(substr($RawImageData, 0, 3)).'"', __FILE__, __LINE__);
  3416. return false;
  3417. break;
  3418. }
  3419. if ($tempnam = $this->phpThumb_tempnam()) {
  3420. if ($fp_tempnam = @fopen($tempnam, 'wb')) {
  3421. fwrite($fp_tempnam, $RawImageData);
  3422. fclose($fp_tempnam);
  3423. if (($ICFSreplacementFunctionName == 'ImageCreateFromGIF') && !function_exists($ICFSreplacementFunctionName)) {
  3424. // Need to create from GIF file, but ImageCreateFromGIF does not exist
  3425. ob_start();
  3426. if (!@include_once(dirname(__FILE__).'/phpthumb.gif.php')) {
  3427. $ErrorMessage = 'Failed to include required file "'.dirname(__FILE__).'/phpthumb.gif.php" in '.__FILE__.' on line '.__LINE__;
  3428. $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
  3429. }
  3430. ob_end_clean();
  3431. // gif_loadFileToGDimageResource() cannot read from raw data, write to file first
  3432. if ($tempfilename = $this->phpThumb_tempnam()) {
  3433. if ($fp_tempfile = @fopen($tempfilename, 'wb')) {
  3434. fwrite($fp_tempfile, $RawImageData);
  3435. fclose($fp_tempfile);
  3436. $gdimg_source = gif_loadFileToGDimageResource($tempfilename);
  3437. $this->DebugMessage('gif_loadFileToGDimageResource('.$tempfilename.') completed', __FILE__, __LINE__);
  3438. $this->DebugMessage('deleting "'.$tempfilename.'"', __FILE__, __LINE__);
  3439. unlink($tempfilename);
  3440. return $gdimg_source;
  3441. break;
  3442. } else {
  3443. $ErrorMessage = 'Failed to open tempfile in '.__FILE__.' on line '.__LINE__;
  3444. $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
  3445. }
  3446. } else {
  3447. $ErrorMessage = 'Failed to open generate tempfile name in '.__FILE__.' on line '.__LINE__;
  3448. $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
  3449. }
  3450. } elseif (function_exists($ICFSreplacementFunctionName) && ($gdimg_source = @$ICFSreplacementFunctionName($tempnam))) {
  3451. // great
  3452. $this->DebugMessage($ICFSreplacementFunctionName.'('.$tempnam.') succeeded', __FILE__, __LINE__);
  3453. $this->DebugMessage('deleting "'.$tempnam.'"', __FILE__, __LINE__);
  3454. unlink($tempnam);
  3455. return $gdimg_source;
  3456. } else {
  3457. // GD functions not available, or failed to create image
  3458. $this->DebugMessage($ICFSreplacementFunctionName.'('.$tempnam.') '.(function_exists($ICFSreplacementFunctionName) ? 'failed' : 'does not exist'), __FILE__, __LINE__);
  3459. if (isset($_GET['phpThumbDebug'])) {
  3460. $this->phpThumbDebug();
  3461. }
  3462. }
  3463. } else {
  3464. $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';
  3465. if ($this->issafemode) {
  3466. $ErrorMessage = 'ImageCreateFromStringReplacement() failed in '.__FILE__.' on line '.__LINE__.': cannot create temp file in SAFE_MODE';
  3467. }
  3468. $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
  3469. }
  3470. $this->DebugMessage('deleting "'.$tempnam.'"', __FILE__, __LINE__);
  3471. @unlink($tempnam);
  3472. } else {
  3473. $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';
  3474. if ($this->issafemode) {
  3475. $ErrorMessage = 'ImageCreateFromStringReplacement() failed in '.__FILE__.' on line '.__LINE__.': cannot create temp file in SAFE_MODE';
  3476. }
  3477. }
  3478. if ($DieOnErrors && $ErrorMessage) {
  3479. return $this->ErrorImage($ErrorMessage);
  3480. }
  3481. return false;
  3482. }
  3483. function ImageResizeFunction(&$dst_im, &$src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH) {
  3484. $this->DebugMessage('ImageResizeFunction($o, $s, '.$dstX.', '.$dstY.', '.$srcX.', '.$srcY.', '.$dstW.', '.$dstH.', '.$srcW.', '.$srcH.')', __FILE__, __LINE__);
  3485. if (($dstW == $srcW) && ($dstH == $srcH)) {
  3486. return ImageCopy($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH);
  3487. }
  3488. if (phpthumb_functions::gd_version() >= 2.0) {
  3489. if ($this->config_disable_imagecopyresampled) {
  3490. return phpthumb_functions::ImageCopyResampleBicubic($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
  3491. }
  3492. return ImageCopyResampled($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
  3493. }
  3494. return ImageCopyResized($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
  3495. }
  3496. function InitializeTempDirSetting() {
  3497. $this->config_temp_directory = realpath($this->config_temp_directory ? $this->config_temp_directory : (getenv('TMPDIR') ? getenv('TMPDIR') : getenv('TMP')));
  3498. return true;
  3499. }
  3500. function phpThumb_tempnam() {
  3501. $this->InitializeTempDirSetting();
  3502. $tempnam = realpath(tempnam($this->config_temp_directory, 'pThumb'));
  3503. $this->DebugMessage('phpThumb_tempnam() returning "'.$tempnam.'"', __FILE__, __LINE__);
  3504. return $tempnam;
  3505. }
  3506. function DebugMessage($message, $file='', $line='') {
  3507. $this->debugmessages[] = $message.($file ? ' in file "'.(basename($file) ? basename($file) : $file).'"' : '').($line ? ' on line '.$line : '');
  3508. return true;
  3509. }
  3510. function DebugTimingMessage($message, $file='', $line='', $timestamp=0) {
  3511. if (!$timestamp) {
  3512. $timestamp = array_sum(explode(' ', microtime()));
  3513. }
  3514. $this->debugtiming[number_format($timestamp, 6, '.', '')] = ': '.$message.($file ? ' in file "'.(basename($file) ? basename($file) : $file).'"' : '').($line ? ' on line '.$line : '');
  3515. return true;
  3516. }
  3517. }
  3518. ?>