示例#1
0
 /**
  * 
  * @return type
  * @throws type
  */
 public static function optimizeImages()
 {
     error_reporting(0);
     $root = JchPlatformPaths::rootPath();
     set_time_limit(0);
     $dir_array = JchPlatformUtility::get('dir', '', 'array');
     $subdirs = JchPlatformUtility::get('subdirs', '', 'array');
     $params = (object) JchPlatformUtility::get('params', '', 'array');
     $task = JchPlatformUtility::get('task', '0', 'string');
     $dir = rtrim(JchPlatformUtility::decrypt($dir_array['path']), '/\\');
     if ($task == 'getfiles') {
         $files = array();
         if (count(array_filter($subdirs))) {
             foreach ($subdirs as $subdir) {
                 $subdir = rtrim(JchPlatformUtility::decrypt($subdir), '/\\');
                 $files = array_merge($files, self::getImageFiles($root . $subdir, TRUE));
             }
         }
         if (!empty($files)) {
             $files = array_map(function ($v) {
                 return JchOptimizeHelper::prepareImageUrl($v);
             }, $files);
         }
         $data = array('files' => $files, 'log_path' => JchPlatformUtility::getLogsPath());
         return new JchOptimizeJson($data);
     }
     $file = $dir;
     $data = array();
     $oJchio = new JchOptimize\ImageOptimizer($params->pro_downloadid, $params->hidden_api_secret);
     $options = array("file" => $file, "lossy" => true);
     if (!empty($dir_array['width']) || !empty($dir_array['height'])) {
         $options['resize']['width'] = (int) (!empty($dir_array['width']) ? $dir_array['width'] : 0);
         $options['resize']['height'] = (int) (!empty($dir_array['height']) ? $dir_array['height'] : 0);
     }
     if ($params->kraken_backup || !empty($options['resize'])) {
         $backup_file = self::getBackupFilename($file);
         self::copy($file, $backup_file);
     }
     $message = '';
     $file = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $file);
     try {
         $response = $oJchio->upload($options);
         if (isset($response->success)) {
             if ($response->success) {
                 if (self::copy($response->data->kraked_url, $file)) {
                     $message .= 'Optimized! You saved ' . $response->data->saved_bytes . ' bytes.';
                 } else {
                     $data = new Exception('Could not copy optimized file.', 404);
                 }
             } else {
                 $data = new Exception($response->message, $response->code);
             }
         } else {
             JchOptimizeLogger::logInfo($response, 'Server error');
             $data = new Exception('Unrecognizable response from server', 500);
         }
     } catch (Exception $e) {
         $data = $e;
     }
     $respond = new JchOptimizeJson($data, $message);
     if ($respond->success || $respond->code == 404) {
         $respond->message = $file . ': ' . $respond->message;
     }
     try {
         JchOptimizeLogger::logInfo($respond->message, 'INFO');
     } catch (Exception $e) {
     }
     return $respond;
 }