Created
June 14, 2014 10:09
-
-
Save lmammino/4b7821f4d4f05845bfcd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sbaam\Bundle\InviteBundle\Http; | |
use Symfony\Component\HttpFoundation\Response; | |
/** | |
* Class BlankPixelResponse | |
* @package Sbaam\Bundle\InviteBundle\Http | |
* @author Luciano Mammino <[email protected]> | |
*/ | |
class BlankPixelResponse extends Response | |
{ | |
/** | |
* Base 64 encoded contents for 1px transparent gif and png | |
* | |
* @var array | |
*/ | |
protected static $IMAGE_CONTENT = array( | |
'gif' => 'R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==', | |
'png' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=' | |
); | |
/** | |
* Constructor | |
* | |
* @param string $type The format of the image. Accepts "gif" or "png" | |
* | |
* @throws \InvalidArgumentException if an invalid type is given | |
*/ | |
public function __construct($type = 'gif') | |
{ | |
if (!array_key_exists($type, self::$IMAGE_CONTENT)) { | |
throw new \InvalidArgumentException( | |
sprintf('Invalid image type "%s". Allowed image types: %s', $type, json_encode(array_keys(self::$IMAGE_CONTENT))) | |
); | |
} | |
$content = base64_decode(self::$IMAGE_CONTENT[$type]); | |
parent::__construct($content); | |
$this->headers->set('Content-Type', sprintf('image/%s', $type)); | |
$this->setPrivate(); | |
$this->headers->addCacheControlDirective('no-cache', true); | |
$this->headers->addCacheControlDirective('must-revalidate', true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment