/** * * @return type * @throws type */ public static function optimizeImages(JchPlatformSettings $params) { $root = JchPlatformPaths::rootPath(); set_time_limit(0); $dir = JchPlatformUtility::get('dir', '', 'string'); $current = JchPlatformUtility::get('current', '0', 'int'); $optimize = JchPlatformUtility::get('optimize', '0', 'int'); $dir = JchPlatformUtility::decrypt($dir); $arr = array('total' => 0, 'current' => $current, 'optimize' => $optimize, 'message' => ''); $files = array(); if (is_dir($root . $dir)) { //$files = glob($root . $dir . '*.{gif,jpg,png}', GLOB_BRACE); if ($dh = opendir($root . $dir)) { while (($file = readdir($dh)) !== false) { if (preg_match('#\\.(?:gif|jpg|png)$#i', $file)) { $files[] = $root . $dir . $file; } } closedir($dh); } $arr['total'] = count($files); } else { $files = array($dir); } try { $smushitclass = 'JchOptimize\\SmushIt'; $smushit = new $smushitclass($files[$arr['current']], 0x2); $opfiles = $smushit->get(); if (copy($opfiles[0]->destination, $opfiles[0]->source)) { $arr['optimize']++; $arr['message'] = $opfiles[0]->source . ':Optimized!'; } else { throw Exception($opfiles[0]->source . ': Could not copy optimized image'); } } catch (Exception $e) { $arr['message'] = $e->getMessage(); } $arr['current']++; $arr['log_path'] = JchPlatformUtility::getLogsPath(); JchOptimizeLogger::logInfo($arr['message'], 'INFO'); return json_encode($arr); }
/** * * @param type $message * @param type $category */ public static function log($message, $priority, $filename) { $file = JchPlatformUtility::getLogsPath() . '/jch-optimize.log'; error_log($message . "\n", 3, $file); }
/** * * @return type * @throws type */ public static function optimizeImages(JchPlatformSettings $params) { error_reporting(0); $root = JchPlatformPaths::rootPath(); set_time_limit(0); $dir = JchPlatformUtility::get('dir', '', 'string'); $current = JchPlatformUtility::get('current', '0', 'int'); $optimize = JchPlatformUtility::get('optimize', '0', 'int'); $dir = JchPlatformUtility::decrypt($dir); $arr = array('total' => 0, 'current' => $current, 'optimize' => $optimize, 'message' => ''); $files = array(); if (is_dir($root . $dir)) { //$files = glob($root . $dir . '*.{gif,jpg,png}', GLOB_BRACE); if ($dh = opendir($root . $dir)) { while (($file = readdir($dh)) !== false) { if (preg_match('#\\.(?:gif|jpe?g|png)$#i', $file)) { $files[] = $root . $dir . $file; } } closedir($dh); } $arr['total'] = count($files); } else { $files = array($dir); } // try // { // $smushitclass = 'JchOptimize\SmushIt'; // $smushit = new $smushitclass($files[$arr['current']], 0x02); // // $opfiles = $smushit->get(); // // if (copy($opfiles[0]->destination, $opfiles[0]->source)) // { // $arr['optimize'] ++; // $arr['message'] = $opfiles[0]->source . ':Optimized!'; // } // else // { // throw new Exception($opfiles[0]->source . ': Could not copy optimized image'); // } // } // catch (Exception $e) // { // $arr['message'] = $e->getMessage(); // } $kraken = new JchOptimize\Kraken($params->get('kraken_api_key'), $params->get('kraken_api_secret')); $options = array("file" => $files[$arr['current']], "wait" => true, "lossy" => $params->get('kraken_optimization_level', 0) ? TRUE : FALSE); if ($params->get('kraken_quality', 0)) { $options['quality'] = (int) $params->get('kraken_quality'); } try { $data = $kraken->upload($options); } catch (Exception $ex) { $data = array('success' => FALSE, 'error' => $ex->getMessage()); } $file = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $files[$arr['current']]); if (isset($data['success'])) { if ($data['success']) { if ($data['saved_bytes'] == 0) { $arr['message'] = $file . ': This image can not be optimized any further.'; } elseif (self::copy($data['kraked_url'], $file)) { $arr['optimize']++; $arr['message'] = $file . ': Optimized! You saved ' . $data['saved_bytes'] . ' bytes.'; } else { $arr['message'] = $file . ': Could not copy optimized file.' . ' ' . $msg; } } else { $arr['message'] = $file . ': ' . $data['error']; } } else { $arr['message'] = $file . ': Unrecognizable response from server'; } $arr['current']++; $arr['log_path'] = JchPlatformUtility::getLogsPath(); try { JchOptimizeLogger::logInfo($arr['message'], 'INFO'); } catch (Exception $e) { } return json_encode($arr); }
/** * * @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; }