-
-
Save Kcko/66af69ca853fff9a435f to your computer and use it in GitHub Desktop.
Image response - generování obrázků
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 | |
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