storage = $storage; $this->setup(); } protected function setup() { if (!is_dir($this->storage)) { mkdir($this->storage, 0777, true); if (!is_dir($this->storage)) { throw new Exception('Could not create storage path: ' . $this->storage); } } elseif (!is_writable($this->storage)) { throw new Exception($this->storage . ' is not writable'); } } protected function getCurrentFiles() { $storage = $this->storage; $allFiles = array_map( function ($file) use ($storage) { $file = str_ireplace('.png', '', $file); return substr($file, strlen($storage)); }, glob($this->storage . "*.png") ); rsort($allFiles); return $allFiles; } protected function getLastImage() { $allFiles = $this->getCurrentFiles(); return (count($allFiles) == 0) ? '' : $allFiles[0] . '.png'; } protected function getNextImage() { $allFiles = $this->getCurrentFiles(); $fileName = (count($allFiles) == 0) ? 1 : ++$allFiles[0]; return $fileName . '.png'; } protected function serveImage($filePath) { header("Content-Type: image/png"); header('Expires: 0'); header("Content-Length: " . filesize($filePath)); readfile($filePath); die(); } public function serveLastImage() { $image = $this->getLastImage(); $fullPath = $this->storage . $image; $this->serveImage($fullPath); } public function saveImage($data) { $saveAs = $this->getNextImage(); $data = trim(str_replace('data:image/png;base64,', '', $data)); file_put_contents($this->storage . $saveAs, base64_decode($data)); } } $imageStorage = new ImageStorage($storage); if (isset($_GET['image'])) { $imageStorage->serveLastImage(); die(); } $data = isset($_REQUEST[$paramName]) ? trim($_REQUEST[$paramName]) : ''; if (!empty($data)) { $imageStorage->saveImage($data); die(); } ?>