/** * @param string $pathAndFilename * @return void * @throws \Exception */ public function optimizeFile($pathAndFilename) { try { $imageType = exif_imagetype($pathAndFilename); $fileExtension = strtolower(pathinfo($pathAndFilename, PATHINFO_EXTENSION)); if ($imageType !== FALSE && !in_array($imageType, [\IMAGETYPE_JPEG, \IMAGETYPE_PNG, \IMAGETYPE_GIF], TRUE) || $imageType === FALSE && $fileExtension !== 'svg') { return; } $pathAndFilename = realpath($pathAndFilename); $cacheIdentifier = md5($pathAndFilename); $lastModificationTime = \DateTime::createFromFormat('U', filemtime($pathAndFilename)); $cachedLastModificationTime = $this->processingCache->get($cacheIdentifier); if ($cachedLastModificationTime instanceof \DateTime && $lastModificationTime->getTimestamp() === $lastModificationTime->getTimestamp()) { return; } $useGlobalBinary = $this->settings['useGlobalBinary']; $binaryRootPath = 'Private/Library/node_modules/'; $file = escapeshellarg($pathAndFilename); if ($imageType !== FALSE) { switch ($imageType) { case \IMAGETYPE_JPEG: if ($this->settings['formats']['jpg']['enabled'] === FALSE) { return; } $library = 'jpegtran'; $binaryPath = sprintf('%1$s-bin/vendor/%s', $library); $arguments = sprintf('-copy none -optimize %s -outfile %s %s', $this->settings['formats']['jpg']['progressive'] === TRUE ? '-progressive' : '', $file, $file); if ($this->settings['formats']['jpg']['useGlobalBinary'] === TRUE) { $useGlobalBinary = TRUE; } break; case \IMAGETYPE_PNG: if ($this->settings['formats']['png']['enabled'] === FALSE) { return; } $library = 'optipng'; $binaryPath = sprintf('%1$s-bin/vendor/%s', $library); $arguments = sprintf('-o%u -strip all -out %s %s', $this->settings['formats']['png']['optimizationLevel'], $file, $file); if ($this->settings['formats']['png']['useGlobalBinary'] === TRUE) { $useGlobalBinary = TRUE; } break; case \IMAGETYPE_GIF: if ($this->settings['formats']['gif']['enabled'] === FALSE) { return; } $library = 'gifsicle'; $binaryPath = sprintf('%1$s/vendor/%1$s', $library); $arguments = sprintf('--batch -O%u %s ', $this->settings['formats']['gif']['optimizationLevel'], $file); if ($this->settings['formats']['gif']['useGlobalBinary'] === TRUE) { $useGlobalBinary = TRUE; } break; } } else { if ($this->settings['formats']['svg']['enabled'] === FALSE) { return; } $library = 'svgo'; $binaryPath = sprintf('%1$s/bin/%1$s', $library); $arguments = sprintf('%s %s', $this->settings['formats']['svg']['pretty'] === TRUE ? '--pretty' : '', $file); if ($this->settings['formats']['svg']['useGlobalBinary'] === TRUE) { $useGlobalBinary = TRUE; } } $binaryPath = $useGlobalBinary === TRUE ? $this->settings['globalBinaryPath'] . $library : $this->packageManager->getPackageOfObject($this)->getResourcesPath() . $binaryRootPath . $binaryPath; $cmd = escapeshellcmd($binaryPath) . ' ' . $arguments; $output = []; exec($cmd, $output, $result); $this->systemLogger->log($cmd . ' (' . ((int) $result === 0 ? 'OK' : 'Error: ' . $result) . ')', LOG_INFO, $output); $lastModificationTime = \DateTime::createFromFormat('U', filemtime($pathAndFilename)); $this->processingCache->set($cacheIdentifier, $lastModificationTime); } catch (\Exception $exception) { $this->systemLogger->logException($exception); } }