PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/thumb/phpthumb.class.php

https://bitbucket.org/webnroll/cosyx
PHP | 3854 lines | 3239 code | 408 blank | 207 comment | 645 complexity | 13fb5b6b4dfd06ba79b9bf1f9bc882d6 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  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 = 100; // 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_disable_imagemagick = true;
  105. var $config_prefer_imagemagick = true;
  106. var $config_imagemagick_use_thumbnail = true;
  107. var $config_cache_maxage = null;
  108. var $config_cache_maxsize = null;
  109. var $config_cache_maxfiles = null;
  110. var $config_cache_source_filemtime_ignore_local = false;
  111. var $config_cache_source_filemtime_ignore_remote = true;
  112. var $config_cache_default_only_suffix = false;
  113. var $config_cache_force_passthru = true;
  114. var $config_cache_prefix = ''; // default value set in the constructor below
  115. // * MySQL
  116. var $config_mysql_query = null;
  117. var $config_mysql_hostname = null;
  118. var $config_mysql_username = null;
  119. var $config_mysql_password = null;
  120. var $config_mysql_database = null;
  121. // * Security
  122. var $config_high_security_enabled = false;
  123. var $config_high_security_password = null;
  124. var $config_disable_debug = false;
  125. var $config_allow_src_above_docroot = false;
  126. var $config_allow_src_above_phpthumb = true;
  127. var $config_allow_parameter_file = false;
  128. var $config_allow_parameter_goto = false;
  129. // * HTTP fopen
  130. var $config_http_fopen_timeout = 10;
  131. var $config_http_follow_redirect = true;
  132. // * Compatability
  133. var $config_disable_pathinfo_parsing = false;
  134. var $config_disable_imagecopyresampled = false;
  135. var $config_disable_onlycreateable_passthru = false;
  136. 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';
  137. // END CONFIGURATION OPTIONS
  138. // public: error messages (read-only; persistant)
  139. var $debugmessages = array();
  140. var $debugtiming = array();
  141. var $fatalerror = null;
  142. // private: (should not be modified directly)
  143. var $thumbnailQuality = 75;
  144. var $thumbnailFormat = null;
  145. var $sourceFilename = null;
  146. var $rawImageData = null;
  147. var $IMresizedData = null;
  148. var $outputImageData = null;
  149. var $useRawIMoutput = false;
  150. var $gdimg_output = null;
  151. var $gdimg_source = null;
  152. var $getimagesizeinfo = null;
  153. var $source_width = null;
  154. var $source_height = null;
  155. var $thumbnailCropX = null;
  156. var $thumbnailCropY = null;
  157. var $thumbnailCropW = null;
  158. var $thumbnailCropH = null;
  159. var $exif_thumbnail_width = null;
  160. var $exif_thumbnail_height = null;
  161. var $exif_thumbnail_type = null;
  162. var $exif_thumbnail_data = null;
  163. var $exif_raw_data = null;
  164. var $thumbnail_width = null;
  165. var $thumbnail_height = null;
  166. var $thumbnail_image_width = null;
  167. var $thumbnail_image_height = null;
  168. var $cache_filename = null;
  169. var $AlphaCapableFormats = array('png', 'ico', 'gif');
  170. var $is_alpha = false;
  171. var $iswindows = null;
  172. var $phpthumb_version = '1.7.8-200709161750';
  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->config_document_root = (@$_SERVER['DOCUMENT_ROOT'] ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root);
  181. $this->config_cache_prefix = 'phpThumb_cache_'.@$_SERVER['SERVER_NAME'];
  182. $php_sapi_name = strtolower(function_exists('php_sapi_name') ? php_sapi_name() : '');
  183. if ($php_sapi_name == 'cli') {
  184. $this->config_allow_src_above_docroot = true;
  185. }
  186. }
  187. // public:
  188. function setSourceFilename($sourceFilename) {
  189. //$this->resetObject();
  190. //$this->rawImageData = null;
  191. $this->sourceFilename = $sourceFilename;
  192. $this->src = $sourceFilename;
  193. if (is_null($this->config_output_format)) {
  194. $sourceFileExtension = strtolower(substr(strrchr($sourceFilename, '.'), 1));
  195. if (ereg('^[a-z]{3,4}$', $sourceFileExtension)) {
  196. $this->config_output_format = $sourceFileExtension;
  197. $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->config_output_format to "'.$sourceFileExtension.'"', __FILE__, __LINE__);
  198. } else {
  199. $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__);
  200. }
  201. }
  202. $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->sourceFilename to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
  203. return true;
  204. }
  205. // public:
  206. function setSourceData($rawImageData, $sourceFilename='') {
  207. //$this->resetObject();
  208. //$this->sourceFilename = null;
  209. $this->rawImageData = $rawImageData;
  210. $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__);
  211. if ($this->config_cache_source_enabled) {
  212. $sourceFilename = ($sourceFilename ? $sourceFilename : md5($rawImageData));
  213. if (!is_dir($this->config_cache_source_directory)) {
  214. $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not a directory');
  215. } elseif (!@is_writable($this->config_cache_source_directory)) {
  216. $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not writable');
  217. }
  218. $this->DebugMessage('setSourceData() attempting to save source image to "'.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).'"', __FILE__, __LINE__);
  219. if ($fp = @fopen($this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename), 'wb')) {
  220. fwrite($fp, $rawImageData);
  221. fclose($fp);
  222. } elseif (!$this->phpThumbDebug) {
  223. $this->ErrorImage('setSourceData() failed to write to source cache ('.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).')');
  224. }
  225. }
  226. return true;
  227. }
  228. // public:
  229. function setSourceImageResource($gdimg) {
  230. //$this->resetObject();
  231. $this->gdimg_source = $gdimg;
  232. return true;
  233. }
  234. // public:
  235. function setParameter($param, $value) {
  236. if ($param == 'src') {
  237. $this->setSourceFilename($this->ResolveFilenameToAbsolute($value));
  238. } elseif (@is_array($this->$param)) {
  239. if (is_array($value)) {
  240. foreach ($value as $arraykey => $arrayvalue) {
  241. array_push($this->$param, $arrayvalue);
  242. }
  243. } else {
  244. array_push($this->$param, $value);
  245. }
  246. } else {
  247. $this->$param = $value;
  248. }
  249. return true;
  250. }
  251. // public:
  252. function getParameter($param) {
  253. //if (property_exists('phpThumb', $param)) {
  254. return $this->$param;
  255. //}
  256. //$this->DebugMessage('setParameter() attempting to get non-existant parameter "'.$param.'"', __FILE__, __LINE__);
  257. //return false;
  258. }
  259. // public:
  260. function GenerateThumbnail() {
  261. $this->setOutputFormat();
  262. $this->phpThumbDebug('8a');
  263. $this->ResolveSource();
  264. $this->phpThumbDebug('8b');
  265. $this->SetCacheFilename();
  266. $this->phpThumbDebug('8c');
  267. $this->ExtractEXIFgetImageSize();
  268. $this->phpThumbDebug('8d');
  269. if ($this->useRawIMoutput) {
  270. $this->DebugMessage('Skipping rest of GenerateThumbnail() because $this->useRawIMoutput', __FILE__, __LINE__);
  271. return true;
  272. }
  273. $this->phpThumbDebug('8e');
  274. if (!$this->SourceImageToGD()) {
  275. $this->DebugMessage('SourceImageToGD() failed', __FILE__, __LINE__);
  276. return false;
  277. }
  278. $this->phpThumbDebug('8f');
  279. $this->Rotate();
  280. $this->phpThumbDebug('8g');
  281. $this->CreateGDoutput();
  282. $this->phpThumbDebug('8h');
  283. switch ($this->far) {
  284. case 'L':
  285. case 'TL':
  286. case 'BL':
  287. $destination_offset_x = 0;
  288. $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
  289. break;
  290. case 'R':
  291. case 'TR':
  292. case 'BR':
  293. $destination_offset_x = round($this->thumbnail_width - $this->thumbnail_image_width);
  294. $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
  295. break;
  296. case 'T':
  297. case 'TL':
  298. case 'TR':
  299. $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2);
  300. $destination_offset_y = 0;
  301. break;
  302. case 'B':
  303. case 'BL':
  304. case 'BR':
  305. $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2);
  306. $destination_offset_y = round($this->thumbnail_height - $this->thumbnail_image_height);
  307. break;
  308. case 'C':
  309. default:
  310. $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2);
  311. $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
  312. }
  313. // // copy/resize image to appropriate dimensions
  314. // $borderThickness = 0;
  315. // if (!empty($this->fltr)) {
  316. // foreach ($this->fltr as $key => $value) {
  317. // if (ereg('^bord\|([0-9]+)', $value, $matches)) {
  318. // $borderThickness = $matches[1];
  319. // break;
  320. // }
  321. // }
  322. // }
  323. // if ($borderThickness > 0) {
  324. // //$this->DebugMessage('Skipping ImageResizeFunction() because BorderThickness="'.$borderThickness.'"', __FILE__, __LINE__);
  325. // $this->thumbnail_image_height /= 2;
  326. // }
  327. $this->ImageResizeFunction(
  328. $this->gdimg_output,
  329. $this->gdimg_source,
  330. $destination_offset_x,
  331. $destination_offset_y,
  332. $this->thumbnailCropX,
  333. $this->thumbnailCropY,
  334. $this->thumbnail_image_width,
  335. $this->thumbnail_image_height,
  336. $this->thumbnailCropW,
  337. $this->thumbnailCropH
  338. );
  339. $this->DebugMessage('memory_get_usage() after copy-resize = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
  340. ImageDestroy($this->gdimg_source);
  341. $this->DebugMessage('memory_get_usage() after ImageDestroy = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
  342. $this->phpThumbDebug('8i');
  343. $this->AntiOffsiteLinking();
  344. $this->phpThumbDebug('8j');
  345. $this->ApplyFilters();
  346. $this->phpThumbDebug('8k');
  347. $this->AlphaChannelFlatten();
  348. $this->phpThumbDebug('8l');
  349. $this->MaxFileSize();
  350. $this->phpThumbDebug('8m');
  351. $this->DebugMessage('GenerateThumbnail() completed successfully', __FILE__, __LINE__);
  352. return true;
  353. }
  354. // public:
  355. function RenderOutput() {
  356. if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) {
  357. $this->DebugMessage('RenderOutput() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
  358. return false;
  359. }
  360. if (!$this->thumbnailFormat) {
  361. $this->DebugMessage('RenderOutput() failed because $this->thumbnailFormat is empty', __FILE__, __LINE__);
  362. return false;
  363. }
  364. if ($this->useRawIMoutput) {
  365. $this->DebugMessage('RenderOutput copying $this->IMresizedData ('.strlen($this->IMresizedData).' bytes) to $this->outputImage', __FILE__, __LINE__);
  366. $this->outputImageData = $this->IMresizedData;
  367. return true;
  368. }
  369. $builtin_formats = array();
  370. if (function_exists('ImageTypes')) {
  371. $imagetypes = ImageTypes();
  372. $builtin_formats['wbmp'] = (bool) ($imagetypes & IMG_WBMP);
  373. $builtin_formats['jpg'] = (bool) ($imagetypes & IMG_JPG);
  374. $builtin_formats['gif'] = (bool) ($imagetypes & IMG_GIF);
  375. $builtin_formats['png'] = (bool) ($imagetypes & IMG_PNG);
  376. }
  377. $this->DebugMessage('RenderOutput() attempting Image'.strtoupper(@$this->thumbnailFormat).'($this->gdimg_output)', __FILE__, __LINE__);
  378. ob_start();
  379. switch ($this->thumbnailFormat) {
  380. case 'wbmp':
  381. if (!@$builtin_formats['wbmp']) {
  382. $this->DebugMessage('GD does not have required built-in support for WBMP output', __FILE__, __LINE__);
  383. ob_end_clean();
  384. return false;
  385. }
  386. ImageJPEG($this->gdimg_output, null, $this->thumbnailQuality);
  387. $this->outputImageData = ob_get_contents();
  388. break;
  389. case 'jpeg':
  390. case 'jpg': // should be "jpeg" not "jpg" but just in case...
  391. if (!@$builtin_formats['jpg']) {
  392. $this->DebugMessage('GD does not have required built-in support for JPEG output', __FILE__, __LINE__);
  393. ob_end_clean();
  394. return false;
  395. }
  396. ImageJPEG($this->gdimg_output, null, $this->thumbnailQuality);
  397. $this->outputImageData = ob_get_contents();
  398. break;
  399. case 'png':
  400. if (!@$builtin_formats['png']) {
  401. $this->DebugMessage('GD does not have required built-in support for PNG output', __FILE__, __LINE__);
  402. ob_end_clean();
  403. return false;
  404. }
  405. ImagePNG($this->gdimg_output);
  406. $this->outputImageData = ob_get_contents();
  407. break;
  408. case 'gif':
  409. if (!@$builtin_formats['gif']) {
  410. $this->DebugMessage('GD does not have required built-in support for GIF output', __FILE__, __LINE__);
  411. ob_end_clean();
  412. return false;
  413. }
  414. ImageGIF($this->gdimg_output);
  415. $this->outputImageData = ob_get_contents();
  416. break;
  417. case 'bmp':
  418. $ImageOutFunction = '"builtin BMP output"';
  419. if (!@include_once(dirname(__FILE__).'/phpthumb.bmp.php')) {
  420. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
  421. ob_end_clean();
  422. return false;
  423. }
  424. $phpthumb_bmp = new phpthumb_bmp();
  425. $this->outputImageData = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
  426. unset($phpthumb_bmp);
  427. break;
  428. case 'ico':
  429. $ImageOutFunction = '"builtin ICO output"';
  430. if (!@include_once(dirname(__FILE__).'/phpthumb.ico.php')) {
  431. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
  432. ob_end_clean();
  433. return false;
  434. }
  435. $phpthumb_ico = new phpthumb_ico();
  436. $arrayOfOutputImages = array($this->gdimg_output);
  437. $this->outputImageData = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
  438. unset($phpthumb_ico);
  439. break;
  440. default:
  441. $this->DebugMessage('RenderOutput failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
  442. ob_end_clean();
  443. return false;
  444. }
  445. ob_end_clean();
  446. if (!$this->outputImageData) {
  447. $this->DebugMessage('RenderOutput() for "'.$this->thumbnailFormat.'" failed', __FILE__, __LINE__);
  448. ob_end_clean();
  449. return false;
  450. }
  451. $this->DebugMessage('RenderOutput() completing with $this->outputImageData = '.strlen($this->outputImageData).' bytes', __FILE__, __LINE__);
  452. return true;
  453. }
  454. // public:
  455. function RenderToFile($filename) {
  456. if (eregi('^(f|ht)tps?\://', $filename)) {
  457. $this->DebugMessage('RenderToFile() failed because $filename ('.$filename.') is a URL', __FILE__, __LINE__);
  458. return false;
  459. }
  460. // render thumbnail to this file only, do not cache, do not output to browser
  461. //$renderfilename = $this->ResolveFilenameToAbsolute(dirname($filename)).DIRECTORY_SEPARATOR.basename($filename);
  462. $renderfilename = $filename;
  463. if (($filename{0} != '/') && ($filename{0} != '\\') && ($filename{1} != ':')) {
  464. $renderfilename = $this->ResolveFilenameToAbsolute($renderfilename);
  465. }
  466. if (!@is_writable(dirname($renderfilename))) {
  467. $this->DebugMessage('RenderToFile() failed because "'.dirname($renderfilename).'/" is not writable', __FILE__, __LINE__);
  468. return false;
  469. }
  470. if (@is_file($renderfilename) && !@is_writable($renderfilename)) {
  471. $this->DebugMessage('RenderToFile() failed because "'.$renderfilename.'" is not writable', __FILE__, __LINE__);
  472. return false;
  473. }
  474. if ($this->RenderOutput()) {
  475. if (file_put_contents($renderfilename, $this->outputImageData)) {
  476. $this->DebugMessage('RenderToFile('.$renderfilename.') succeeded', __FILE__, __LINE__);
  477. return true;
  478. }
  479. if (!@file_exists($renderfilename)) {
  480. $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] did not appear to fail, but the output image does not exist either...', __FILE__, __LINE__);
  481. }
  482. } else {
  483. $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] failed', __FILE__, __LINE__);
  484. }
  485. return false;
  486. }
  487. // public:
  488. function OutputThumbnail() {
  489. if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) {
  490. $this->DebugMessage('OutputThumbnail() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
  491. return false;
  492. }
  493. if (headers_sent()) {
  494. return $this->ErrorImage('OutputThumbnail() failed - headers already sent');
  495. exit;
  496. }
  497. $downloadfilename = phpthumb_functions::SanitizeFilename(is_string($this->sia) ? $this->sia : ($this->down ? $this->down : 'phpThumb_generated_thumbnail'.'.'.$this->thumbnailFormat));
  498. $this->DebugMessage('Content-Disposition header filename set to "'.$downloadfilename.'"', __FILE__, __LINE__);
  499. if ($downloadfilename) {
  500. header('Content-Disposition: '.($this->down ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"');
  501. } else {
  502. $this->DebugMessage('failed to send Content-Disposition header because $downloadfilename is empty', __FILE__, __LINE__);
  503. }
  504. if ($this->useRawIMoutput) {
  505. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  506. echo $this->IMresizedData;
  507. } else {
  508. $this->DebugMessage('ImageInterlace($this->gdimg_output, '.intval($this->config_output_interlace).')', __FILE__, __LINE__);
  509. ImageInterlace($this->gdimg_output, intval($this->config_output_interlace));
  510. switch ($this->thumbnailFormat) {
  511. case 'jpeg':
  512. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  513. $ImageOutFunction = 'image'.$this->thumbnailFormat;
  514. @$ImageOutFunction($this->gdimg_output, '', $this->thumbnailQuality);
  515. break;
  516. case 'png':
  517. case 'gif':
  518. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  519. $ImageOutFunction = 'image'.$this->thumbnailFormat;
  520. @$ImageOutFunction($this->gdimg_output);
  521. break;
  522. case 'bmp':
  523. if (!@include_once(dirname(__FILE__).'/phpthumb.bmp.php')) {
  524. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
  525. return false;
  526. }
  527. $phpthumb_bmp = new phpthumb_bmp();
  528. if (is_object($phpthumb_bmp)) {
  529. $bmp_data = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
  530. unset($phpthumb_bmp);
  531. if (!$bmp_data) {
  532. $this->DebugMessage('$phpthumb_bmp->GD2BMPstring() failed', __FILE__, __LINE__);
  533. return false;
  534. }
  535. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  536. echo $bmp_data;
  537. } else {
  538. $this->DebugMessage('new phpthumb_bmp() failed', __FILE__, __LINE__);
  539. return false;
  540. }
  541. break;
  542. case 'ico':
  543. if (!@include_once(dirname(__FILE__).'/phpthumb.ico.php')) {
  544. $this->DebugMessage('Error including "'.dirname(__FILE__).'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
  545. return false;
  546. }
  547. $phpthumb_ico = new phpthumb_ico();
  548. if (is_object($phpthumb_ico)) {
  549. $arrayOfOutputImages = array($this->gdimg_output);
  550. $ico_data = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
  551. unset($phpthumb_ico);
  552. if (!$ico_data) {
  553. $this->DebugMessage('$phpthumb_ico->GD2ICOstring() failed', __FILE__, __LINE__);
  554. return false;
  555. }
  556. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  557. echo $ico_data;
  558. } else {
  559. $this->DebugMessage('new phpthumb_ico() failed', __FILE__, __LINE__);
  560. return false;
  561. }
  562. break;
  563. default:
  564. $this->DebugMessage('OutputThumbnail failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
  565. return false;
  566. break;
  567. }
  568. }
  569. return true;
  570. }
  571. // public:
  572. function CleanUpCacheDirectory() {
  573. $DeletedKeys = array();
  574. $AllFilesInCacheDirectory = array();
  575. $this->DebugMessage('skipping 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__);
  576. if (($this->config_cache_maxage > 0) || ($this->config_cache_maxsize > 0) || ($this->config_cache_maxfiles > 0)) {
  577. $CacheDirOldFilesAge = array();
  578. $CacheDirOldFilesSize = array();
  579. $AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory);
  580. foreach ($AllFilesInCacheDirectory as $fullfilename) {
  581. if (eregi('^phpThumb_cache_', basename($fullfilename)) && file_exists($fullfilename)) {
  582. $CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename);
  583. if ($CacheDirOldFilesAge[$fullfilename] == 0) {
  584. $CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename);
  585. }
  586. $CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename);
  587. }
  588. }
  589. if (empty($CacheDirOldFilesSize)) {
  590. return true;
  591. }
  592. $DeletedKeys['zerobyte'] = array();
  593. foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) {
  594. // purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files)
  595. $cutofftime = time() - 3600;
  596. if (($filesize == 0) && ($CacheDirOldFilesAge[$fullfilename] < $cutofftime)) {
  597. if (@unlink($fullfilename)) {
  598. $DeletedKeys['zerobyte'][] = $fullfilename;
  599. unset($CacheDirOldFilesSize[$fullfilename]);
  600. unset($CacheDirOldFilesAge[$fullfilename]);
  601. }
  602. }
  603. }
  604. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['zerobyte']).' zero-byte files', __FILE__, __LINE__);
  605. asort($CacheDirOldFilesAge);
  606. if ($this->config_cache_maxfiles > 0) {
  607. $TotalCachedFiles = count($CacheDirOldFilesAge);
  608. $DeletedKeys['maxfiles'] = array();
  609. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  610. if ($TotalCachedFiles > $this->config_cache_maxfiles) {
  611. if (@unlink($fullfilename)) {
  612. $TotalCachedFiles--;
  613. $DeletedKeys['maxfiles'][] = $fullfilename;
  614. }
  615. } else {
  616. // there are few enough files to keep the rest
  617. break;
  618. }
  619. }
  620. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxfiles']).' files based on (config_cache_maxfiles='.$this->config_cache_maxfiles.')', __FILE__, __LINE__);
  621. foreach ($DeletedKeys['maxfiles'] as $fullfilename) {
  622. unset($CacheDirOldFilesAge[$fullfilename]);
  623. unset($CacheDirOldFilesSize[$fullfilename]);
  624. }
  625. }
  626. if ($this->config_cache_maxage > 0) {
  627. $mindate = time() - $this->config_cache_maxage;
  628. $DeletedKeys['maxage'] = array();
  629. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  630. if ($filedate > 0) {
  631. if ($filedate < $mindate) {
  632. if (@unlink($fullfilename)) {
  633. $DeletedKeys['maxage'][] = $fullfilename;
  634. }
  635. } else {
  636. // the rest of the files are new enough to keep
  637. break;
  638. }
  639. }
  640. }
  641. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxage']).' files based on (config_cache_maxage='.$this->config_cache_maxage.')', __FILE__, __LINE__);
  642. foreach ($DeletedKeys['maxage'] as $fullfilename) {
  643. unset($CacheDirOldFilesAge[$fullfilename]);
  644. unset($CacheDirOldFilesSize[$fullfilename]);
  645. }
  646. }
  647. if ($this->config_cache_maxsize > 0) {
  648. $TotalCachedFileSize = array_sum($CacheDirOldFilesSize);
  649. $DeletedKeys['maxsize'] = array();
  650. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  651. if ($TotalCachedFileSize > $this->config_cache_maxsize) {
  652. if (@unlink($fullfilename)) {
  653. $TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename];
  654. $DeletedKeys['maxsize'][] = $fullfilename;
  655. }
  656. } else {
  657. // the total filesizes are small enough to keep the rest of the files
  658. break;
  659. }
  660. }
  661. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxsize']).' files based on (config_cache_maxsize='.$this->config_cache_maxsize.')', __FILE__, __LINE__);
  662. foreach ($DeletedKeys['maxsize'] as $fullfilename) {
  663. unset($CacheDirOldFilesAge[$fullfilename]);
  664. unset($CacheDirOldFilesSize[$fullfilename]);
  665. }
  666. }
  667. } else {
  668. $this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__);
  669. }
  670. $totalpurged = 0;
  671. foreach ($DeletedKeys as $key => $value) {
  672. $totalpurged += count($value);
  673. }
  674. $this->DebugMessage('CleanUpCacheDirectory() purged '.$totalpurged.' files (from '.count($AllFilesInCacheDirectory).') based on config settings', __FILE__, __LINE__);
  675. if ($totalpurged > 0) {
  676. $empty_dirs = array();
  677. foreach ($AllFilesInCacheDirectory as $fullfilename) {
  678. if (is_dir($fullfilename)) {
  679. $empty_dirs[realpath($fullfilename)] = 1;
  680. } else {
  681. unset($empty_dirs[realpath(dirname($fullfilename))]);
  682. }
  683. }
  684. krsort($empty_dirs);
  685. $totalpurgeddirs = 0;
  686. foreach ($empty_dirs as $empty_dir => $dummy) {
  687. if ($empty_dir == $this->config_cache_directory) {
  688. // shouldn't happen, but just in case, don't let it delete actual cache directory
  689. continue;
  690. } elseif (@rmdir($empty_dir)) {
  691. $totalpurgeddirs++;
  692. } else {
  693. $this->DebugMessage('failed to rmdir('.$empty_dir.')', __FILE__, __LINE__);
  694. }
  695. }
  696. $this->DebugMessage('purged '.$totalpurgeddirs.' empty directories', __FILE__, __LINE__);
  697. }
  698. return true;
  699. }
  700. //////////////////////////////////////////////////////////////////////
  701. // private: re-initializator (call between rendering multiple images with one object)
  702. function resetObject() {
  703. $class_vars = get_class_vars(get_class($this));
  704. foreach ($class_vars as $key => $value) {
  705. // do not clobber debug or config info
  706. if (!eregi('^(config_|debug|fatalerror)', $key)) {
  707. $this->$key = $value;
  708. }
  709. }
  710. $this->phpThumb(); // re-initialize some class variables
  711. return true;
  712. }
  713. //////////////////////////////////////////////////////////////////////
  714. function ResolveSource() {
  715. if (is_resource($this->gdimg_source)) {
  716. $this->DebugMessage('ResolveSource() exiting because is_resource($this->gdimg_source)', __FILE__, __LINE__);
  717. return true;
  718. }
  719. if ($this->rawImageData) {
  720. $this->sourceFilename = null;
  721. $this->DebugMessage('ResolveSource() exiting because $this->rawImageData is set ('.number_format(strlen($this->rawImageData)).' bytes)', __FILE__, __LINE__);
  722. return true;
  723. }
  724. if ($this->sourceFilename) {
  725. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->sourceFilename);
  726. $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
  727. } elseif ($this->src) {
  728. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
  729. $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'" from $this->src ('.$this->src.')', __FILE__, __LINE__);
  730. } else {
  731. return $this->ErrorImage('$this->sourceFilename and $this->src are both empty');
  732. }
  733. if ($this->iswindows && ((substr($this->sourceFilename, 0, 2) == '//') || (substr($this->sourceFilename, 0, 2) == '\\\\'))) {
  734. // Windows \\share\filename.ext
  735. } elseif (eregi('^(f|ht)tps?\://', $this->sourceFilename)) {
  736. // URL
  737. if ($this->config_http_user_agent) {
  738. ini_set('user_agent', $this->config_http_user_agent);
  739. }
  740. } elseif (!@file_exists($this->sourceFilename)) {
  741. return $this->ErrorImage('"'.$this->sourceFilename.'" does not exist');
  742. } elseif (!@is_file($this->sourceFilename)) {
  743. return $this->ErrorImage('"'.$this->sourceFilename.'" is not a file');
  744. }
  745. return true;
  746. }
  747. function setOutputFormat() {
  748. static $alreadyCalled = false;
  749. if ($this->thumbnailFormat && $alreadyCalled) {
  750. return true;
  751. }
  752. $alreadyCalled = true;
  753. $AvailableImageOutputFormats = array();
  754. $AvailableImageOutputFormats[] = 'text';
  755. if (@is_readable(dirname(__FILE__).'/phpthumb.ico.php')) {
  756. $AvailableImageOutputFormats[] = 'ico';
  757. }
  758. if (@is_readable(dirname(__FILE__).'/phpthumb.bmp.php')) {
  759. $AvailableImageOutputFormats[] = 'bmp';
  760. }
  761. $this->thumbnailFormat = 'ico';
  762. // Set default output format based on what image types are available
  763. if (function_exists('ImageTypes')) {
  764. $imagetypes = ImageTypes();
  765. if ($imagetypes & IMG_WBMP) {
  766. $this->thumbnailFormat = 'wbmp';
  767. $AvailableImageOutputFormats[] = 'wbmp';
  768. }
  769. if ($imagetypes & IMG_GIF) {
  770. $this->thumbnailFormat = 'gif';
  771. $AvailableImageOutputFormats[] = 'gif';
  772. }
  773. if ($imagetypes & IMG_PNG) {
  774. $this->thumbnailFormat = 'png';
  775. $AvailableImageOutputFormats[] = 'png';
  776. }
  777. if ($imagetypes & IMG_JPG) {
  778. $this->thumbnailFormat = 'jpeg';
  779. $AvailableImageOutputFormats[] = 'jpeg';
  780. }
  781. } else {
  782. //return $this->ErrorImage('ImageTypes() does not exist - GD support might not be enabled?');
  783. $this->DebugMessage('ImageTypes() does not exist - GD support might not be enabled?', __FILE__, __LINE__);
  784. }
  785. if (!$this->config_disable_imagemagick && $this->ImageMagickVersion()) {
  786. $IMformats = array('jpeg', 'png', 'gif', 'bmp', 'ico', 'wbmp');
  787. $this->DebugMessage('Addding ImageMagick formats to $AvailableImageOutputFormats ('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
  788. foreach ($IMformats as $key => $format) {
  789. $AvailableImageOutputFormats[] = $format;
  790. }
  791. }
  792. $AvailableImageOutputFormats = array_unique($AvailableImageOutputFormats);
  793. $this->DebugMessage('$AvailableImageOutputFormats = array('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
  794. $this->f = ereg_replace('[^a-z]', '', strtolower($this->f));
  795. if (strtolower($this->config_output_format) == 'jpg') {
  796. $this->config_output_format = 'jpeg';
  797. }
  798. if (strtolower($this->f) == 'jpg') {
  799. $this->f = 'jpeg';
  800. }
  801. if (phpthumb_functions::CaseInsensitiveInArray($this->config_output_format, $AvailableImageOutputFormats)) {
  802. // set output format to config default if that format is available
  803. $this->DebugMessage('$this->thumbnailFormat set to $this->config_output_format "'.strtolower($this->config_output_format).'"', __FILE__, __LINE__);
  804. $this->thumbnailFormat = strtolower($this->config_output_format);
  805. } elseif ($this->config_output_format) {
  806. $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->config_output_format ('.strtolower($this->config_output_format).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
  807. }
  808. if ($this->f && (phpthumb_functions::CaseInsensitiveInArray($this->f, $AvailableImageOutputFormats))) {
  809. // override output format if $this->f is set and that format is available
  810. $this->DebugMessage('$this->thumbnailFormat set to $this->f "'.strtolower($this->f).'"', __FILE__, __LINE__);
  811. $this->thumbnailFormat = strtolower($this->f);
  812. } elseif ($this->f) {
  813. $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->f ('.strtolower($this->f).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
  814. }
  815. // for JPEG images, quality 1 (worst) to 99 (best)
  816. // quality < 25 is nasty, with not much size savings - not recommended
  817. // problems with 100 - invalid JPEG?
  818. $this->thumbnailQuality = max(1, min(99, ($this->q ? $this->q : 75)));
  819. $this->DebugMessage('$this->thumbnailQuality set to "'.$this->thumbnailQuality.'"', __FILE__, __LINE__);
  820. return true;
  821. }
  822. function setCacheDirectory() {
  823. // resolve cache directory to absolute pathname
  824. $this->DebugMessage('setCacheDirectory() starting with config_cache_directory = "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
  825. if (substr($this->config_cache_directory, 0, 1) == '.') {
  826. if (eregi('^(f|ht)tps?\://', $this->src)) {
  827. if (!$this->config_cache_disable_warning) {
  828. $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');
  829. }
  830. } elseif ($this->src) {
  831. // resolve relative cache directory to source image
  832. $this->config_cache_directory = dirname($this->ResolveFilenameToAbsolute($this->src)).DIRECTORY_SEPARATOR.$this->config_cache_directory;
  833. } else {
  834. // $this->new is probably set
  835. }
  836. }
  837. if (substr($this->config_cache_directory, -1) == '/') {
  838. $this->config_cache_directory = substr($this->config_cache_directory, 0, -1);
  839. }
  840. if ($this->iswindows) {
  841. $this->config_cache_directory = str_replace('/', DIRECTORY_SEPARATOR, $this->config_cache_directory);
  842. }
  843. if ($this->config_cache_directory) {
  844. $real_cache_path = realpath($this->config_cache_directory);
  845. if (!$real_cache_path) {
  846. $this->DebugMessage('realpath($this->config_cache_directory) failed for "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
  847. if (!is_dir($this->config_cache_directory)) {
  848. $this->DebugMessage('!is_dir('.$this->config_cache_directory.')', __FILE__, __LINE__);
  849. }
  850. }
  851. if ($real_cache_path) {
  852. $this->DebugMessage('setting config_cache_directory to realpath('.$this->config_cache_directory.') = "'.$real_cache_path.'"', __FILE__, __LINE__);
  853. $this->config_cache_directory = $real_cache_path;
  854. }
  855. }
  856. if (!is_dir($this->config_cache_directory)) {
  857. if (!$this->config_cache_disable_warning) {
  858. $this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') does not exist. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php');
  859. }
  860. $this->DebugMessage('$this->config_cache_directory ('.$this->config_cache_directory.') is not a directory', __FILE__, __LINE__);
  861. $this->config_cache_directory = null;
  862. } elseif (!@is_writable($this->config_cache_directory)) {
  863. $this->DebugMessage('$this->config_cache_directory is not writable ('.$this->config_cache_directory.')', __FILE__, __LINE__);
  864. }
  865. $this->InitializeTempDirSetting();
  866. 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)) {
  867. $this->DebugMessage('setting $this->config_temp_directory = $this->config_cache_directory ('.$this->config_cache_directory.')', __FILE__, __LINE__);
  868. $this->config_temp_directory = $this->config_cache_directory;
  869. }
  870. return true;
  871. }
  872. function ResolveFilenameToAbsolute($filename) {
  873. if (!$filename) {
  874. return false;
  875. }
  876. //if (eregi('^(f|ht)tps?\://', $filename)) {
  877. if (eregi('^[a-z0-9]+\:/{1,2}', $filename)) {
  878. // eg: http://host/path/file.jpg (HTTP URL)
  879. // eg: ftp://host/path/file.jpg (FTP URL)
  880. // eg: data1:/path/file.jpg (Netware path)
  881. //$AbsoluteFilename = $filename;
  882. return $filename;
  883. } elseif ($this->iswindows && ($filename{1} == ':')) {
  884. // absolute pathname (Windows)
  885. $AbsoluteFilename = $filename;
  886. } elseif ($this->iswindows && ((substr($filename, 0, 2) == '//') || (substr($filename, 0, 2) == '\\\\'))) {
  887. // absolute pathname (Windows)
  888. $AbsoluteFilename = $filename;
  889. } elseif ($filename{0} == '/') {
  890. if (@is_readable($filename) && !@is_readable($this->config_document_root.$filename)) {
  891. // absolute filename (*nix)
  892. $AbsoluteFilename = $filename;
  893. } elseif ($filename{1} == '~') {
  894. // /~user/path
  895. if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray($filename)) {
  896. $AbsoluteFilename = $ApacheLookupURIarray['filename'];
  897. } else {
  898. $AbsoluteFilename = realpath($filename);
  899. if (@is_readable($AbsoluteFilename)) {
  900. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.$filename.'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with realpath($filename)', __FILE__, __LINE__);
  901. } elseif (is_dir(dirname($AbsoluteFilename))) {
  902. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname($filename).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with realpath(.)', __FILE__, __LINE__);
  903. } else {
  904. 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")');
  905. }
  906. }
  907. } else {
  908. // relative filename (any OS)
  909. if (ereg('^'.preg_quote($this->config_document_root), $filename)) {
  910. $AbsoluteFilename = $filename;
  911. $this->DebugMessage('ResolveFilenameToAbsolute() NOT prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__);
  912. } else {
  913. $AbsoluteFilename = $this->config_document_root.$filename;
  914. $this->DebugMessage('ResolveFilenameToAbsolute() prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__);
  915. }
  916. }
  917. } else {
  918. // relative to current directory (any OS)
  919. $AbsoluteFilename = $this->config_document_root.dirname(@$_SERVER['PHP_SELF']).DIRECTORY_SEPARATOR.$filename;
  920. //if (!@file_exists($AbsoluteFilename) && @file_exists(realpath($this->DotPadRelativeDirectoryPath($filename)))) {
  921. // $AbsoluteFilename = realpath($this->DotPadRelativeDirectoryPath($filename));
  922. //}
  923. if (substr(dirname(@$_SERVER['PHP_SELF']), 0, 2) == '/~') {
  924. if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) {
  925. $AbsoluteFilename = $ApacheLookupURIarray['filename'].DIRECTORY_SEPARATOR.$filename;
  926. } else {
  927. $AbsoluteFilename = realpath('.').DIRECTORY_SEPARATOR.$filename;
  928. if (@is_readable($AbsoluteFilename)) {
  929. $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__);
  930. } elseif (is_dir(dirname($AbsoluteFilename))) {
  931. $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__);
  932. } else {
  933. 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');
  934. }
  935. }
  936. }
  937. }
  938. if (is_link($AbsoluteFilename)) {
  939. $this->DebugMessage('is_link()==true, changing "'.$AbsoluteFilename.'" to "'.readlink($AbsoluteFilename).'"', __FILE__, __LINE__);
  940. $AbsoluteFilename = readlink($AbsoluteFilename);
  941. }
  942. if (realpath($AbsoluteFilename)) {
  943. $AbsoluteFilename = realpath($AbsoluteFilename);
  944. }
  945. if ($this->iswindows) {
  946. $AbsoluteFilename = eregi_replace('^'.preg_quote(realpath($this->config_document_root)), realpath($this->config_document_root), $AbsoluteFilename);
  947. $AbsoluteFilename = str_replace(DIRECTORY_SEPARATOR, '/', $AbsoluteFilename);
  948. }
  949. if (!$this->config_allow_src_above_docroot && !ereg('^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', realpath($this->config_document_root))), $AbsoluteFilename)) {
  950. $this->DebugMessage('!$this->config_allow_src_above_docroot therefore setting "'.$AbsoluteFilename.'" (outside "'.realpath($this->config_document_root).'") to null', __FILE__, __LINE__);
  951. return false;
  952. }
  953. if (!$this->config_allow_src_above_phpthumb && !ereg('^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__))), $AbsoluteFilename)) {
  954. $this->DebugMessage('!$this->config_allow_src_above_phpthumb therefore setting "'.$AbsoluteFilename.'" (outside "'.dirname(__FILE__).'") to null', __FILE__, __LINE__);
  955. return false;
  956. }
  957. return $AbsoluteFilename;
  958. }
  959. function ImageMagickWhichConvert() {
  960. static $WhichConvert = null;
  961. if (is_null($WhichConvert)) {
  962. if ($this->iswindows) {
  963. $WhichConvert = false;
  964. } else {
  965. $WhichConvert = trim(phpthumb_functions::SafeExec('which convert'));
  966. }
  967. }
  968. return $WhichConvert;
  969. }
  970. function ImageMagickCommandlineBase() {
  971. static $commandline = null;
  972. if (is_null($commandline)) {
  973. $commandline = (!is_null($this->config_imagemagick_path) ? $this->config_imagemagick_path : '');
  974. if ($this->config_imagemagick_path && ($this->config_imagemagick_path != realpath($this->config_imagemagick_path))) {
  975. if (@is_executable(realpath($this->config_imagemagick_path))) {
  976. $this->DebugMessage('Changing $this->config_imagemagick_path ('.$this->config_imagemagick_path.') to realpath($this->config_imagemagick_path) ('.realpath($this->config_imagemagick_path).')', __FILE__, __LINE__);
  977. $this->config_imagemagick_path = realpath($this->config_imagemagick_path);
  978. } else {
  979. $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__);
  980. }
  981. }
  982. $this->DebugMessage(' file_exists('.$this->config_imagemagick_path.') = '.intval( @file_exists($this->config_imagemagick_path)), __FILE__, __LINE__);
  983. $this->DebugMessage('is_executable('.$this->config_imagemagick_path.') = '.intval(@is_executable($this->config_imagemagick_path)), __FILE__, __LINE__);
  984. if (@file_exists($this->config_imagemagick_path)) {
  985. $this->DebugMessage('using ImageMagick path from $this->config_imagemagick_path ('.$this->config_imagemagick_path.')', __FILE__, __LINE__);
  986. if ($this->iswindows) {
  987. $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);
  988. } else {
  989. $commandline = '"'.$this->config_imagemagick_path.'"';
  990. }
  991. return $commandline;
  992. }
  993. $which_convert = $this->ImageMagickWhichConvert();
  994. $IMversion = $this->ImageMagickVersion();
  995. if ($which_convert && ($which_convert{0} == '/') && @file_exists($which_convert)) {
  996. // `which convert` *should* return the path if "convert" exist, or nothing if it doesn't
  997. // other things *may* get returned, like "sh: convert: not found" or "no convert in /usr/local/bin /usr/sbin /usr/bin /usr/ccs/bin"
  998. // so only do this if the value returned exists as a file
  999. $this->DebugMessage('using ImageMagick path from `which convert` ('.$which_convert.')', __FILE__, __LINE__);
  1000. $commandline = 'convert';
  1001. } elseif ($IMversion) {
  1002. $this->DebugMessage('setting ImageMagick path to $this->config_imagemagick_path ('.$this->config_imagemagick_path.') ['.$IMversion.']', __FILE__, __LINE__);
  1003. $commandline = $this->config_imagemagick_path;
  1004. } else {
  1005. $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__);
  1006. $commandline = '';
  1007. }
  1008. }
  1009. return $commandline;
  1010. }
  1011. function ImageMagickVersion($returnRAW=false) {
  1012. static $versionstring = null;
  1013. if (is_null($versionstring)) {
  1014. $commandline = $this->ImageMagickCommandlineBase();
  1015. $commandline = (!is_null($commandline) ? $commandline : '');
  1016. $versionstring = array(0=>'', 1=>'');
  1017. if ($commandline) {
  1018. $commandline .= ' --version';
  1019. $this->DebugMessage('ImageMagick version checked with "'.$commandline.'"', __FILE__, __LINE__);
  1020. $versionstring[1] = trim(phpthumb_functions::SafeExec($commandline));
  1021. if (eregi('^Version: [^0-9]*([ 0-9\\.\\:Q/]+) (http|file)\:', $versionstring[1], $matches)) {
  1022. $versionstring[0] = $matches[1];
  1023. } else {
  1024. $versionstring[0] = false;
  1025. $this->DebugMessage('ImageMagick did not return recognized version string ('.$versionstring[1].')', __FILE__, __LINE__);
  1026. }
  1027. $this->DebugMessage('ImageMagick convert --version says "'.$matches[0].'"', __FILE__, __LINE__);
  1028. }
  1029. }
  1030. return @$versionstring[intval($returnRAW)];
  1031. }
  1032. function ImageMagickSwitchAvailable($switchname) {
  1033. static $IMoptions = null;
  1034. if (is_null($IMoptions)) {
  1035. $IMo

Large files files are truncated, but you can click here to view the full file