"image/jpg",
"jpeg" => "image/jpeg",
"gif" => "image/gif",
"png" => "image/png"
);
$PATHS["thumbs"] = $PATHS["images"] . DIRECTORY_SEPARATOR . "thumbs";
$PATHS["checksum"] = $PATHS["images"] . DIRECTORY_SEPARATOR . "checksum";
$START = timestamp();
////////////////////// display functions
function debug ($msg)
{
if (DEBUG)
{
print_debug(nl2br("$msg\n"));
}
}
function debug_dump ($variable)
{
if (DEBUG)
{
echo "
";
var_dump($variable);
echo "
";
}
}
function print_debug($msg)
{
printf('%s', $msg);
flush();
}
function print_error($msg)
{
print_class ($msg, "err");
}
function print_msg ($msg)
{
if (DEBUG || SHOW_MSGS)
{
print_class ($msg, "msg");
}
}
function print_ok ($msg)
{
print_class ($msg, "ok");
}
function print_class ($msg, $class)
{
echo '' . $msg .'
' . "\n";
flush();
}
function format_bytes ($bytes)
{
//$postfix = array(" bytes", "Kb", "Mb", "Gb", "Tb");
//$entry = floor(log10($bytes) / 3);
//return sprintf("%5.2f%s", $bytes / 1024, $postfix[$entry]);
return sprintf("%.2fKb", $bytes / 1024);
}
////////////////////////////////////// path functions
function image_path ($img)
{
global $PATHS;
return $PATHS["images"] . DIRECTORY_SEPARATOR . $img;
}
function checksum_path ($data)
{
global $PATHS;
return $PATHS["checksum"] . DIRECTORY_SEPARATOR . md5($data);
}
function thumb_path ($img)
{
global $PATHS;
return $PATHS["thumbs"] . DIRECTORY_SEPARATOR . $img;
}
//////////////////////////////////////// general miscellansous functions
function image_del ($filename)
{
global $CFG;
// if the file was modified very recently, it's likely it's a file we have
// just tried fetching and maybe the url didn't work. if not, it may be that
// we applied this to a whole bunch of pre-fetched images; if this is the case
// we don't want to be deleting the whole set of images if something doesn't
// work
if ($CFG["DEL_IMG_ON_ERR"] || time() - filemtime(image_path($filename)) < 2)
{
unlink(image_path($filename));
unlink(checksum_path($filename));
}
thumb_del($filename);
}
function thumb_del ($filename)
{
@unlink(thumb_path($filename));
}
function detect_gd2 ()
{
return (detect_gd() && function_exists("gd_info"));
}
function detect_gd ()
{
return (extension_loaded("gd") || @dl("gd"));
}
function detect_convert ()
{
return (strpos(`convert 2>&1`, "imagemagick") !== FALSE);
}
function detect_pnm ()
{
$giftopnm = shell_exec("giftopnm -h 2>&1");
$pnmtopng = shell_exec("pnmtopng --help 2>&1");
debug("giftopnm:$giftopnm:");
debug("pnmtopng:$pnmtopng:");
return (
(strpos($giftopnm, "unrecognized option") !== FALSE) &&
(strpos($pnmtopng, "man pnmtopng") !== FALSE)
);
}
function timestamp ()
{
return array_sum(explode(' ', microtime()));
}
function current_user ()
{
//FIXME: figure out how to do this in windows too
return `whoami`;
}
// set things right as far as the path is concerned
function check_dir ($dir)
{
global $PATHS;
$err = FALSE;
if (!file_exists($dir)){
if (!@mkdir($dir, 0755))
{
$user = current_user();
$err =
"could not create sub-directory '$dir'. do either of the
following:
- give user '$user' ownership of the directory containing
this file (chown -R $user " . getcwd() . ")
- create directory '$dir' manually and give user '$user'
ownership of it (mkdir $dir; chown -R $user $dir)
If you do not have access to the chown command then you
need to create the '$dir' directory manually and then make it
world-writeable (chmod -R 0777 $dir). This is not a real
great solution, but it does work.";
}
}
else if (!is_dir($dir))
{
$err = "file '$dir' exists, but is not a directory. please " .
"change the \$PATHS[\"images\"] setting";
}
else if (!is_writeable($dir))
{
$err = "'$dir' exists and is a directory, but i cannot write " .
"to it. make it writeable for user '" .
current_user() . "'";
}
return $err;
}
////////////////////////////////////////////// thumbnail / image functions
function thumb_exists ($img)
{
return file_exists(thumb_path($img));
}
/////////////////////////// functions relating to checking for requirements
function ensure_version ()
{
$v = explode('.', phpversion());
if ($v[0] < 4 || ($v[0] == 4 && $v[1] < 2))
{
print_error(
"Sorry, this script requires PHP 4.2 or higher, you're running " .
phpversion() . ". Download the latest and greatest from
php.net"
);
die("