Ejemplo n.º 1
0
 /**
  * Test static methods
  */
 public function testStatic()
 {
     // JPG is just an alias
     $this->assertEquals(Image::TYPE_JPEG, Image::TYPE_JPG);
     $this->assertEquals('image/gif', Image::typeToMimeType(Image::TYPE_GIF));
     $this->assertEquals('image/png', Image::typeToMimeType(Image::TYPE_PNG));
     $this->assertEquals('image/jpeg', Image::typeToMimeType(Image::TYPE_JPEG));
     $this->assertEquals('.gif', Image::typeToExtension(Image::TYPE_GIF));
     $this->assertEquals('.png', Image::typeToExtension(Image::TYPE_PNG));
     $this->assertEquals('.jpeg', Image::typeToExtension(Image::TYPE_JPEG));
     $this->assertEquals('gif', Image::typeToExtension(Image::TYPE_GIF, false));
     $this->assertEquals('png', Image::typeToExtension(Image::TYPE_PNG, false));
     $this->assertEquals('jpeg', Image::typeToExtension(Image::TYPE_JPEG, false));
     $this->assertTrue(Image::isResize(Image::RESIZE_DEFAULT));
     $this->assertTrue(Image::isResize(Image::RESIZE_CUT));
     $this->assertTrue(Image::isResize(Image::RESIZE_FIT));
     $this->assertTrue(Image::isResize(Image::RESIZE_FRAME));
     $this->assertTrue(Image::isResize(Image::RESIZE_STRETCH));
     $this->assertTrue(Image::isFilter(Image::FILTER_BRIGHTNESS));
     $this->assertTrue(Image::isFilter(Image::FILTER_COLORIZE));
     $this->assertTrue(Image::isFilter(Image::FILTER_COLORIZE_BLUE));
     $this->assertTrue(Image::isFilter(Image::FILTER_COLORIZE_GREEN));
     $this->assertTrue(Image::isFilter(Image::FILTER_COLORIZE_RED));
     $this->assertTrue(Image::isFilter(Image::FILTER_CONTRAST));
     $this->assertTrue(Image::isFilter(Image::FILTER_EDGEDETECT));
     $this->assertTrue(Image::isFilter(Image::FILTER_EMBOSS));
     $this->assertTrue(Image::isFilter(Image::FILTER_GAUSSIAN_BLUR));
     $this->assertTrue(Image::isFilter(Image::FILTER_GRAYSCALE));
     $this->assertTrue(Image::isFilter(Image::FILTER_MEAN_REMOVAL));
     $this->assertTrue(Image::isFilter(Image::FILTER_NEGATE));
     $this->assertTrue(Image::isFilter(Image::FILTER_PIXELATE));
     $this->assertTrue(Image::isFilter(Image::FILTER_REMOVE_MEAN));
     $this->assertTrue(Image::isFilter(Image::FILTER_SELECTIVE_BLUR));
     $this->assertTrue(Image::isFilter(Image::FILTER_SMOOTH));
 }
Ejemplo n.º 2
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;
 }