imagemanager.php

来自「国外很不错的一个开源OA系统Group-Office」· PHP 代码 · 共 676 行 · 第 1/2 页

PHP
676
字号
	 * @return boolean true if the path exists, false otherwise	 */	function validRelativePath($path) 	{		$dirs = $this->getDirs();		if($path == '/')			Return true;		//check the path given in the url against the 		//list of paths in the system.		for($i = 0; $i < count($dirs); $i++)		{			$key = key($dirs);			//we found the path			if($key == $path)				Return true;					next($dirs);		}				Return false;	}	/**	 * Process uploaded files, assumes the file is in 	 * $_FILES['upload'] and $_POST['dir'] is set.	 * The dir must be relative to the base_dir and exists.	 * If 'validate_images' is set to true, only file with	 * image dimensions will be accepted.	 * @return null	 */	function processUploads() 	{		if($this->isValidBase() == false)			return;		$relative = null;		if(isset($_POST['dir'])) 			$relative = rawurldecode($_POST['dir']);		else			return;		//check for the file, and must have valid relative path		if(isset($_FILES['upload']) && $this->validRelativePath($relative))		{			$this->_processFiles($relative, $_FILES['upload']);		}	}	/**	 * Process upload files. The file must be an 	 * uploaded file. If 'validate_images' is set to	 * true, only images will be processed. Any duplicate	 * file will be renamed. See Files::copyFile for details	 * on renaming.	 * @param string $relative the relative path where the file	 * should be copied to.	 * @param array $file the uploaded file from $_FILES	 * @return boolean true if the file was processed successfully, 	 * false otherwise	 */	function _processFiles($relative, $file)	{				if($file['error']!=0)		{			Return false;		}		if(!is_file($file['tmp_name']))		{			Return false;		}		if(!is_uploaded_file($file['tmp_name']))		{			Files::delFile($file['tmp_name']);			Return false;		}				if($this->config['validate_images'] == true)		{			$imgInfo = @getImageSize($file['tmp_name']);			if(!is_array($imgInfo))			{				Files::delFile($file['tmp_name']);				Return false;			}		}		//now copy the file		$path = Files::makePath($this->getBaseDir(),$relative);		$result = Files::copyFile($file['tmp_name'], $path, $file['name']);		//no copy error		if(!is_int($result))		{			Files::delFile($file['tmp_name']);			Return true;		}		//delete tmp files.		Files::delFile($file['tmp_name']);		Return false;	}	/**	 * Get the URL of the relative file.	 * basically appends the relative file to the 	 * base_url given in config.inc.php	 * @param string $relative a file the relative to the base_dir	 * @return string the URL of the relative file.	 */	function getFileURL($relative) 	{		Return Files::makeFile($this->getBaseURL(),$relative);	}	/**	 * Get the fullpath to a relative file.	 * @param string $relative the relative file.	 * @return string the full path, .ie. the base_dir + relative.	 */	function getFullPath($relative) 	{		Return Files::makeFile($this->getBaseDir(),$relative);;	}	/**	 * Get the default thumbnail.	 * @return string default thumbnail, empty string if 	 * the thumbnail doesn't exist.	 */	function getDefaultThumb() 	{		if(is_file($this->config['default_thumbnail']))			Return $this->config['default_thumbnail'];		else 			Return '';	}	/**	 * Get the thumbnail url to be displayed. 	 * If the thumbnail exists, and it is up-to-date	 * the thumbnail url will be returns. If the 	 * file is not an image, a default image will be returned.	 * If it is an image file, and no thumbnail exists or 	 * the thumbnail is out-of-date (i.e. the thumbnail 	 * modified time is less than the original file)	 * then a thumbs.php?img=filename.jpg is returned.	 * The thumbs.php url will generate a new thumbnail	 * on the fly. If the image is less than the dimensions	 * of the thumbnails, the image will be display instead.	 * @param string $relative the relative image file.	 * @return string the url of the thumbnail, be it	 * actually thumbnail or a script to generate the	 * thumbnail on the fly.	 */	function getThumbnail($relative) 	{		$fullpath = Files::makeFile($this->getBaseDir(),$relative);		//not a file???		if(!is_file($fullpath))			Return $this->getDefaultThumb();		$imgInfo = @getImageSize($fullpath);				//not an image		if(!is_array($imgInfo))			Return $this->getDefaultThumb();		//the original image is smaller than thumbnails,		//so just return the url to the original image.		if ($imgInfo[0] <= $this->config['thumbnail_width']		 && $imgInfo[1] <= $this->config['thumbnail_height'])			Return $this->getFileURL($relative);		$thumbnail = $this->getThumbName($fullpath);				//check for thumbnails, if exists and		// it is up-to-date, return the thumbnail url		if(is_file($thumbnail))		{			if(filemtime($thumbnail) >= filemtime($fullpath))				Return $this->getThumbURL($relative);		}		//well, no thumbnail was found, so ask the thumbs.php		//to generate the thumbnail on the fly.		Return 'thumbs.php?img='.rawurlencode($relative);	}	/**	 * Delete and specified files.	 * @return boolean true if delete, false otherwise	 */	function deleteFiles() 	{		if(isset($_GET['delf']))			$this->_delFile(rawurldecode($_GET['delf']));	}	/**	 * Delete and specified directories.	 * @return boolean true if delete, false otherwise	 */	function deleteDirs() 	{		 if(isset($_GET['deld']))			return $this->_delDir(rawurldecode($_GET['deld']));				 else			 Return false;	}	/**	 * Delete the relative file, and any thumbnails.	 * @param string $relative the relative file.	 * @return boolean true if deleted, false otherwise.	 */	function _delFile($relative) 	{		$fullpath = Files::makeFile($this->getBaseDir(),$relative);				//check that the file is an image		if($this->config['validate_images'] == true)		{			if(!is_array($this->getImageInfo($fullpath)))				return false; //hmmm not an Image!!???		}		$thumbnail = $this->getThumbName($fullpath);		if(Files::delFile($fullpath))			Return Files::delFile($thumbnail);		else			Return false;	}	/**	 * Delete directories recursively.	 * @param string $relative the relative path to be deleted.	 * @return boolean true if deleted, false otherwise.	 */	function _delDir($relative) 	{		$fullpath = Files::makePath($this->getBaseDir(),$relative);		if($this->countFiles($fullpath) <= 0)			return Files::delFolder($fullpath,true); //delete recursively.		else			Return false;	}	/**	 * Create new directories.	 * If in safe_mode, nothing happens.	 * @return boolean true if created, false otherwise.	 */	function processNewDir() 	{		if($this->config['safe_mode'] == true)			Return false;		if(isset($_GET['newDir']) && isset($_GET['dir']))		{			$newDir = rawurldecode($_GET['newDir']);			$dir = rawurldecode($_GET['dir']);			$path = Files::makePath($this->getBaseDir(),$dir);			$fullpath = Files::makePath($path, Files::escape($newDir));			if(is_dir($fullpath))				Return false;			Return Files::createFolder($fullpath);		}	}	/**	 * Do some graphic library method checkings	 * @param string $library the graphics library, GD, NetPBM, or IM.	 * @param string $method the method to check.	 * @return boolean true if able, false otherwise.	 */	function validGraphicMethods($library,$method)	{		switch ($library)		{			case 'GD':				return $this->_checkGDLibrary($method);				break;			case 'NetPBM':				return $this->_checkNetPBMLibrary($method);				break;			case 'IM':				return $this->_checkIMLibrary($method);		}		return false;	}	function _checkIMLibrary($method)	{		//ImageMagick goes throught 1 single executable		if(is_file(Files::fixPath(IMAGE_TRANSFORM_LIB_PATH).'convert'))			return true;		else			return false;	}	/**	 * Check the GD library functionality.	 * @param string $library the graphics library, GD, NetPBM, or IM.	 * @return boolean true if able, false otherwise.	 */	function _checkGDLibrary($method)	{		$errors = array();		switch($method)		{			case 'create':				$errors['createjpeg'] = function_exists('imagecreatefromjpeg');				$errors['creategif'] = function_exists('imagecreatefromgif');				$errors['createpng'] = function_exists('imagecreatefrompng');				break;			case 'modify':				$errors['create'] = function_exists('ImageCreateTrueColor') || function_exists('ImageCreate');				$errors['copy'] = function_exists('ImageCopyResampled') || function_exists('ImageCopyResized');				break;			case 'save':				$errors['savejpeg'] = function_exists('imagejpeg');				$errors['savegif'] = function_exists('imagegif');				$errors['savepng'] = function_exists('imagepng');				break;		}		return $errors;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?