setCreated() public method

Sets the value of created.
public setCreated ( DateTime $created ) : self
$created DateTime the created
return self
Ejemplo n.º 1
0
 /**
  * Upload image and create entity
  *
  * @param UploadedFile   $file
  * @param array          $attributes
  * @param ImageInterface $image
  *
  * @return LocalImage
  */
 public function upload(UploadedFile $file, array $attributes, ImageInterface $image = null, $keepRatio = true)
 {
     $filesystem = new Filesystem();
     $imagine = new Imagine();
     $mimeType = $file->getClientMimeType();
     if (!in_array($mimeType, $this->supportedTypes)) {
         throw new \InvalidArgumentException('Unsupported image type ' . $mimeType . '.');
     }
     if (!file_exists($this->config['image_path']) || !is_writable($this->config['image_path'])) {
         throw new FileException('Directory ' . $this->config['image_path'] . ' is not writable');
     }
     if (!file_exists($this->config['thumbnail_path']) || !is_writable($this->config['thumbnail_path'])) {
         throw new FileException('Directory ' . $this->config['thumbnail_path'] . ' is not writable');
     }
     $attributes = array_merge(array('content_type' => $mimeType), $attributes);
     if (!is_null($image)) {
         if (file_exists($image->getPath())) {
             $filesystem->remove($image->getPath());
         }
         if (file_exists($image->getThumbnailPath())) {
             unlink($this->config['thumbnail_path'] . $image->getThumbnailPath(true));
         }
     } else {
         $image = new LocalImage($file->getClientOriginalName());
         $image->setCreated(new \DateTime());
         $this->orm->persist($image);
     }
     list($width, $height) = getimagesize($file->getRealPath());
     $image->setWidth($width);
     $image->setHeight($height);
     $this->fillImage($image, $attributes);
     $this->orm->flush();
     $imagePath = $this->generateImagePath($image->getId(), $file->getClientOriginalExtension());
     $thumbnailPath = $this->generateThumbnailPath($image->getId(), $file->getClientOriginalExtension());
     $image->setBasename($this->generateImagePath($image->getId(), $file->getClientOriginalExtension(), true));
     $image->setThumbnailPath($this->generateThumbnailPath($image->getId(), $file->getClientOriginalExtension(), true));
     $this->orm->flush();
     try {
         $file->move($this->config['image_path'], $this->generateImagePath($image->getId(), $file->getClientOriginalExtension(), true));
         $filesystem->chmod($imagePath, 0644);
         if ($keepRatio) {
             $ratioOrig = $width / $height;
             $ratioNew = $this->config['thumbnail_max_size'] / $this->config['thumbnail_max_size'];
             if ($ratioNew > $ratioOrig) {
                 $newImageWidth = $this->config['thumbnail_max_size'] * $ratioOrig;
                 $newImageHeight = $this->config['thumbnail_max_size'];
             } else {
                 $newImageWidth = $this->config['thumbnail_max_size'];
                 $newImageHeight = $this->config['thumbnail_max_size'] / $ratioOrig;
             }
         } else {
             $newImageWidth = $this->config['thumbnail_max_size'];
             $newImageHeight = $this->config['thumbnail_max_size'];
         }
         $imagine->open($imagePath)->resize(new Box($newImageWidth, $newImageHeight))->save($thumbnailPath, array());
         $filesystem->chmod($thumbnailPath, 0644);
     } catch (\Exceptiom $e) {
         $filesystem->remove($imagePath);
         $filesystem->remove($thumbnailPath);
         $this->orm->remove($image);
         $this->orm->flush();
         throw new \Exception($e->getMessage(), $e->getCode());
     }
     $this->cacheService->clearNamespace('image');
     return $image;
 }