Image response - generování obrázků
<?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);
}
}