Ejemplo n.º 1
0
 /**
  * Thumbnail renderer action
  *
  * @return array
  */
 public function renderAction()
 {
     $redir = false;
     $params = $this->params();
     $pathname = $params->fromRoute('pathname');
     $matches = array();
     $hasMatch = preg_match('#^(.+)/([^/]+)/([0-9x\\-]+)/([^/]+)/' . '([^/]+)/([0-9]+)/([^/\\?]+)(\\?.*)?$#', $pathname, $matches);
     if (!$hasMatch) {
         return $this->error(400);
     }
     list(, $this->path, $this->method, $sizes, $this->bgColor, $filters, $mtime, $this->file) = $matches;
     $sizes = explode('-', $sizes, 3);
     if (count($sizes) == 3) {
         $this->crop = true;
         list($this->width, $this->height) = explode('x', $sizes[2], 2);
         list($this->cropLeft, $this->cropTop) = explode('x', $sizes[0], 2);
         list($this->cropWidth, $this->cropHeight) = explode('x', $sizes[1], 2);
     } else {
         $this->crop = false;
         list($this->width, $this->height) = explode('x', $sizes[0], 2);
     }
     $mtime = (int) $mtime;
     $this->width = (int) $this->width;
     $this->height = (int) $this->height;
     $inputPath = Thumbnail::THUMBNAIL_BASEPATH . '/' . $this->filePath();
     if (0 == $this->width && self::MIN_HEIGHT <= $this->height && is_file($inputPath) && filesize($inputPath) > 0) {
         $inputSizes = getimagesize($inputPath);
         if (!empty($inputSizes[0]) && !empty($inputSizes[1])) {
             $this->width = (int) ($inputSizes[0] * $this->height / $inputSizes[1]);
             $redir = true;
         }
     }
     if (0 == $this->height && self::MIN_WIDTH <= $this->width && is_file($inputPath) && filesize($inputPath) > 0) {
         $inputSizes = getimagesize($inputPath);
         if (!empty($inputSizes[0]) && !empty($inputSizes[1])) {
             $this->height = (int) ($inputSizes[1] * $this->width / $inputSizes[0]);
             $redir = true;
         }
     }
     if (self::MIN_WIDTH > $this->width) {
         $this->width = self::DEFAULT_WIDTH;
         $redir = true;
     }
     if (self::MIN_HEIGHT > $this->height) {
         $this->height = self::DEFAULT_HEIGHT;
         $redir = true;
     }
     if (!is_file($inputPath)) {
         return $this->error(404);
     }
     if (1 > filesize($inputPath)) {
         return $this->error(415);
     }
     if (preg_match('#^image/(x-|vnd.microsoft.)?icon?$#', $this->getMime($inputPath))) {
         return $this->redirect()->toUrl('/uploads/' . $this->filePath());
     }
     if ($filters == 'none') {
         $this->filters = array();
     } else {
         $this->filters = explode('-', $filters);
     }
     if (!Image::isResize($this->method)) {
         $this->method = self::DEFAULT_METHOD;
         $redir = true;
     }
     foreach ($this->filters as $index => $filter) {
         $filter = explode(',', $filter);
         if (!Image::isFilter($filter[0])) {
             unset($this->filters[$index]);
         }
     }
     if (empty($this->filters)) {
         $newFilters = 'none';
     } else {
         $newFilters = implode('-', $this->filters);
     }
     if ($filters != $newFilters) {
         $redir = true;
     }
     $fmt = filemtime($inputPath);
     if ($fmt != $mtime) {
         $mtime = $fmt;
         $redir = true;
     }
     if ($redir) {
         return $this->redirect()->toUrl($this->outputUri($mtime));
     }
     if (!$this->getImage($inputPath)) {
         return $this->error(415);
     }
     if ($this->crop) {
         $this->getImage()->cropTo($this->cropLeft, $this->cropTop, $this->cropWidth, $this->cropHeight);
     }
     $this->getImage()->resize($this->width, $this->height, $this->method, $this->bgColor);
     foreach ($this->filters as $filter) {
         $filter = explode(',', $filter);
         $name = array_shift($filter);
         $this->getImage()->filter($name, $filter);
     }
     $outputPath = $this->outputPath($mtime);
     if (!is_dir(dirname($outputPath))) {
         mkdir(dirname($outputPath), 0777, true);
     }
     $this->getImage()->render($outputPath);
     $response = Readfile::fromFile($outputPath, Image::typeToMimeType($this->getImage()->getType()));
     $this->getEvent()->setResponse($response);
     return $response;
 }
Ejemplo n.º 2
0
 /**
  * Test render with unknown type
  *
  * @expectedException   Zork\Image\Exception\ExceptionInterface
  */
 public function testRenderWithUnknownType()
 {
     $image = Image::create(100, 100);
     $image->render('/tmp/~' . uniqid(), -1);
 }