Font
3 years ago
CaptchaBuilder.php
8 months ago
CaptchaBuilderInterface.php
4 years ago
ImageFileHandler.php
4 years ago
PhraseBuilder.php
4 years ago
PhraseBuilderInterface.php
4 years ago
index.php
3 years ago
ImageFileHandler.php
47 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Gregwar\Captcha; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Finder\Finder; |
| 5 | class ImageFileHandler |
| 6 | { |
| 7 | protected $imageFolder; |
| 8 | protected $webPath; |
| 9 | protected $gcFreq; |
| 10 | protected $expiration; |
| 11 | public function __construct($imageFolder, $webPath, $gcFreq, $expiration) |
| 12 | { |
| 13 | $this->imageFolder = $imageFolder; |
| 14 | $this->webPath = $webPath; |
| 15 | $this->gcFreq = $gcFreq; |
| 16 | $this->expiration = $expiration; |
| 17 | } |
| 18 | public function saveAsFile($contents) |
| 19 | { |
| 20 | $this->createFolderIfMissing(); |
| 21 | $filename = \md5(\uniqid()) . '.jpg'; |
| 22 | $filePath = $this->webPath . '/' . $this->imageFolder . '/' . $filename; |
| 23 | \imagejpeg($contents, $filePath, 15); |
| 24 | return '/' . $this->imageFolder . '/' . $filename; |
| 25 | } |
| 26 | public function collectGarbage() |
| 27 | { |
| 28 | if (!\mt_rand(1, $this->gcFreq) == 1) { |
| 29 | return \false; |
| 30 | } |
| 31 | $this->createFolderIfMissing(); |
| 32 | $finder = new Finder(); |
| 33 | $criteria = \sprintf('<= now - %s minutes', $this->expiration); |
| 34 | $finder->in($this->webPath . '/' . $this->imageFolder)->date($criteria); |
| 35 | foreach ($finder->files() as $file) { |
| 36 | \unlink($file->getPathname()); |
| 37 | } |
| 38 | return \true; |
| 39 | } |
| 40 | protected function createFolderIfMissing() |
| 41 | { |
| 42 | if (!\file_exists($this->webPath . '/' . $this->imageFolder)) { |
| 43 | \mkdir($this->webPath . '/' . $this->imageFolder, 0755); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 |