/** * Execute the action */ public function execute() { parent::execute(); // Get api key $this->apiKey = BackendModel::getModuleSetting($this->getModule(), 'api_key', null); // Get uncompressed images list $this->images = BackendCompressionModel::getImagesFromFolders(); if (!empty($this->images)) { // Compress each image from each folder $output = 'Compressing ' . count($this->images) . ' images...' . "<br />\r\n"; BackendCompressionModel::writeToCacheFile($output, true); foreach ($this->images as $image) { $tinyPNGApi = new TinyPNGApi($this->apiKey); // Shrink the image and check if succesful if ($tinyPNGApi->shrink($image['full_path'])) { // Check if the file was successfully downloaded. if ($tinyPNGApi->download($image['full_path'])) { $output = 'Compression succesful for image ' . $image['filename'] . '. Saved ' . number_format($tinyPNGApi->getSavingSize() / 1024, 2) . ' KB' . ' bytes. (' . $tinyPNGApi->getSavingPercentage() . '%)'; BackendCompressionModel::writeToCacheFile($output); // Save to db $imageInfo = array('filename' => $image['filename'], 'path' => $image['full_path'], 'original_size' => $tinyPNGApi->getInputSize(), 'compressed_size' => $tinyPNGApi->getOutputSize(), 'saved_bytes' => $tinyPNGApi->getSavingSize(), 'saved_percentage' => $tinyPNGApi->getSavingPercentage(), 'checksum_hash' => sha1_file($image['full_path']), 'compressed_on' => BackendModel::getUTCDate()); BackendCompressionModel::insertImageHistory($imageInfo, $image['file_compressed_before']); } } else { BackendCompressionModel::writeToCacheFile($tinyPNGApi->getErrorMessage()); } } BackendCompressionModel::writeToCacheFile("...Done!"); } else { BackendCompressionModel::writeToCacheFile('There are no images that can be compressed.', true); } // Print the output for debug purposes $output = BackendCompressionModel::readCacheFile(); print $output; }
/** * Execute the action */ public function execute() { parent::execute(); $overwrite = (bool) \SpoonFilter::getPostValue('overwrite', null, ''); if ($overwrite) { BackendCompressionModel::writeToCacheFile(""); } $this->output(self::OK); }
/** * Parse into template */ private function parse() { // Fetch statistics $statistics = BackendCompressionModel::getStatistics(); // Format bytes $statistics['saved_bytes'] = $this->formatSizeUnits($statistics['saved_bytes']); // Compress data available? $statistics['statistics_enable'] = (int) $statistics['total_compressed'] == 0 ? false : true; // Parse into template $this->header->addCSS('Compression.css', 'Compression'); $this->tpl->assign('statistics', $statistics); }
/** * Validates the compression settings form. */ private function validateCompressionSettingsForm() { if ($this->frmCompressionSettings->isSubmitted()) { if ($this->frmCompressionSettings->isCorrect()) { $this->frmCompressionSettings->cleanupFields(); // validate fields if ($this->frmCompressionSettings->isCorrect()) { if (!empty($this->folders)) { // insert the folders BackendCompressionModel::insertFolders($this->folders); } } BackendModel::triggerEvent($this->getModule(), 'after_saved_settings'); $this->redirect(BackendModel::createURLForAction('Settings') . '&report=saved'); } } }