function getImageStamp($matches) { $requestedFormat = $_GET['format']; $file = base64_decode($matches[1]); $stampPath = PccConfig::getImageStampPath(); $filepath = $stampPath . $file; if (!file_exists($filepath)) { throw new Exception('Image not found.'); } $fileParts = explode('.', $file); $sourceImageFormat = strtolower($fileParts[1]); $acceptableFormats = explode(',', str_replace('.', '', PccConfig::getValidImageStampTypes())); if (!in_array($sourceImageFormat, $acceptableFormats)) { throw new Exception('Image format is not valid.'); } $lastModifiedTime = filemtime($filepath); if ($lastModifiedTime === false) { throw new Exception('Modify date unknown'); } if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { $ifModifiedSinceTime = strtotime(preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'])); if ($ifModifiedSinceTime >= $lastModifiedTime) { // Is the Cached version the most recent? header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified'); return; } } header('Last-Modified: ' . date('r', $lastModifiedTime)); header('Pragma: public'); header('Cache-Control: max-age=86400'); header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 86400)); if ($requestedFormat == 'Base64') { $outputFormat = $requestedFormat; } else { $outputFormat = $sourceImageFormat; } $ext = strtolower(array_pop(explode('.', $filepath))); $mime_types = array('png' => 'image/png', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif'); if (array_key_exists($ext, $mime_types)) { $imageType = $mime_types[$ext]; } else { throw new Exception('Image type not supported.'); } switch ($outputFormat) { case 'gif': case 'jpg': case 'jpeg': case 'png': header("Content-Type: {$imageType}"); echo file_get_contents($filepath); break; case 'Base64': header("Content-Type: application/json"); $base64Data = base64_encode(file_get_contents($filepath)); echo json_encode(array('dataHash' => sha1($base64Data), 'dataUrl' => 'data: ' . $imageType . ';base64,' . $base64Data)); break; } }