示例#1
0
 public function resize(\Symforce\AdminBundle\Entity\File $file, $_crop, array $config)
 {
     $crop = json_decode($_crop, true);
     $imagine = $this->getImagine();
     $box_size = new \Imagine\Image\Box($config[0][0] / $crop['width'], $config[0][1] / $crop['height']);
     $crop_point = new \Imagine\Image\Point($box_size->getWidth() * $crop['left'], $box_size->getHeight() * $crop['top']);
     $crop_size = new \Imagine\Image\Box($config[0][0], $config[0][1]);
     while ($crop_point->getX() + $crop_size->getWidth() > $box_size->getWidth()) {
         if ($crop_point->getX() < 1) {
             break;
         }
         $crop_point = new \Imagine\Image\Point($crop_point->getX() - 1, $crop_point->getY());
     }
     while ($crop_point->getY() + $crop_size->getHeight() > $box_size->getHeight()) {
         if ($crop_point->getY() < 1) {
             break;
         }
         $crop_point = new \Imagine\Image\Point($crop_point->getX(), $crop_point->getY() - 1);
     }
     $options = array('quality' => 100);
     $img = $imagine->load(stream_get_contents($file->getContent()));
     $img->resize($box_size)->crop($crop_point, $crop_size);
     $data = $img->get($file->getExt(), $options);
     $stream = fopen('php://memory', 'r+');
     fwrite($stream, $data);
     rewind($stream);
     $file->setContent($stream);
     if ($config[1][0] && $config[1][1]) {
         $small_box = new \Imagine\Image\Box($config[1][0], $config[1][1]);
         $img = $imagine->load($data)->resize($small_box);
         $small_data = $img->get($file->getExt(), $options);
         $small_stream = fopen('php://memory', 'r+');
         fwrite($small_stream, $small_data);
         rewind($small_stream);
         $file->setPreview($small_stream);
     }
     $file->setUpdated(new \DateTime('now'));
 }
 /**
  * @Route("/upload/save/{admin_name}/{field_name}", name="sf_upload_save")
  * @Template()
  */
 public function imageAction(Request $request, $admin_name, $field_name)
 {
     $entity_id = (int) $request->get('id');
     $loader = $this->container->get('sf.admin.loader');
     if (!$loader->hasAdminName($admin_name)) {
         return $this->onError(sprintf('admin `%s` not exists(%d)!', $admin_name, __LINE__));
     }
     $admin = $loader->getAdminByName($admin_name);
     $config = $admin->getDoctrineConfigBy('file', $field_name);
     if (!$config) {
         return $this->onError(sprintf('admin `%s` file `%s` not exists(%d)!', $admin_name, $field_name, __LINE__));
     }
     $option = $admin->getFormOption($field_name);
     if (!$option) {
         return $this->onError(sprintf('admin `%s` file `%s` not exists(%d)!', $admin_name, $field_name, __LINE__));
     }
     /**
      * @var \Symfony\Component\HttpFoundation\File\UploadedFile
      */
     $handel = $request->files->get('attachment');
     if (!$handel || !$handel->isValid()) {
         return $this->onError('upload invalid');
     }
     $ext = strtolower(pathinfo($handel->getClientOriginalName(), PATHINFO_EXTENSION));
     if (!in_array($ext, $option['ext'])) {
         return $this->onError(sprintf('only allow (%s) you upload %s', join(', ', $option['ext']), $ext));
     }
     $session_id = $this->getRequest()->getSession()->getId();
     $file = new File();
     $file->setSessionId($session_id);
     $file->setName($handel->getClientOriginalName());
     $file->setExt($ext);
     $file->setSize($handel->getClientSize());
     $file->setType($handel->getMimeType());
     $file->setClassName($admin->getClassName());
     $file->setPropertyName($field_name);
     $file->setEntityId($entity_id);
     $stream = fopen($handel->getPathname(), 'rb');
     $data = stream_get_contents($stream);
     $file->setContent($data);
     $em = $this->get('doctrine')->getManager();
     $_file = $em->getRepository('Symforce\\AdminBundle\\Entity\\File')->loadByURL($request->get('url'));
     if ($_file && $admin->getClassName() === $_file->getClassName() && $field_name === $_file->getPropertyName() && $entity_id === $_file->getEntityId() && $session_id === $_file->getSessionId()) {
         $em->remove($_file);
     }
     $em->persist($file);
     $em->flush();
     return $this->sendJSON(array('url' => $this->getParameter('sf.web_assets_dir') . $file->__toString(), 'name' => $file->getName(), 'ext' => $ext, 'size' => $file->getSize()));
 }