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;
 }
 /**
  * @param Attachment $attachment
  * @param array      $dimensionIds
  *
  * @return ImageInstance
  */
 public function create(Attachment $attachment, DimensionContainer $dimensionContainer)
 {
     $validate = $this->constraints->getConstraints($attachment, $dimensionContainer);
     if ($validate) {
         $fs = new Filesystem();
         $fs->remove(sprintf('%s/%s', $this->tempPath, $attachment->getFile()));
         $fs->remove(sprintf('%s/%s', $this->uploadPath, $attachment->getFile()));
         $this->objectManager->remove($attachment);
         $this->objectManager->flush();
         return $validate;
     }
     $instance = new ImageInstance();
     $instance->setName($attachment->getName());
     $this->objectManager->persist($instance);
     foreach ($dimensionContainer->getDimensions() as $dimension) {
         $cropping = new ImageCropping();
         $cropping->setAttachment($attachment);
         $cropping->setInstance($instance);
         $cropping->setDimension($dimension);
         $cropping->setName($attachment->getName());
         $this->objectManager->persist($cropping);
     }
     $this->objectManager->flush();
     return $instance;
 }