コード例 #1
0
<?php

if (isNitroEnabled() && (getNitroPersistence('Smush.Method') && getNitroPersistence('Smush.Method') == 'local') && getNitroPersistence('Smush.OnDemand')) {
    if (!empty($new_image) && file_exists(DIR_IMAGE . $new_image)) {
        loadNitroLib('NitroSmush/NitroSmush');
        $smusher = new NitroSmush();
        global $registry;
        $log = $registry->get('log');
        try {
            $filename = DIR_IMAGE . $new_image;
            $res = $smusher->smush($filename);
            if (!empty($res['errors']) && NITRO_DEBUG_MODE == 1) {
                $log->write("[NitroSmush: {$filename}] => " . var_export($res, true));
            }
        } catch (Exception $e) {
            set_time_limit(30);
        }
    }
}
コード例 #2
0
ファイル: nitro.php プロジェクト: artlabsdesign/missbloom
 private function smush($resume = false, $method = 'local')
 {
     session_write_close();
     loadNitroLib('iprogress');
     loadNitroLib('NitroSmush/NitroSmush');
     $method = in_array($method, array('local', 'remote')) ? $method : 'local';
     $progress = new iProgress('smush_web', 200);
     $smusher = new NitroSmush();
     $smusher->setTempDir(NITRO_FOLDER . 'temp');
     $list_filename = NITRO_DATA_FOLDER . 'smush_files.flist';
     $fh = fopen($list_filename, 'r');
     if ($resume) {
         fseek($fh, $progress->getData('last_pointer_position'), SEEK_SET);
         $progress->resume();
     }
     $data_sizes = array('KiloBytes' => 1024, 'MegaBytes' => 1024 * 1024);
     $progress->setData('is_process_active', true);
     while ($this->smush_can_proceed($progress) && false !== ($filename = $this->smush_get_next_image($progress, $fh))) {
         set_time_limit(30);
         try {
             switch ($method) {
                 case 'local':
                     $res = $smusher->smush($filename);
                     break;
                 case 'remote':
                     $res = $smusher->smush($filename, false, true);
                     break;
             }
             if ($res['smushed']) {
                 $savings = $progress->getData('b_saved');
                 $savings += $res['savings_b'];
                 $progress->setData('b_saved', $savings);
                 $saved_size = $res['savings_b'] . ' bytes';
                 foreach ($data_sizes as $k => $v) {
                     if ($res['savings_b'] > $v) {
                         $saved_size = number_format($res['savings_b'] / $v, 2) . " " . $k;
                     }
                 }
                 $progress->addMsg($filename . " -> Saved " . $saved_size . " (<b>" . $res['savings_percent'] . "%</b>)");
             } else {
                 if (empty($res['errors'])) {
                     $already_smushed = $progress->getData('already_smushed');
                     $already_smushed++;
                     $progress->setData('already_smushed', $already_smushed);
                     $progress->addMsg($filename . " is already optimized");
                 } else {
                     foreach ($res['errors'] as $err) {
                         $progress->addMsg("<b>ERROR</b> [{$filename}] => {$err}");
                     }
                 }
             }
         } catch (Exception $e) {
             $progress->addMsg("<b>Error</b> [{$filename}] => " . $e->getMessage());
         }
         $progress->iterateWith(1);
     }
     $progress->setData('last_smush_timestamp', time());
     $progress->setData('is_process_active', false);
     exit;
 }