| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
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 11 ob_start(); 12 if (!include_once(dirname(__FILE__).'/phpthumb.functions.php')) { 13 ob_end_flush(); 14 die('failed to include_once("'.realpath(dirname(__FILE__).'/phpthumb.functions.php').'")'); 15 } 16 ob_end_clean(); 17 18 class phpthumb { 19 20 // public: 21 // START PARAMETERS (for object mode and phpThumb.php) 22 // See phpthumb.readme.txt for descriptions of what each of these values are 23 var $src = null; // SouRCe filename 24 var $new = null; // NEW image (phpThumb.php only) 25 var $w = null; // Width 26 var $h = null; // Height 27 var $wp = null; // Width (Portrait Images Only) 28 var $hp = null; // Height (Portrait Images Only) 29 var $wl = null; // Width (Landscape Images Only) 30 var $hl = null; // Height (Landscape Images Only) 31 var $ws = null; // Width (Square Images Only) 32 var $hs = null; // Height (Square Images Only) 33 var $f = null; // Format 34 var $q = 75; // jpeg output Quality 35 var $sx = null; // Source crop top-left X position 36 var $sy = null; // Source crop top-left Y position 37 var $sw = null; // Source crop Width 38 var $sh = null; // Source crop Height 39 var $zc = null; // Zoom Crop 40 var $bc = null; // Border Color 41 var $bg = null; // BackGround color 42 var $fltr = array(); // FiLTeRs 43 var $goto = null; // GO TO url after processing 44 var $err = null; // default ERRor image filename 45 var $xto = null; // extract eXif Thumbnail Only 46 var $ra = null; // Rotate by Angle 47 var $ar = null; // Auto Rotate 48 var $aoe = null; // Allow Output Enlargement 49 var $far = null; // Fixed Aspect Ratio 50 var $iar = null; // Ignore Aspect Ratio 51 var $maxb = null; // MAXimum Bytes 52 var $down = null; // DOWNload thumbnail filename 53 var $md5s = null; // MD5 hash of Source image 54 var $file = null; // >>deprecated, do not use<< 55 56 var $phpThumbDebug = null; 57 // END PARAMETERS 58 59 60 // public: 61 // START CONFIGURATION OPTIONS (for object mode only) 62 // See phpThumb.config.php for descriptions of what each of these settings do 63 64 // * Directory Configuration 65 var $config_cache_directory = null; 66 var $config_cache_disable_warning = true; 67 var $config_cache_source_enabled = false; 68 var $config_cache_source_directory = null; 69 var $config_temp_directory = null; 70 var $config_document_root = null; 71 72 // * Default output configuration: 73 var $config_output_format = 'jpeg'; 74 var $config_output_maxwidth = 0; 75 var $config_output_maxheight = 0; 76 var $config_output_interlace = true; 77 78 // * Error message configuration 79 var $config_error_image_width = 400; 80 var $config_error_image_height = 100; 81 var $config_error_message_image_default = ''; 82 var $config_error_bgcolor = 'CCCCFF'; 83 var $config_error_textcolor = 'FF0000'; 84 var $config_error_fontsize = 1; 85 var $config_error_die_on_error = false; 86 var $config_error_silent_die_on_error = false; 87 var $config_error_die_on_source_failure = true; 88 89 // * Anti-Hotlink Configuration: 90 var $config_nohotlink_enabled = true; 91 var $config_nohotlink_valid_domains = array(); 92 var $config_nohotlink_erase_image = true; 93 var $config_nohotlink_text_message = 'Off-server thumbnailing is not allowed'; 94 // * Off-server Linking Configuration: 95 var $config_nooffsitelink_enabled = false; 96 var $config_nooffsitelink_valid_domains = array(); 97 var $config_nooffsitelink_require_refer = false; 98 var $config_nooffsitelink_erase_image = true; 99 var $config_nooffsitelink_text_message = 'Off-server linking is not allowed'; 100 101 // * Border & Background default colors 102 var $config_border_hexcolor = '000000'; 103 var $config_background_hexcolor = 'FFFFFF'; 104 105 // * TrueType Fonts 106 var $config_ttf_directory = '.'; 107 108 var $config_max_source_pixels = null; 109 var $config_use_exif_thumbnail_for_speed = false; 110 var $allow_local_http_src = false; 111 112 var $config_imagemagick_path = null; 113 var $config_prefer_imagemagick = true; 114 115 var $config_cache_maxage = null; 116 var $config_cache_maxsize = null; 117 var $config_cache_maxfiles = null; 118 var $config_cache_source_filemtime_ignore_local = false; 119 var $config_cache_source_filemtime_ignore_remote = true; 120 var $config_cache_default_only_suffix = false; 121 var $config_cache_force_passthru = true; 122 var $config_cache_prefix = ''; // default value set in the constructor below 123 124 // * MySQL 125 var $config_mysql_query = null; 126 var $config_mysql_hostname = null; 127 var $config_mysql_username = null; 128 var $config_mysql_password = null; 129 var $config_mysql_database = null; 130 131 // * Security 132 var $config_high_security_enabled = false; 133 var $config_high_security_password = null; 134 var $config_disable_debug = false; 135 var $config_allow_src_above_docroot = false; 136 var $config_allow_src_above_phpthumb = true; 137 var $config_allow_parameter_file = false; 138 var $config_allow_parameter_goto = false; 139 140 // * Compatability 141 var $config_disable_pathinfo_parsing = false; 142 var $config_disable_imagecopyresampled = false; 143 var $config_disable_onlycreateable_passthru = false; 144 145 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'; 146 147 // END CONFIGURATION OPTIONS 148 149 150 // public: error messages (read-only) 151 var $debugmessages = array(); 152 var $debugtiming = array(); 153 var $fatalerror = null; 154 155 156 // private: (should not be modified directly) 157 var $thumbnailQuality = 75; 158 var $thumbnailFormat = null; 159 160 var $sourceFilename = null; 161 var $rawImageData = null; 162 var $IMresizedData = null; 163 var $outputImageData = null; 164 165 var $useRawIMoutput = false; 166 167 var $gdimg_output = null; 168 var $gdimg_source = null; 169 170 var $getimagesizeinfo = null; 171 172 var $source_width = null; 173 var $source_height = null; 174 175 var $thumbnailCropX = null; 176 var $thumbnailCropY = null; 177 var $thumbnailCropW = null; 178 var $thumbnailCropH = null; 179 180 var $exif_thumbnail_width = null; 181 var $exif_thumbnail_height = null; 182 var $exif_thumbnail_type = null; 183 var $exif_thumbnail_data = null; 184 var $exif_raw_data = null; 185 186 var $thumbnail_width = null; 187 var $thumbnail_height = null; 188 var $thumbnail_image_width = null; 189 var $thumbnail_image_height = null; 190 191 var $cache_filename = null; 192 193 var $AlphaCapableFormats = array('png', 'ico', 'gif'); 194 var $is_alpha = false; 195 196 var $iswindows = null; 197 198 var $phpthumb_version = '1.7.3-200607111933'; 199 200 ////////////////////////////////////////////////////////////////////// 201 202 // public: constructor 203 function phpThumb() { 204 $this->DebugTimingMessage('phpThumb() constructor', __FILE__, __LINE__); 205 $this->DebugMessage('phpThumb() v'.$this->phpthumb_version, __FILE__, __LINE__); 206 $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 207 $this->iswindows = (bool) (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'); 208 $this->config_temp_directory = realpath($this->config_temp_directory ? $this->config_temp_directory : (getenv('TMPDIR') ? getenv('TMPDIR') : getenv('TMP'))); 209 $this->config_document_root = (@$_SERVER['DOCUMENT_ROOT'] ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root); 210 $this->config_cache_prefix = 'phpThumb_cache_'.@$_SERVER['SERVER_NAME']; 211 212 $php_sapi_name = strtolower(function_exists('php_sapi_name') ? php_sapi_name() : ''); 213 if ($php_sapi_name == 'cli') { 214 $this->config_allow_src_above_docroot = true; 215 } 216 } 217 218 // public: 219 function setSourceFilename($sourceFilename) { 220 $this->rawImageData = null; 221 $this->sourceFilename = $sourceFilename; 222 $this->src = $sourceFilename; 223 $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->sourceFilename to "'.$this->sourceFilename.'"', __FILE__, __LINE__); 224 return true; 225 } 226 227 // public: 228 function setSourceData($rawImageData, $sourceFilename='') { 229 $this->sourceFilename = null; 230 $this->rawImageData = $rawImageData; 231 $this->DebugMessage('setSourceData() setting $this->rawImageData ('.strlen($this->rawImageData).' bytes)', __FILE__, __LINE__); 232 if ($this->config_cache_source_enabled) { 233 $sourceFilename = ($sourceFilename ? $sourceFilename : md5($rawImageData)); 234 if (!is_dir($this->config_cache_source_directory)) { 235 $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not a directory'); 236 } elseif (!@is_writable($this->config_cache_source_directory)) { 237 $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not writable'); 238 } 239 $this->DebugMessage('setSourceData() attempting to save source image to "'.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).'"', __FILE__, __LINE__); 240 if ($fp = @fopen($this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename), 'wb')) { 241 fwrite($fp, $rawImageData); 242 fclose($fp); 243 } elseif (!$this->phpThumbDebug) { 244 $this->ErrorImage('setSourceData() failed to write to source cache ('.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).')'); 245 } 246 } 247 return true; 248 } 249 250 // public: 251 function setSourceImageResource($gdimg) { 252 $this->gdimg_source = $gdimg; 253 return true; 254 } 255 256 // public: 257 function setParameter($param, $value) { 258 if ($param == 'src') { 259 $this->setSourceFilename($this->ResolveFilenameToAbsolute($value)); 260 } elseif (@is_array($this->$param)) { 261 if (is_array($value)) { 262 foreach ($value as $arraykey => $arrayvalue) { 263 array_push($this->$param, $arrayvalue); 264 } 265 } else { 266 array_push($this->$param, $value); 267 } 268 } else { 269 $this->$param = $value; 270 } 271 return true; 272 } 273 274 // public: 275 function getParameter($param) { 276 //if (property_exists('phpThumb', $param)) { 277 return $this->$param; 278 //} 279 //$this->DebugMessage('setParameter() attempting to get non-existant parameter "'.$param.'"', __FILE__, __LINE__); 280 //return false; 281 } 282 283 284 // public: 285 function GenerateThumbnail() { 286 287 $this->setOutputFormat(); 288 $this->ResolveSource(); 289 $this->SetCacheFilename(); 290 $this->ExtractEXIFgetImageSize(); 291 if ($this->useRawIMoutput) { 292 $this->DebugMessage('Skipping rest of GenerateThumbnail() because $this->useRawIMoutput', __FILE__, __LINE__); 293 return true; 294 } 295 if (!$this->SourceImageToGD()) { 296 $this->DebugMessage('SourceImageToGD() failed', __FILE__, __LINE__); 297 return false; 298 } 299 $this->Rotate(); 300 $this->CreateGDoutput(); 301 302 switch ($this->far) { 303 case 'L': 304 case 'TL': 305 case 'BL': 306 $destination_offset_x = 0; 307 $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2); 308 break; 309 case 'R': 310 case 'TR': 311 case 'BR': 312 $destination_offset_x = round($this->thumbnail_width - $this->thumbnail_image_width); 313 $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2); 314 break; 315 case 'T': 316 case 'TL': 317 case 'TR': 318 $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2); 319 $destination_offset_y = 0; 320 break; 321 case 'B': 322 case 'BL': 323 case 'BR': 324 $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2); 325 $destination_offset_y = round($this->thumbnail_height - $this->thumbnail_image_height); 326 break; 327 case 'C': 328 default: 329 $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2); 330 $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2); 331 } 332 333 // // copy/resize image to appropriate dimensions 334 // $borderThickness = 0; 335 // if (!empty($this->fltr)) { 336 // foreach ($this->fltr as $key => $value) { 337 // if (ereg('^bord\|([0-9]+)', $value, $matches)) { 338 // $borderThickness = $matches[1]; 339 // break; 340 // } 341 // } 342 // } 343 // if ($borderThickness > 0) { 344 // //$this->DebugMessage('Skipping ImageResizeFunction() because BorderThickness="'.$borderThickness.'"', __FILE__, __LINE__); 345 // $this->thumbnail_image_height /= 2; 346 // } 347 $this->ImageResizeFunction( 348 $this->gdimg_output, 349 $this->gdimg_source, 350 $destination_offset_x, 351 $destination_offset_y, 352 $this->thumbnailCropX, 353 $this->thumbnailCropY, 354 $this->thumbnail_image_width, 355 $this->thumbnail_image_height, 356 $this->thumbnailCropW, 357 $this->thumbnailCropH 358 ); 359 360 $this->DebugMessage('memory_get_usage() after copy-resize = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__); 361 ImageDestroy($this->gdimg_source); 362 $this->DebugMessage('memory_get_usage() after ImageDestroy = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__); 363 364 $this->AntiOffsiteLinking(); 365 $this->ApplyFilters(); 366 $this->AlphaChannelFlatten(); 367 $this->MaxFileSize(); 368 369 $this->DebugMessage('GenerateThumbnail() completed successfully', __FILE__, __LINE__); 370 return true; 371 } 372 373 374 // public: 375 function RenderOutput() { 376 if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) { 377 $this->DebugMessage('RenderOutput() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__); 378 return false; 379 } 380 if (!$this->thumbnailFormat) { 381 $this->DebugMessage('RenderOutput() failed because $this->thumbnailFormat is empty', __FILE__, __LINE__); 382 return false; 383 } 384 if ($this->useRawIMoutput) { 385 $this->DebugMessage('RenderOutput copying $this->IMresizedData ('.strlen($this->IMresizedData).' bytes) to $this->outputImage', __FILE__, __LINE__); 386 $this->outputImageData = $this->IMresizedData; 387 return true; 388 } 389 390 $this->DebugMessage('RenderOutput() attempting Image'.strtoupper(@$this->thumbnailFormat).'($this->gdimg_output)', __FILE__, __LINE__); 391 ob_start(); 392 switch ($this->thumbnailFormat) { 393 case 'jpeg': 394 ImageJPEG($this->gdimg_output, null, $this->thumbnailQuality); 395 $this->outputImageData = ob_get_contents(); 396 break; 397 398 case 'png': 399 ImagePNG($this->gdimg_output); 400 $this->outputImageData = ob_get_contents(); 401 break; 402 403 case 'gif': 404 ImageGIF($this->gdimg_output); 405 $this->outputImageData = ob_get_contents(); 406 break; 407 408 case 'bmp': 409 $ImageOutFunction = '"builtin BMP output"'; 410 if (!@include_once(dirname(__FILE__).'/phpthumb.bmp.php')) { 411 $this->DebugMessage('Error including "'.