public static function close()
 {
     if (self::getConfig('optimize', 'classes_combine')) {
         Lms_NameScheme_Autoload::compileTo(APP_ROOT . '/includes/All.php');
     }
     foreach (self::$_config['databases'] as $dbAlias => $dbConfig) {
         if (Lms_Db::isInstanciated($dbAlias)) {
             $db = Lms_Db::get($dbAlias);
             $sqlStatistics = $db->getStatistics();
             $time = round(1000 * $sqlStatistics['time']);
             $count = $sqlStatistics['count'];
             Lms_Debug::debug("Database {$dbAlias} time: {$time} ms ({$count} queries)");
         }
     }
     foreach (Lms_Timers::getTimers() as $name => $timer) {
         $time = round(1000 * $timer->getSumTime());
         Lms_Debug::debug('Profiling "' . $name . '": ' . $time . ' ms (' . $timer->getCount() . ')');
     }
     Lms_Debug::debug('Used memory: ' . round(memory_get_usage() / 1024) . ' KB');
     self::$_mainTimer->stop();
     $time = round(1000 * self::$_mainTimer->getSumTime());
     Lms_Debug::debug("Execution time: {$time} ms");
 }
 static function thumbnail($imgPath, &$width = 0, &$height = 0, $tolerance = 0.0, $zoom = true, $force = false, $deferDownload = false, $deferResize = false)
 {
     Lms_Timers::start('thumbnail');
     if (self::$cache) {
         $key = md5(serialize(func_get_args()));
         if ($data = self::$cache->load($key)) {
             Lms_Timers::stop('thumbnail');
             extract($data);
             return $url;
         }
     }
     Lms_Timers::start('thumbnail1');
     if (preg_match('{^https?://}i', $imgPath)) {
         $path = null;
         $hash = md5($imgPath);
         if (!$path) {
             $fileDirPath = self::$imageDir . implode("/", str_split(substr($hash, 0, 2))) . "/" . $hash;
             if (is_dir($fileDirPath)) {
                 $folder = Lms_FileSystem::getFolder($fileDirPath);
                 $files = $folder->getFiles();
                 if ($files->getCount()) {
                     $path = $files->getFirst()->getPath();
                 }
             }
             if (!$path) {
                 if ($deferDownload) {
                     Lms_Timers::stop('thumbnail');
                     return self::getDeferUrl(func_get_args());
                 }
                 $tempPath = $fileDirPath . '/tmp';
                 self::downloadImage($imgPath, $tempPath);
                 $imageFormat = self::getFormat($tempPath);
                 $path = "{$fileDirPath}/image.{$imageFormat}";
                 Lms_Ufs::rename($tempPath, $path);
             }
         }
     } else {
         $path = str_replace('\\', '/', $imgPath);
     }
     $imageFormat = strtolower(pathinfo($path, PATHINFO_EXTENSION));
     Lms_Timers::stop('thumbnail1');
     Lms_Timers::start('getimagesize');
     $imageSize = getimagesize($path);
     $imageX = $imageSize[0];
     $imageY = $imageSize[1];
     $imageType = $imageSize[2];
     $imageFormat = self::getFormatByType($imageType);
     Lms_Timers::stop('getimagesize');
     Lms_Timers::start('thumbnail2');
     $image = array();
     $image['x'] = $imageX;
     $image['y'] = $imageY;
     // check resize
     $k = $height ? ($height - $imageY) / $height : 0;
     if (!$zoom && $k > 0) {
         $k = 0;
     }
     if ($height && abs($k) > $tolerance) {
         $image['y'] = $height;
         $image['x'] = round($image['x'] / (1 - $k));
     }
     $k = $width ? ($width - $image['x']) / $width : 0;
     if (!$zoom && $k > 0) {
         $k = 0;
     }
     if ($width && abs($k) > $tolerance) {
         $image['x'] = $width;
         $image['y'] = round($image['y'] / (1 - $k));
     }
     $width = $image['x'];
     $height = $image['y'];
     $prefix = md5($imgPath);
     $prefix = implode("/", str_split(substr($prefix, 0, 2))) . "/" . $prefix;
     //$cachepath = LMS_PUBLIC_MEDIA_DIR . 'cache/';
     $cachepath = self::$thumbnailDir;
     if (self::$gdEnabled === null) {
         self::$gdEnabled = function_exists('imagecreatefromjpeg');
     }
     if (($image['x'] != $imageX || $image['y'] != $imageY || $force) && self::$gdEnabled) {
         $filepath = $cachepath . $prefix . "_" . $image['x'] . "x" . $image['y'] . "." . $imageFormat;
         Lms_Timers::start('is_file');
         $isFile = is_file($filepath);
         Lms_Timers::stop('is_file');
         if (!$isFile) {
             if ($deferResize) {
                 Lms_Timers::stop('thumbnail');
                 return self::getDeferUrl(func_get_args());
             }
             $obj = new Lms_ImageProcessor();
             try {
                 $obj->loadfile($path);
                 $obj->resize($image['x'], $image['y']);
                 Lms_FileSystem::createFolder(dirname($filepath), 0777, true);
                 $obj->savefile($filepath);
             } catch (Exception $e) {
                 //$filepath = LMS_PUBLIC_COMMON_MEDIA_DIR . 'error.jpg';
                 $filepath = self::$errorImagePath;
             }
         }
         $url = $filepath;
     } else {
         $url = $path;
     }
     Lms_Timers::stop('thumbnail2');
     $url = str_replace('\\', '/', $url);
     $url = str_replace($_SERVER['DOCUMENT_ROOT'], '', $url);
     Lms_Timers::stop('thumbnail');
     if (self::$cache) {
         $data = array('url' => $url, 'width' => $width, 'height' => $height);
         self::$cache->save($data);
     }
     return $url;
 }