示例#1
0
 public function testCropImage()
 {
     $image = new Image(__DIR__ . '/../../Fixtures/upload/black.png');
     $image->addFilterCrop(0, 0, 5, 5);
     $base64 = $image->getBase64();
     $this->assertEquals(5, $image->getWidth());
     $this->assertEquals(5, $image->getHeight());
 }
 public function changeAction()
 {
     $request = $this->container->get('request');
     $rootDir = rtrim($this->container->getParameter('genemu.form.file.root_dir'), '/\\') . DIRECTORY_SEPARATOR;
     $folder = rtrim($this->container->getParameter('genemu.form.file.folder'), '/\\') . DIRECTORY_SEPARATOR;
     $file = $request->get('image');
     $handle = new Image($rootDir . $this->stripQueryString($file));
     switch ($request->get('filter')) {
         case 'rotate':
             $handle->addFilterRotate(90);
             break;
         case 'negative':
             $handle->addFilterNegative();
             break;
         case 'bw':
             $handle->addFilterBw();
             break;
         case 'sepia':
             $handle->addFilterSepia('#C68039');
             break;
         case 'crop':
             $x = $request->get('x');
             $y = $request->get('y');
             $w = $request->get('w');
             $h = $request->get('h');
             $handle->addFilterCrop($x, $y, $w, $h);
             break;
         case 'blur':
             $handle->addFilterBlur();
         default:
             break;
     }
     $handle->save();
     $thumbnail = $handle;
     if ($this->container->hasParameter('genemu.form.image.thumbnails')) {
         $thumbnails = $this->container->getParameter('genemu.form.image.thumbnails');
         foreach ($thumbnails as $name => $thumbnail) {
             $handle->createThumbnail($name, $thumbnail[0], $thumbnail[1]);
         }
         $selected = key(reset($thumbnails));
         if ($this->container->hasParameter('genemu.form.image.selected')) {
             $selected = $this->container->getParameter('genemu.form.image.selected');
         }
         $thumbnail = $handle->getThumbnail($selected);
     }
     $json = array('result' => '1', 'file' => $folder . '/' . $handle->getFilename() . '?' . time(), 'thumbnail' => array('file' => $folder . '/' . $thumbnail->getFilename() . '?' . time(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()), 'image' => array('width' => $handle->getWidth(), 'height' => $handle->getHeight()));
     return new Response(json_encode($json));
 }