📄 phpthumb.class.php
字号:
}
// copy/resize image to appropriate dimensions
phpthumb_functions::ImageResizeFunction(
$this->gdimg_output,
$this->gdimg_source,
$destination_offset_x,
$destination_offset_y,
$this->thumbnailCropX,
$this->thumbnailCropY,
$this->thumbnail_image_width,
$this->thumbnail_image_height,
$this->thumbnailCropW,
$this->thumbnailCropH
);
$this->AntiOffsiteLinking();
$this->ApplyFilters();
$this->AlphaChannelFlatten();
$this->MaxFileSize();
$this->DebugMessage('GenerateThumbnail() completed successfully', __FILE__, __LINE__);
return true;
}
// public:
function RenderToFile($filename) {
if (!is_resource($this->gdimg_output)) {
$this->DebugMessage('RenderToFile('.$filename.') failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
return false;
}
if (!$this->thumbnailFormat) {
$this->DebugMessage('RenderToFile() failed because $this->thumbnailFormat is empty', __FILE__, __LINE__);
return false;
}
// render thumbnail to this file only, do not cache, do not output to browser
//$renderfilename = $this->ResolveFilenameToAbsolute(dirname($filename)).$this->osslash.basename($filename);
$renderfilename = $filename;
if (($filename{0} != '/') && ($filename{0} != '\\') && ($filename{1} != ':')) {
$renderfilename = $this->ResolveFilenameToAbsolute($renderfilename);
}
$this->DebugMessage('RenderToFile('.$filename.') attempting '.$ImageOutFunction.'($this->gdimg_output, '.$renderfilename.')', __FILE__, __LINE__);
if (!is_writable(dirname($renderfilename))) {
$this->DebugMessage(dirname($renderfilename).'/ is not writable', __FILE__, __LINE__);
}
if (is_file($renderfilename) && !is_writable($renderfilename)) {
$this->DebugMessage($renderfilename.' is not writable', __FILE__, __LINE__);
}
ob_start();
switch ($this->thumbnailFormat) {
case 'jpeg':
$ImageOutFunction = 'image'.$this->thumbnailFormat;
$ImageOutFunction($this->gdimg_output, $renderfilename, $this->thumbnailQuality);
break;
case 'png':
case 'gif':
$ImageOutFunction = 'image'.$this->thumbnailFormat;
$ImageOutFunction($this->gdimg_output, $renderfilename);
break;
default:
$this->DebugMessage('RenderToFile failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
return false;
}
$errormessage = strip_tags(ob_get_contents());
ob_end_clean();
if ($errormessage) {
$this->DebugMessage('RenderToFile ['.$ImageOutFunction.'('.$renderfilename.')] failed with message "'.$errormessage.'"', __FILE__, __LINE__);
return false;
} elseif (!file_exists($renderfilename)) {
$this->DebugMessage('RenderToFile ['.$ImageOutFunction.'('.$renderfilename.')] did not appear to fail, but the output image does not exist either...', __FILE__, __LINE__);
}
return true;
}
// public:
function OutputThumbnail() {
if (!is_resource($this->gdimg_output)) {
$this->DebugMessage('OutputThumbnail() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
return false;
}
if (headers_sent()) {
return $this->ErrorImage('OutputThumbnail() failed - headers already sent');
exit;
}
if ($this->down) {
$downloadfilename = ereg_replace('[/\\:\*\?"<>|]', '_', $this->down);
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.1.0', '>=')) {
$downloadfilename = trim($downloadfilename, '.');
}
if ($downloadfilename != $this->down) {
$this->DebugMessage('renaming output file for "down" from "'.$this->down.'" to "'.$downloadfilename.'"', __FILE__, __LINE__);
}
if ($downloadfilename) {
header('Content-Disposition: attachment; filename="'.$downloadfilename.'"');
} else {
$this->DebugMessage('failed to send Content-Disposition header because $downloadfilename is empty', __FILE__, __LINE__);
}
}
ImageInterlace($this->gdimg_output, intval($this->config_output_interlace));
switch ($this->thumbnailFormat) {
case 'jpeg':
header('Content-Type: image/'.$this->thumbnailFormat);
$ImageOutFunction = 'image'.$this->thumbnailFormat;
@$ImageOutFunction($this->gdimg_output, '', $this->thumbnailQuality);
break;
case 'png':
case 'gif':
header('Content-Type: image/'.$this->thumbnailFormat);
$ImageOutFunction = 'image'.$this->thumbnailFormat;
@$ImageOutFunction($this->gdimg_output);
break;
default:
$this->DebugMessage('OutputThumbnail failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
return false;
break;
}
ImageDestroy($this->gdimg_output);
return true;
}
// public:
function CleanUpCacheDirectory() {
if (($this->config_cache_maxage > 0) || ($this->config_cache_maxsize > 0) || ($this->config_cache_maxfiles > 0)) {
$CacheDirOldFilesAge = array();
$CacheDirOldFilesSize = array();
if ($dirhandle = opendir($this->config_cache_directory)) {
while ($oldcachefile = readdir($dirhandle)) {
if (eregi('^phpThumb_cache_', $oldcachefile)) {
$CacheDirOldFilesAge[$oldcachefile] = fileatime($this->config_cache_directory.$this->osslash.$oldcachefile);
if ($CacheDirOldFilesAge[$oldcachefile] == 0) {
$CacheDirOldFilesAge[$oldcachefile] = filemtime($this->config_cache_directory.$this->osslash.$oldcachefile);
}
$CacheDirOldFilesSize[$oldcachefile] = filesize($this->config_cache_directory.$this->osslash.$oldcachefile);
}
}
}
asort($CacheDirOldFilesAge);
if ($this->config_cache_maxfiles > 0) {
$TotalCachedFiles = count($CacheDirOldFilesAge);
$DeletedKeys = array();
foreach ($CacheDirOldFilesAge as $oldcachefile => $filedate) {
if ($TotalCachedFiles > $this->config_cache_maxfiles) {
$TotalCachedFiles--;
if (@unlink($this->config_cache_directory.$this->osslash.$oldcachefile)) {
$DeletedKeys[] = $oldcachefile;
}
} else {
// there are few enough files to keep the rest
break;
}
}
foreach ($DeletedKeys as $oldcachefile) {
unset($CacheDirOldFilesAge[$oldcachefile]);
unset($CacheDirOldFilesSize[$oldcachefile]);
}
}
if ($this->config_cache_maxage > 0) {
$mindate = time() - $this->config_cache_maxage;
$DeletedKeys = array();
foreach ($CacheDirOldFilesAge as $oldcachefile => $filedate) {
if ($filedate > 0) {
if ($filedate < $mindate) {
if (@unlink($this->config_cache_directory.$this->osslash.$oldcachefile)) {
$DeletedKeys[] = $oldcachefile;
}
} else {
// the rest of the files are new enough to keep
break;
}
}
}
foreach ($DeletedKeys as $oldcachefile) {
unset($CacheDirOldFilesAge[$oldcachefile]);
unset($CacheDirOldFilesSize[$oldcachefile]);
}
}
if ($this->config_cache_maxsize > 0) {
$TotalCachedFileSize = array_sum($CacheDirOldFilesSize);
$DeletedKeys = array();
foreach ($CacheDirOldFilesAge as $oldcachefile => $filedate) {
if ($TotalCachedFileSize > $this->config_cache_maxsize) {
$TotalCachedFileSize -= $CacheDirOldFilesSize[$oldcachefile];
if (@unlink($this->config_cache_directory.$this->osslash.$oldcachefile)) {
$DeletedKeys[] = $oldcachefile;
}
} else {
// the total filesizes are small enough to keep the rest of the files
break;
}
}
foreach ($DeletedKeys as $oldcachefile) {
unset($CacheDirOldFilesAge[$oldcachefile]);
unset($CacheDirOldFilesSize[$oldcachefile]);
}
}
}
return true;
}
//////////////////////////////////////////////////////////////////////
function ResolveSource() {
if (is_resource($this->gdimg_source)) {
return true;
}
if ($this->rawImageData) {
$this->sourceFilename = null;
return true;
}
if ($this->sourceFilename) {
$this->sourceFilename = $this->ResolveFilenameToAbsolute($this->sourceFilename);
$this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
} else {
$this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
$this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'" from $this->src ('.$this->src.')', __FILE__, __LINE__);
}
if ($this->iswindows && ((substr($this->sourceFilename, 0, 2) == '//') || (substr($this->sourceFilename, 0, 2) == '\\\\'))) {
// Windows \\share\filename.ext
} elseif (eregi('^(f|ht)tps?://', $this->sourceFilename)) {
// URL
} elseif (!file_exists($this->sourceFilename)) {
return $this->ErrorImage('"'.$this->sourceFilename.'" does not exist');
} elseif (!is_file($this->sourceFilename)) {
return $this->ErrorImage('"'.$this->sourceFilename.'" is not a file');
}
return true;
}
function setOutputFormat() {
static $alreadyCalled = false;
if ($this->thumbnailFormat && $alreadyCalled) {
return true;
}
$alreadyCalled = true;
$AvailableImageOutputFormats = array();
$AvailableImageOutputFormats[] = 'text';
$this->thumbnailFormat = 'text';
// Set default output format based on what image types are available
if (!function_exists('ImageTypes')) {
return $this->ErrorImage('ImageTypes() does not exist - GD support might not be enabled?');
}
$imagetypes = ImageTypes();
if ($imagetypes & IMG_WBMP) {
$this->thumbnailFormat = 'wbmp';
$AvailableImageOutputFormats[] = 'wbmp';
}
if ($imagetypes & IMG_GIF) {
$this->thumbnailFormat = 'gif';
$AvailableImageOutputFormats[] = 'gif';
}
if ($imagetypes & IMG_PNG) {
$this->thumbnailFormat = 'png';
$AvailableImageOutputFormats[] = 'png';
}
if ($imagetypes & IMG_JPG) {
$this->thumbnailFormat = 'jpeg';
$AvailableImageOutputFormats[] = 'jpeg';
}
$this->DebugMessage('$AvailableImageOutputFormats = array('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
if (strtolower($this->config_output_format) == 'jpg') {
$this->config_output_format = 'jpeg';
}
if (strtolower($this->f) == 'jpg') {
$this->f = 'jpeg';
}
if (in_array(strtolower($this->config_output_format), $AvailableImageOutputFormats)) {
// set output format to config default if that format is available
$this->DebugMessage('$this->thumbnailFormat set to $this->config_output_format "'.strtolower($this->config_output_format).'"', __FILE__, __LINE__);
$this->thumbnailFormat = strtolower($this->config_output_format);
} elseif ($this->config_output_format) {
$this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->config_output_format ('.strtolower($this->config_output_format).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
}
if ($this->f && (in_array(strtolower($this->f), $AvailableImageOutputFormats))) {
// override output format if $this->f is set and that format is available
$this->DebugMessage('$this->thumbnailFormat set to $this->f "'.strtolower($this->f).'"', __FILE__, __LINE__);
$this->thumbnailFormat = strtolower($this->f);
} elseif ($this->f) {
$this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->f ('.strtolower($this->f).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
}
// for JPEG images, quality 1 (worst) to 99 (best)
// quality < 25 is nasty, with not much size savings - not recommended
// problems with 100 - invalid JPEG?
$this->thumbnailQuality = max(1, min(99, ($this->q ? $this->q : 75)));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -