public static function optimise($all = false)
 {
     /* clear cache if all parameter is true */
     if ($all) {
         self::clearOptimisedImages();
     }
     /* get all files */
     $all_images = self::getAllImages();
     $all_files = $all_images['files'];
     $total_size = $all_images['total'];
     /* get already optimised files */
     $optimised_images = self::getOptimisedImages();
     $optimised_files = $optimised_images['files'];
     $current_size = $optimised_images['total'];
     $optimised = count($optimised_files);
     /* exclude already optimised images */
     foreach ($all_files as $key => $file) {
         if (in_array($file['name'], $optimised_files)) {
             unset($all_files[$key]);
         }
     }
     /* make array chunks */
     $all_files = array_chunk($all_files, defined('JOURNAL_SMUSH_BATCH_SIZE') ? JOURNAL_SMUSH_BATCH_SIZE : 1);
     /* optimise process */
     foreach ($all_files as $files) {
         try {
             $start = microtime(true);
             $batch = array();
             $batch_size = 0;
             foreach ($files as $file) {
                 $batch[] = $file['name'];
                 $batch_size += $file['size'];
             }
             $current_size += $batch_size;
             $percent = self::percent($current_size, $total_size);
             $smushit = new SmushIt($batch);
             foreach ($smushit->get() as $file) {
                 $file = $file[0];
                 $src = pathinfo($file->source, PATHINFO_EXTENSION);
                 $dst = pathinfo($file->destination, PATHINFO_EXTENSION);
                 if ($src === $dst) {
                     copy($file->destination, $file->source);
                 }
             }
             self::setOptimisedImages($batch);
             $optimised++;
             $stop = microtime(true);
             self::send(array('percent' => $percent, 'time' => $stop - $start, 'all' => $all, 'optimised' => $optimised, 'total' => count($all_images['files'])));
         } catch (Exception $e) {
             self::send(array('error' => $e->getMessage()));
         }
     }
 }
Example #2
0
 /**
  * Smush.it constructor
  * @access public
  * @param  array | string           $sources List of files to compress
  * @param  int                      $flags   List of flags
  * @return object
  * @see    SmushIt::KEEP_ERRORS
  * @see    SmushIt::THROW_EXCEPTION
  */
 public function __construct($sources, $flags = null)
 {
     $this->flags = $flags;
     //$sources = $this->clean($sources);
     if (is_string($sources)) {
         if ($this->check($sources)) {
             $this->smush();
         }
     } else {
         foreach ($sources as $source) {
             $smush = new SmushIt($source, $flags);
             $smushResult = $smush->get();
             if (!empty($smushResult)) {
                 $this->items[] = $smushResult;
             }
         }
     }
 }
Example #3
0
 /**
  * @@todo: avoid processing same image twice?
  */
 public static function smushItDirectory($path, $recursive, $replace, $fix)
 {
     if (empty($path) || !$replace && empty($fix)) {
         return false;
     }
     require_once dirname(__FILE__) . '/class.smushit.php';
     $files = JFolder::files(JPATH_SITE . '/' . $path, '\\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$', $recursive, true);
     $smush = new SmushIt(true, $replace, $fix, JPATH_SITE . '/' . $path);
     $result = array('success' => 0, 'fail' => 0);
     foreach ($files as $file) {
         $smush->smushFile($file);
     }
     return JText::sprintf('PLG_JBETOLO_SMUSHIT_SUCCESS', $smush->count, ($smush->size - $smush->compressedSize) / 1000);
 }
Example #4
0
 public function smushCachedImages()
 {
     $this->closeSession();
     require_once DIR_APPLICATION . '../assets/smushit.php';
     $cacheImagesDir = DIR_IMAGE . 'cache';
     $images = $this->onlyImages($this->directoryToArray($cacheImagesDir, true));
     $total_images = count($images);
     $smushedNumber = 0;
     $files = array_chunk($images, 3);
     //$file = DIR_SYSTEM . 'nitro/data/smush_refresh.cache';
     //file_put_contents($file, '');
     $this->openSession();
     $_SESSION['smush_progress'] = array('smushed_images_count' => 0, 'total_images' => $total_images, 'kb_saved' => 0, 'last_smush_timestamp' => 0, 'smushed_files' => array(), 'messages' => array());
     $this->setSmushitPersistence($_SESSION['smush_progress']);
     $this->closeSession();
     // Take a batch of three files
     foreach ($files as $batch) {
         try {
             // Compress the batch
             //$this->setSmushProgress($smushedNumber, 0, time(), $file);
             $smushit = new SmushIt($batch, SmushIt::LOCAL_ORIGIN);
             set_time_limit(30);
             // And finaly, replace original files by their compressed version
             foreach ($smushit->get() as $k => $file) {
                 if (!$this->smushCanContinue()) {
                     return $smushedNumber;
                 }
                 // Sometimes, Smush.it convert files. We don't want that to happen.
                 $src = pathinfo($file[0]->source, PATHINFO_EXTENSION);
                 $dst = pathinfo($file[0]->destination, PATHINFO_EXTENSION);
                 if ($src == $dst and copy($file[0]->destination, $file[0]->source)) {
                     // Success !
                     //echo 'Smushed File: '.$source.'<br>';
                     $smushedNumber++;
                     $this->setSmushProgress($smushedNumber, ($file[0]->sourceSize - $file[0]->destinationSize) / 1024, time(), $file[0]->source, $file[0]->savings);
                 } else {
                     $this->setSmushProgressMessage('Skip: SmushIt converted from  ' . $src . ' to ' . $dst);
                 }
             }
         } catch (Exception $e) {
             $this->setSmushProgressMessage($e->getMessage());
             //$this->log->write($e->getMessage());
             continue;
         }
     }
     return $smushedNumber;
 }