Aca sigue:
Código PHP:
/**
*
*/
function get_request( $property, $default = 0 ) {
if( isset($_REQUEST[$property]) ) {
return $_REQUEST[$property];
} else {
return $default;
}
}
/**
*
*/
function open_image($mime_type, $src) {
if(stristr($mime_type, 'gif')) {
$image = imagecreatefromgif($src);
} elseif(stristr($mime_type, 'jpeg')) {
@ini_set('gd.jpeg_ignore_warning', 1);
$image = imagecreatefromjpeg($src);
} elseif( stristr($mime_type, 'png')) {
$image = imagecreatefrompng($src);
}
return $image;
}
/**
* clean out old files from the cache
* you can change the number of files to store and to delete per loop in the defines at the top of the code
*/
function cleanCache() {
$files = glob("cache/*", GLOB_BRACE);
$yesterday = time() - (24 * 60 * 60);
if (count($files) > 0) {
usort($files, "filemtime_compare");
$i = 0;
if (count($files) > CACHE_SIZE) {
foreach ($files as $file) {
$i ++;
if ($i >= CACHE_CLEAR) {
return;
}
if (filemtime($file) > $yesterday) {
return;
}
unlink($file);
}
}
}
}
/**
* compare the file time of two files
*/
function filemtime_compare($a, $b) {
return filemtime($a) - filemtime($b);
}
/**
* determine the file mime type
*/
function mime_type($file) {
if (stristr(PHP_OS, 'WIN')) {
$os = 'WIN';
} else {
$os = PHP_OS;
}
$mime_type = '';
if (function_exists('mime_content_type')) {
$mime_type = mime_content_type($file);
}
// use PECL fileinfo to determine mime type
if (!valid_src_mime_type($mime_type)) {
if (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($finfo, $file);
finfo_close($finfo);
}
}
// try to determine mime type by using unix file command
// this should not be executed on windows
if (!valid_src_mime_type($mime_type) && $os != "WIN") {
if (preg_match("/FREEBSD|LINUX/", $os)) {
$mime_type = trim(@shell_exec('file -bi "' . $file . '"'));
}
}
// use file's extension to determine mime type
if (!valid_src_mime_type($mime_type)) {
// set defaults
$mime_type = 'image/png';
// file details
$fileDetails = pathinfo($file);
$ext = strtolower($fileDetails["extension"]);
// mime types
$types = array(
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif'
);
if (strlen($ext) && strlen($types[$ext])) {
$mime_type = $types[$ext];
}
}
return $mime_type;
}
/**
*
*/
function valid_src_mime_type($mime_type) {
if (preg_match("/jpg|jpeg|gif|png/i", $mime_type)) {
return true;
}
return false;
}
/**
*
*/
function check_cache($cache_dir, $mime_type) {
// make sure cache dir exists
if (!file_exists($cache_dir)) {
// give 777 permissions so that developer can overwrite
// files created by web server user
mkdir($cache_dir);
chmod($cache_dir, 0777);
}
show_cache_file($cache_dir, $mime_type);
}
/**
*
*/
function show_cache_file($cache_dir) {
$cache_file = $cache_dir . '/' . get_cache_file();
if (file_exists($cache_file)) {
$gmdate_mod = gmdate("D, d M Y H:i:s", filemtime($cache_file));
if(! strstr($gmdate_mod, "GMT")) {
$gmdate_mod .= " GMT";
}
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
// check for updates
$if_modified_since = preg_replace("/;.*$/", "", $_SERVER["HTTP_IF_MODIFIED_SINCE"]);
if ($if_modified_since == $gmdate_mod) {
header("HTTP/1.1 304 Not Modified");
exit;
}
}
$fileSize = filesize($cache_file);
// send headers then display image
header("Content-Type: image/png");
header("Accept-Ranges: bytes");
header("Last-Modified: " . $gmdate_mod);
header("Content-Length: " . $fileSize);
header("Cache-Control: max-age=9999, must-revalidate");
header("Expires: " . $gmdate_mod);
readfile($cache_file);
exit;
}
}
/**
*
*/
function get_cache_file() {
global $lastModified;
static $cache_file;
if(!$cache_file) {
$cachename = $_SERVER['QUERY_STRING'] . VERSION . $lastModified;
$cache_file = md5($cachename) . '.png';
}
return $cache_file;
}
/**
* check to if the url is valid or not
*/
function valid_extension ($ext) {
if (preg_match("/jpg|jpeg|png|gif/i", $ext)) {
return TRUE;
} else {
return FALSE;
}
}
/**
* tidy up the image source url
*/
function cleanSource($src) {
// remove slash from start of string
if(strpos($src, "/") == 0) {
$src = substr($src, -(strlen($src) - 1));
}
// remove h ttp/ h ttps/ ftp
$src = preg_replace("/^((ht|f)tp(s|):\/\/)/i", "", $src);
// remove domain name from the source url
$host = $_SERVER["HTTP_HOST"];
$src = str_replace($host, "", $src);
$host = str_replace("w w w.", "", $host);
$src = str_replace($host, "", $src);
// don't allow users the ability to use '../'
// in order to gain access to files below document root
// src should be specified relative to document root like:
// src=images/img.jpg or src=/images/img.jpg
// not like:
// src=../images/img.jpg
$src = preg_replace("/\.\.+\//", "", $src);
// get path to image on file system
$src = get_document_root($src) . '/' . $src;
return $src;
}
/**
*
*/
function get_document_root ($src) {
// check for unix servers
if(@file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $src)) {
return $_SERVER['DOCUMENT_ROOT'];
}
// check from script filename (to get all directories to timthumb location)
$parts = array_diff(explode('/', $_SERVER['SCRIPT_FILENAME']), explode('/', $_SERVER['DOCUMENT_ROOT']));
$path = $_SERVER['DOCUMENT_ROOT'] . '/';
foreach ($parts as $part) {
$path .= $part . '/';
if (file_exists($path . $src)) {
return $path;
}
}
// the relative paths below are useful if timthumb is moved outside of document root
// specifically if installed in wordpress themes like mimbo pro:
// /wp-content/themes/mimbopro/scripts/timthumb.php
$paths = array(
".",
"..",
"../..",
"../../..",
"../../../..",
"../../../../.."
);
foreach($paths as $path) {
if(@file_exists($path . '/' . $src)) {
return $path;
}
}
// special check for microsoft servers
if(!isset($_SERVER['DOCUMENT_ROOT'])) {
$path = str_replace("/", "\\", $_SERVER['ORIG_PATH_INFO']);
$path = str_replace($path, "", $_SERVER['SCRIPT_FILENAME']);
if( @file_exists( $path . '/' . $src ) ) {
return $path;
}
}
displayError('file not found ' . $src);
}
/**
* generic error message
*/
function displayError($errorString = '') {
header('HTTP/1.1 400 Bad Request');
die($errorString);
}
?>
Si alguien sabe como hacerlo le estare muy agradecido que lo comparte.
Desde ya muchas gracias y un saludo!