diff options
author | Frédéric Guillot <fred@kanboard.net> | 2017-12-15 13:27:25 -0800 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2017-12-15 13:27:25 -0800 |
commit | 8e6476b4028e44687e10bf7fb2617a2d2fff97d2 (patch) | |
tree | f010db02cb0ff016bdc806a0a87e1a4b21946f0a /vendor/gregwar/captcha/src/Gregwar/Captcha/ImageFileHandler.php | |
parent | a93b8e10f5954be0853eec693c13e84c4bd9e6f2 (diff) |
Update composer dependencies
Diffstat (limited to 'vendor/gregwar/captcha/src/Gregwar/Captcha/ImageFileHandler.php')
-rw-r--r-- | vendor/gregwar/captcha/src/Gregwar/Captcha/ImageFileHandler.php | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/vendor/gregwar/captcha/src/Gregwar/Captcha/ImageFileHandler.php b/vendor/gregwar/captcha/src/Gregwar/Captcha/ImageFileHandler.php new file mode 100644 index 00000000..dd20fcb2 --- /dev/null +++ b/vendor/gregwar/captcha/src/Gregwar/Captcha/ImageFileHandler.php @@ -0,0 +1,105 @@ +<?php + +namespace Gregwar\Captcha; + +use Symfony\Component\Finder\Finder; + +/** + * Handles actions related to captcha image files including saving and garbage collection + * + * @author Gregwar <g.passault@gmail.com> + * @author Jeremy Livingston <jeremy@quizzle.com> + */ +class ImageFileHandler +{ + /** + * Name of folder for captcha images + * @var string + */ + protected $imageFolder; + + /** + * Absolute path to public web folder + * @var string + */ + protected $webPath; + + /** + * Frequency of garbage collection in fractions of 1 + * @var int + */ + protected $gcFreq; + + /** + * Maximum age of images in minutes + * @var int + */ + protected $expiration; + + /** + * @param $imageFolder + * @param $webPath + * @param $gcFreq + * @param $expiration + */ + public function __construct($imageFolder, $webPath, $gcFreq, $expiration) + { + $this->imageFolder = $imageFolder; + $this->webPath = $webPath; + $this->gcFreq = $gcFreq; + $this->expiration = $expiration; + } + + /** + * Saves the provided image content as a file + * + * @param string $contents + * + * @return string + */ + public function saveAsFile($contents) + { + $this->createFolderIfMissing(); + + $filename = md5(uniqid()) . '.jpg'; + $filePath = $this->webPath . '/' . $this->imageFolder . '/' . $filename; + imagejpeg($contents, $filePath, 15); + + return '/' . $this->imageFolder . '/' . $filename; + } + + /** + * Randomly runs garbage collection on the image directory + * + * @return bool + */ + public function collectGarbage() + { + if (!mt_rand(1, $this->gcFreq) == 1) { + return false; + } + + $this->createFolderIfMissing(); + + $finder = new Finder(); + $criteria = sprintf('<= now - %s minutes', $this->expiration); + $finder->in($this->webPath . '/' . $this->imageFolder) + ->date($criteria); + + foreach ($finder->files() as $file) { + unlink($file->getPathname()); + } + + return true; + } + + /** + * Creates the folder if it doesn't exist + */ + protected function createFolderIfMissing() + { + if (!file_exists($this->webPath . '/' . $this->imageFolder)) { + mkdir($this->webPath . '/' . $this->imageFolder, 0755); + } + } +} |