コード例 #1
0
 public function testShouldSaveWithPathAndCustomOption()
 {
     $image = $this->getImage();
     $path = '/path/to/dest';
     $filter = new WebOptimization($path, array('custom-option' => 'custom-value', 'resolution-y' => 100));
     $capturedOptions = null;
     $image->expects($this->once())->method('usePalette')->with($this->isInstanceOf('Imagine\\Image\\Palette\\RGB'))->will($this->returnValue($image));
     $image->expects($this->once())->method('strip')->will($this->returnValue($image));
     $image->expects($this->once())->method('save')->with($this->equalTo($path), $this->isType('array'))->will($this->returnCallback(function ($path, $options) use(&$capturedOptions, $image) {
         $capturedOptions = $options;
         return $image;
     }));
     $this->assertSame($image, $filter->apply($image));
     $this->assertCount(4, $capturedOptions);
     $this->assertEquals('custom-value', $capturedOptions['custom-option']);
     $this->assertEquals(ImageInterface::RESOLUTION_PIXELSPERINCH, $capturedOptions['resolution-units']);
     $this->assertEquals(72, $capturedOptions['resolution-x']);
     $this->assertEquals(100, $capturedOptions['resolution-y']);
 }
コード例 #2
0
ファイル: IndexController.php プロジェクト: skwear/depot
 protected function deliverImage($file)
 {
     // operation parsen uit url
     $operations = array();
     $params = $this->router->getParams();
     if (isset($params['operations'])) {
         $matches = preg_split('#-/#', $params['operations']);
         foreach ($matches as $match) {
             $options = explode('/', trim($match, '/'));
             $operations[array_shift($options)] = $options;
         }
     }
     if (!empty($params['ext'])) {
         if ($params['ext'] == 'json') {
             $operations['exif'] = true;
         } else {
             $operations['format'] = array($params['ext']);
         }
     }
     // afbeelding serveren
     if (empty($operations)) {
         $this->deliverOther($file);
     } else {
         // aan de hand van opgegeven operaties afbeelding manipulaties uitvoeren
         try {
             $imagine = new Imagine();
             $image = $imagine->open($file->getPath());
             // kwaliteit
             $quality = isset($operations['quality']) ? $operations['quality'] : 100;
             // formaat (mime type)
             $format = isset($operations['format'][0]) ? $operations['format'][0] : null;
             if ($format === null) {
                 switch ($file->mime_type) {
                     default:
                     case 'image/bmp':
                     case 'image/jpg':
                     case 'image/jpeg':
                     case 'image/tiff':
                         $format = 'jpg';
                         break;
                     case 'image/png':
                         $format = 'png';
                         break;
                     case 'image/ico':
                     case 'image/gif':
                         $format = 'gif';
                         break;
                 }
             }
             // schalen tot minimale afmetingen
             if (isset($operations['scale_crop'])) {
                 $value = $operations['scale_crop'][0];
                 if (strpos($value, 'x') !== false) {
                     list($width, $height) = explode('x', $value);
                 } else {
                     $width = $height = $value;
                 }
                 $size = $image->getSize();
                 if ($size->getHeight() > $size->getWidth()) {
                     $height = $size->getHeight() * ($width / $size->getWidth());
                 } else {
                     if ($size->getHeight() < $size->getWidth()) {
                         $width = $size->getWidth() * ($height / $size->getHeight());
                     }
                 }
                 $operations['resize'] = array("{$width}x{$height}");
                 $operations['crop'] = $operations['scale_crop'];
             }
             // afmetingen wijzigen
             if (isset($operations['resize'])) {
                 $value = $operations['resize'][0];
                 $width = $value;
                 $height = null;
                 if (strpos($value, 'x') !== false) {
                     list($width, $height) = explode('x', $value);
                 }
                 $size = $image->getSize();
                 if (empty($width) && !empty($height)) {
                     $size = $size->heighten($height);
                 } else {
                     if (!empty($width) && empty($height)) {
                         $size = $size->widen($width);
                     } else {
                         $size = new Box($width, $height);
                     }
                 }
                 $image->resize($size);
             }
             // bijsnijden
             if (isset($operations['crop'])) {
                 $size = $image->getSize();
                 $value = $operations['crop'][0];
                 if (strpos($value, 'x') !== false) {
                     list($width, $height) = explode('x', $value);
                 } else {
                     $width = $height = $value;
                 }
                 $size = $image->getSize();
                 if (empty($width) && !empty($height)) {
                     $size = $size->heighten($height);
                 } else {
                     if (!empty($width) && empty($height)) {
                         $size = $size->widen($width);
                     } else {
                         $size = new Box($width, $height);
                     }
                 }
                 if (empty($operations['crop'][1])) {
                     $operations['crop'][1] = 'center';
                 }
                 if ($operations['crop'][1] == 'center') {
                     $x = ($image->getSize()->getWidth() - $size->getWidth()) / 2;
                     $y = ($image->getSize()->getHeight() - $size->getHeight()) / 2;
                 } else {
                     list($x, $y) = explode(',', $operations['crop'][1]);
                 }
                 $point = new Point($x, $y);
                 $image->crop($point, $size);
             }
             // gamma instellen
             if (isset($operations['gamma'])) {
                 $value = $operations['gamma'][0];
                 $image->effects()->gamma($value);
             }
             // grijs kleuren
             if (isset($operations['greyscale'])) {
                 $image->effects()->grayscale();
             }
             // kleuren
             if (isset($operations['colorize'])) {
                 $value = $operations['colorize'][0];
                 $value = $image->palette()->color($value);
                 $image->effects()->colorize($value);
             }
             // vervagen
             if (isset($operations['blur'])) {
                 $value = $operations['blur'][0];
                 $image->effects()->blur($value);
             }
             // schalen
             if (isset($operations['scale'])) {
                 $value = $operations['scale'][0];
                 $size = $image->getSize();
                 $size = $size->scale($value);
                 $image->resize($size);
             }
             // roteren
             if (isset($operations['rotate'])) {
                 $value = $operations['rotate'][0];
                 $image->rotate($value);
             }
             // websafe kleuren
             if (isset($operations['web'])) {
                 $transformation = new WebOptimization();
                 $image = $transformation->apply($image);
             }
             // toon exif data
             if (isset($operations['exif'])) {
                 $this->response->setContentType('application/json');
                 $this->response->setJsonContent(exif_read_data($file->getPath()));
                 $this->response->send();
             } else {
                 $image->show($format, array('quality' => $quality));
             }
         } catch (\Exception $e) {
             $this->deliverOther($file);
         }
     }
 }