Opitimize a image.

@param $filepath
@throws \Exception
483286
public function optimizeImage($filepath)
    {
        $fileExtension = $this->extensions[mime_content_type($filepath)];

        $transformHandler = config('imageoptimizer.transform_handler');

        if (!isset($transformHandler[$fileExtension])) {
            throw new \Exception('TransformHandler for file extension: "' . $fileExtension . '" was not found');
        }

        $this->get($transformHandler[$fileExtension])->optimize($filepath);
    }
886575
public function locate($filePath)
    {
        $file = $filePath;
        if (!is_file($filePath))
        {
            throw new FileException(sprintf('Invalid file! File "%s" does not exist.',
                                            $filePath));
        }

        if (!is_readable($filePath))
        {
            throw new FileException(sprintf('Invalid file! File "%s" is not readable.',
                                            $filePath));
        }

        $pathInfo = pathinfo($filePath);
        if (!isset($pathInfo['extension'])
            || !in_array($pathInfo['extension'], $this->supportedExtensions)
        )
        {
            throw new FileException(sprintf('Invalid file extension! Supported file extensions: %s',
                implode(', ', $this->supportedExtensions)));
        }

        return realpath($file);
    }
1294367
private function preCompile($filePath)
    {
        if (!$this->parserManager->isAvailable()) {
            throw new IllegalStateException($this->parserManager->requirements());
        }

        if (preg_match('#\.zep$#', $filePath)) {
            $className = str_replace(\DIRECTORY_SEPARATOR, '\\', $filePath);
            $className = preg_replace('#.zep$#', '', $className);

            $className = implode('\\', array_map('ucfirst', explode('\\', $className)));

            $compilerFile = $this->compilerFileFactory->create($className, $filePath);
            $compilerFile->preCompile($this);

            $this->files[$className] = $compilerFile;
            $this->definitions[$className] = $compilerFile->getClassDefinition();
        }
    }
426204
public function loadFile($filePath)
    {
        if (strrpos($filePath, $this->extension) !== false) {
            $fileKey = str_replace([$this->path, $this->extension, DIRECTORY_SEPARATOR], ['', '', '-'], $filePath);
            $this->store(camelcase($fileKey), new SplFileInfo($filePath));
        }
    }
421736
public static function put(string $filepath, string $content):int
    {
        if (!file_exists($filepath)) {
            Server::create($filepath, "file");
        }
        file_put_contents($filepath, $content);
        self::open($filepath);
        return (1);
    }
305488
public function setFileMode($filePath = './cwsdebug.html', $fileClear = false)
    {
        if (empty($filePath)) {
            $this->error('You have to set the file path for debugging in file mode...', self::VERBOSE_QUIET);
            exit();
        }

        $this->filePath = $filePath;
        if (!file_exists($this->filePath)) {
            touch($this->filePath);
        }
        if ($fileClear) {
            unlink($this->filePath);
            touch($this->filePath);
        }

        $this->setMode(self::MODE_FILE);
    }
847131
protected function postCopy(
        string $filePath
    ): void {
        $this->fileCreationTransaction::setPathCreated($filePath);
        $this->findAndReplaceHelper->replaceName(
            $this->codeHelper->classy($this->className),
            $filePath,
            FieldGenerator::FIND_ENTITY_FIELD_NAME
        );
        $this->findAndReplaceHelper->findReplace(
            $this->codeHelper->consty(FieldGenerator::FIND_ENTITY_FIELD_NAME),
            $this->codeHelper->consty($this->className),
            $filePath
        );
        $this->codeHelper->tidyNamespacesInFile($filePath);
        $this->setGetterToIsForBools($filePath);
    }
855048
public function save($filePath, $size = null, $compression = -1, $filters = -1)
    {
        return imagepng($this->getImage($size), $filePath, $compression, $filters);
    }
851208
public function manipulateImage(ImageInterface $image, ImagePathInterface $path)
    {
        /**
         * @var ImageOptimizerInterface $optimizer
         */
        $optimizer = null;
        if (!empty($this->imageOptimizers[$image->getMimeType()])) {
            // Use the first matching optimizer.
            $optimizer = reset($this->imageOptimizers[$image->getMimeType()]);
        }
        if ($optimizer) {
            $optimizer->optimizeImage($image);
        }
    }
1296263
private function moveFileToExternalStorage($filePath,Folder $folder = null)
	{
		$oldPath = $this->getInternalPath() . $filePath;
		$newPath = $this->getExternalPath() . $filePath;

		$this->createBothFoldersInFileSystem($folder);

		if ( ! rename($oldPath, $newPath)) {
//			throw new Exception\RuntimeException('Failed to move file to the public storage');
			$filename = basename($newPath);
			$this->container->getLogger()->warn('Failed to move file (' . $filename . ') to the public storage');
		}
	}
756114
public function setRestorePath($filePath)
    {
        $path = realpath(dirname($filePath));
        if (!file_exists($path)) {
            throw new FilesystemException(sprintf(
                'The restore phar does not exist: %s.', $path
            ));
        }
        if (!is_readable($path)) {
            throw new FilesystemException(sprintf(
                'The restore file is not readable: %s.', $path
            ));
        }
        $this->restorePath = $filePath;
    }
1151899
private function replaceDummyInFile($filePath)
    {
        $fileSource = File::get($filePath);
        $fileContent = str_replace(array_keys($this->dummyReplacements), array_values($this->dummyReplacements), $fileSource);
        $writeFile = File::put($filePath, $fileContent);
        if ($writeFile === false) {
            throw new \Exception("Error writing to file " . $filePath);
        }
    }
350896
public function watermark(string $filePath)
    {
        if (! file_exists($filePath)) {
            throw new FileNotFoundException($filePath);
        }

        $this->addManipulation('watermark', $filePath);

        return $this;
    }
707948
public function saveToFile($filePath)
    {
        $handle = @fopen($filePath, 'w');
        if (false === $handle) {
            throw new Exception('Cannot open file "' . $filePath . '" for writing.');
        }

        $bytesWritten = @fwrite($handle, (string)$this);
        if (false === $bytesWritten) {
            fclose($handle);
            throw new Exception('Cannot write into file "' . $filePath . '".');
        }

        $closed = @fclose($handle);
        if (false === $closed) {
            throw new Exception('Cannot close file handle for "' . $filePath . '".');
        }
    }
22443
public function uploadAsset($filePath)
    {
        $params = array( 'file' => '@'.$filePath );
        $params = $this->validator->validate($params);
        $this->validator->validateAssetFile($params['file']);

        try {
            return $this->client->sendRequest('assets', $params, 'POST');
        } catch (\Exception $ex) {
            throw $ex;
        }
    }
341212
public function saveFile($filePath, $fileMode = 'w+')
    {
        $this->saveFileOption['filePath'] = $filePath;
        $this->saveFileOption['fileMode'] = $fileMode;
        return $this;
    }
1116195
static function changeFilePermissions( $filepath )
    {
        if ( !file_exists( $filepath ) )
            return false;
        $ini = eZINI::instance( 'image.ini' );
        $perm = $ini->variable( "FileSettings", "ImagePermissions" );
        $success = false;
        $oldmask = umask( 0 );
        if ( !chmod( $filepath, octdec( $perm ) ) )
            eZDebug::writeError( "Chmod $perm $filepath failed", __METHOD__ );
        else
            $success = true;
        umask( $oldmask );
        return $success;
    }