示例#1
0
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form)
 {
     $request = $this->request;
     $configs = $form->getAttribute('configs');
     $data = $form->getClientData();
     $fileMeta = new Upload();
     $uploadDir = "/web/temp";
     $uploadDir .= '/' . date("Y") . '/' . date("m") . '/' . date("d");
     $relativeFilePath = $uploadDir;
     $absolutePath = str_replace('/', '//', $request->getRootDir());
     $absolutePath = str_replace('\\', '\\\\', $absolutePath);
     if (!empty($data)) {
         $absoluteFilePath = $request->getRootDir() . '/../' . $data->getPath() . '/' . $data->getUniqId();
         $relativeFilePath = $data->getPath() . '/' . $data->getUniqId();
         $relativePath = $data->getPath();
         $fileMeta = $this->em->getRepository('LowbiSystemBundle:Upload')->find($data->getId());
         if ($fileMeta == null) {
             $fileMeta = new Upload();
         }
         if ($fileMeta->getTodelete()) {
             try {
                 $this->em->remove($fileMeta);
                 $this->em->flush();
             } catch (\EXception $ex) {
             }
             $view->set('filePath', $relativeFilePath)->set('absolutePath', $absolutePath)->set('value', null)->set('fileMeta', $fileMeta);
             return;
         }
         if (!$data instanceof Image) {
             if ($data instanceof File) {
                 $data = new \Lowbi\SystemBundle\Gd\File\Image($absoluteFilePath);
             } else {
                 $data = new \Lowbi\SystemBundle\Gd\File\Image($absoluteFilePath);
             }
         }
         //if ($data->hasThumbnail($this->selected)) {
         $thumbnail = $data->getThumbnail($this->selected);
         $view->set('thumbnail', array('file' => $relativePath . '/' . $thumbnail->getFilename(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()));
         //}
         if ($configs['custom_storage_folder'] && false === ($value = $form->getClientData()) instanceof File) {
             // This if will be executed only when we load entity with existing file pointed to the folder different
             // from $configs['folder']
         } else {
             $value = $configs['folder'] . '/' . $data->getFilename();
         }
         $view->set('value', $value)->set('file', $value)->set('width', $data->getWidth())->set('height', $data->getHeight())->set('filePath', $relativeFilePath)->set('absolutePath', $absolutePath)->set('fileMeta', $fileMeta);
     } else {
         $view->set('filePath', $relativeFilePath)->set('absolutePath', $absolutePath)->set('fileMeta', $fileMeta);
     }
     $view->set('filters', $this->filters);
 }
示例#2
0
文件: Image.php 项目: r4cker/lowbi
 /**
  * Search thumbnails
  */
 public function searchThumbnails()
 {
     $thumbnails = array();
     $fileExt = $this->guessExtension();
     $fileName = $this->getBasename('.' . $fileExt);
     $files = new Finder();
     $files->in($this->getPath())->name($fileName . '*.' . $fileExt)->notName($this->getFilename())->files();
     foreach ($files as $file) {
         $file = new Image($file->getPathname());
         $thumbnail = preg_replace('/^' . $fileName . '(\\w+)(.*)/', '$1', $file->getFilename());
         $thumbnails[$thumbnail] = $file;
     }
     $this->gd->setThumbnails($thumbnails);
 }
示例#3
0
 public function uploadAction(Request $request)
 {
     $handle = $request->files->get('Filedata');
     if (substr($handle->getClientOriginalName(), strrpos($handle->getClientOriginalName(), ".") + 1) == "php") {
         throw new AccessDeniedException();
     }
     $upload = new Upload();
     $ownerType = $request->request->get('ownerType');
     $ownerId = $request->request->get('ownerId');
     $fileName = $handle->getClientOriginalName();
     $extension = null;
     $folder = $this->container->getParameter('lowbi.form.file.folder');
     $folder .= '/' . date("Y") . '/' . date("m") . '/' . date("d");
     $uploadDir = $this->container->getParameter('lowbi.form.file.upload_dir');
     $uploadDir .= '/' . date("Y") . '/' . date("m") . '/' . date("d");
     //$value = print_r($request,true);
     $logger = $this->get('logger');
     //$logger->info('------------>'. var_export($request->request->get('ownerType'), true));
     if ($this->container->hasParameter('lowbi.form.file.disable_guess_extension')) {
         $name = uniqid() . '.' . substr($handle->getClientOriginalName(), strrpos($handle->getClientOriginalName(), ".") + 1);
         $extension = substr($handle->getClientOriginalName(), strrpos($handle->getClientOriginalName(), ".") + 1);
         $realName = $name;
     } else {
         $name = uniqid() . '.' . $handle->guessExtension();
         $extension = $handle->guessExtension();
         $realName = $name;
     }
     $json = array();
     if (false !== ($handle = $handle->move($uploadDir, $name))) {
         $json = array('result' => '1', 'thumbnail' => array(), 'image' => array(), 'file' => '');
         if (0 === strpos($handle->getMimeType(), 'image')) {
             $handle = new Image($handle->getPathname());
             $thumbnail = $handle;
             if (true === $this->container->hasParameter('lowbi.form.image.thumbnails')) {
                 $thumbnails = $this->container->getParameter('lowbi.form.image.thumbnails');
                 foreach ($thumbnails as $name => $thumbnail) {
                     $handle->createThumbnail($name, $thumbnail[0], $thumbnail[1]);
                 }
                 $selected = key(reset($thumbnails));
                 if (true === $this->container->hasParameter('lowbi.form.image.selected')) {
                     $selected = $this->container->getParameter('lowbi.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';
     }
     //save metadata in Upload entity
     $em = $this->getDoctrine()->getEntityManager();
     try {
         $upload->setName($fileName);
         $upload->setUniqId($realName);
         $logger->info('------------>' . $ownerType);
         $logger->info('------------>' . strlen($ownerType));
         if (strlen($ownerType) >= 5) {
             //check for null
             $upload->setOwnerType($ownerType);
             $upload->setOwnerId($ownerId);
         }
         $upload->setExtension($extension);
         $upload->setPath('/web/' . $folder);
         $upload->setSize(round(filesize($uploadDir . '/' . $realName) / 1024));
         $em->persist($upload);
         $em->flush();
     } catch (\Exception $ex) {
         $logger->info('------------>' . print_r($ex));
     }
     $json['file'] = $upload->getId();
     return new Response(json_encode($json));
 }
示例#4
0
 public function changeAction(Request $request)
 {
     $imageId = $request->request->get('image');
     $em = $this->getDoctrine()->getEntityManager();
     $upload = $em->getRepository('LowbiSystemBundle:Upload')->findOneById($imageId);
     $uniqId = $upload->getUniqId();
     $relativefilePath = $upload->getPath();
     $serverPath = $request->get('absolutePath') . '/../';
     $handle = new Image($serverPath . $relativefilePath . '/' . $uniqId);
     $rootDir = $this->container->getParameter('lowbi.form.file.root_dir') . '/';
     $folder = $this->container->getParameter('lowbi.form.file.folder') . '/';
     //$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 (true === $this->container->hasParameter('lowbi.form.image.thumbnails')) {
         $thumbnails = $this->container->getParameter('lowbi.form.image.thumbnails');
         foreach ($thumbnails as $name => $thumbnail) {
             $handle->createThumbnail($name, $thumbnail[0], $thumbnail[1]);
         }
         $selected = key(reset($thumbnails));
         if ($this->container->hasParameter('lowbi.form.image.selected')) {
             $selected = $this->container->getParameter('lowbi.form.image.selected');
         }
         $thumbnail = $handle->getThumbnail($selected);
     }
     if ($this->container->getParameter('lowbi.form.file.custom_storage_folder')) {
         $filePath = $this->stripQueryString($file);
     } else {
         $filePath = $relativefilePath . '/' . $handle->getFilename();
     }
     $json = array('result' => '1', 'file' => $filePath . '?' . time(), 'thumbnail' => array('file' => $filePath . '?' . time(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()), 'image' => array('width' => $handle->getWidth(), 'height' => $handle->getHeight()));
     return new Response(json_encode($json));
 }