Пример #1
0
 public function uploadAction()
 {
     $handle = $this->container->get('request')->files->get('Filedata');
     $folder = $this->container->getParameter('genemu.form.file.folder');
     $uploadDir = $this->container->getParameter('genemu.form.file.upload_dir');
     $name = uniqid() . '.' . $handle->guessExtension();
     $json = array();
     if ($handle = $handle->move($uploadDir, $name)) {
         $json = array('result' => '1', 'thumbnail' => array(), 'image' => array(), 'file' => '');
         if (preg_match('/image/', $handle->getMimeType())) {
             $handle = new Image($handle->getPathname());
             $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]);
                 }
                 if (0 < count($thumbnails)) {
                     $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_replace($json, array('thumbnail' => array('file' => $folder . '/' . $thumbnail->getFilename() . '?' . time(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()), 'image' => array('width' => $handle->getWidth(), 'height' => $handle->getHeight())));
         }
         $json['file'] = $folder . '/' . $handle->getFilename() . '?' . time();
     } else {
         $json['result'] = '0';
     }
     return new Response(json_encode($json));
 }
 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));
 }