Exemplo n.º 1
0
 /**
  * @param File $file
  */
 public function upload(MediaFile $file)
 {
     if ($file->getHeight() && $file->getWidth()) {
         $category = MediaTypes::IMAGE;
     } else {
         $category = MediaTypes::DOCUMENT;
     }
     $oCategory = $this->objectManager->getRepository('BigfishMediaBundle:Category')->find($category);
     $fs = new Filesystem();
     $checksum = $file->getChecksum();
     $checkIfExists = $this->objectManager->getRepository('BigfishMediaBundle:Attachment')->findOneBy(array('checksum' => $checksum));
     if ($checkIfExists) {
         $fs->remove(array($file->getRealPath()));
         return $checkIfExists;
     }
     $baseName = str_replace('.' . $file->getExtension(), '', $file->getFilename());
     $entity = new Attachment();
     $entity->setChecksum($checksum);
     $entity->setName($baseName);
     $entity->setFile($file->getFilename());
     $entity->setExtension($file->getExtension());
     $entity->setSize($file->getSize());
     $entity->setMimeType($file->getMimeType());
     $entity->setWidth($file->getWidth());
     $entity->setHeight($file->getHeight());
     $entity->setCategory($oCategory);
     $this->objectManager->persist($entity);
     $this->objectManager->flush();
     $file->move($this->uploadPath, $file->getFilename());
     return $entity;
 }
Exemplo n.º 2
0
 public function execute()
 {
     /** @var $dimensionSettings ImageDimension **/
     $dimensionSettings = $this->cropping->getDimension();
     // first delete old file
     $oldFile = sprintf('%s/%s.%s', $this->uploadPath, $this->oldFile->getFileName(), $this->attachment->getExtension());
     if (file_exists($oldFile)) {
         @unlink($oldFile);
     }
     /* @var Box */
     $size = $this->setBox($dimensionSettings);
     $originalFile = sprintf('%s/%s.%s', $this->uploadPath, $this->attachment->getName(), $this->attachment->getExtension());
     $checkFilename = sprintf('%s/%s.%s', $this->uploadPath, $this->cropping->getFileName(), $this->attachment->getExtension());
     if ($this->oldFile->getName() == $this->cropping->getName() && file_exists($checkFilename)) {
         $cropName = $this->cropping->getFileName();
     } else {
         $fileDuplicateChecker = $this->checkDuplicateFile($this->cropping->getName(), $this->attachment->getExtension());
         $cropName = $fileDuplicateChecker['name'];
         $counter = $fileDuplicateChecker['counter'];
         $this->cropping->setCounter($counter);
     }
     $cropFile = sprintf('%s/%s.%s', $this->uploadPath, $cropName, $this->attachment->getExtension());
     $this->imagine->open($originalFile)->crop(new Point($this->x, $this->y), new Box($this->width, $this->height))->save($cropFile);
     $this->imagine->open($cropFile)->thumbnail($size, ImageInterface::THUMBNAIL_INSET)->save($cropFile);
     $this->cropping->setFileName($cropName);
     $this->cropping->setChecksum(sha1_file($cropFile));
     $initSize = new MediaFile($cropFile);
     $this->cropping->setClientHeight($initSize->getHeight());
     $this->cropping->setClientWidth($initSize->getWidth());
     return $this->cropping;
 }