Skip to content

Instantly share code, notes, and snippets.

@matej21
Created October 21, 2013 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matej21/7088539 to your computer and use it in GitHub Desktop.
Save matej21/7088539 to your computer and use it in GitHub Desktop.
<?php
class ImageResponse extends \Nette\Object implements \Nette\Application\IResponse
{
/** @var \Nette\Image|string */
private $image;
/**
* @param \Nette\Image|string
*/
public function __construct($image)
{
if (!$image instanceof \Nette\Image && !file_exists($image)) {
throw new \Nette\InvalidArgumentException('Image must be Nette\Image or file path');
}
$this->image = $image;
}
/**
* @param \Nette\Http\IRequest
* @param \Nette\Http\IResponse
*/
public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse)
{
if ($this->image instanceof \Nette\Image) {
$this->image->send();
return;
}
$httpResponse->setContentType(\Nette\Utils\MimeTypeDetector::fromFile($this->image));
$length = filesize($this->image);
$handle = fopen($this->image, 'r');
$httpResponse->setHeader('Content-Length', $length);
while (!feof($handle)) {
echo fread($handle, 4e6);
}
fclose($handle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment