PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/leganto/document_root/thumb/phpthumb.class.php

http://preader.googlecode.com/
PHP | 3948 lines | 3323 code | 408 blank | 217 comment | 672 complexity | b79bc604ca19e09cb0654cd382b17edb MD5 | raw file
Possible License(s): GPL-2.0

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

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