-
-
Save sheerif/1080c1bc06185b1d5536393ef5cf7fcf to your computer and use it in GitHub Desktop.
Revisions
-
cirykpopeye revised this gist
Dec 9, 2018 . 2 changed files with 31 additions and 37 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,23 @@ <?php /** * Created by PhpStorm. * User: ciryk * Date: 9/12/18 * Time: 21:27 */ namespace App\Form; use App\Entity\Image; use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\OptionsResolver\OptionsResolver; class ImageType extends FileType { private $imagePath; /** * ImageType constructor. * @param $imagePath @@ -23,37 +27,35 @@ public function __construct($imagePath) $this->imagePath = $imagePath; } public function buildForm(FormBuilderInterface $builder, array $options) { parent::buildForm($builder, $options); $builder->addModelTransformer(new CallbackTransformer( function(Image $image = null) { if ($image instanceof Image) { return new File($this->imagePath . $image->getFile()); } }, function(UploadedFile $uploadedFile = null) { if ($uploadedFile instanceof UploadedFile) { $image = new Image(); $image->setFile($uploadedFile); return $image; } } )); } public function getBlockPrefix() { return 'image'; } public function configureOptions(OptionsResolver $resolver) { parent::configureOptions($resolver); $resolver->setDefaults([ 'required' => false ]); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,13 +2,11 @@ /** * Created by PhpStorm. * User: ciryk * Date: 9/12/18 * Time: 21:28 */ namespace App\EventListener; use App\Entity\Image; use App\Service\FileUploader; use Doctrine\ORM\Event\LifecycleEventArgs; @@ -18,7 +16,6 @@ class ImageUploadListener { private $uploader; /** * ImageUploadListener constructor. * @param $uploader @@ -27,27 +24,22 @@ public function __construct(FileUploader $uploader) { $this->uploader = $uploader; } public function prePersist(LifecycleEventArgs $args) { $entity = $args->getEntity(); $this->uploadFile($entity); } public function preUpdate(PreUpdateEventArgs $args) { $entity = $args->getEntity(); $this->uploadFile($entity); } private function uploadFile($entity) { if (!$entity instanceof Image) { return; } $file = $entity->getFile(); if ($file instanceof UploadedFile) { $filename = $this->uploader->upload($file); $entity->setFile($filename); -
cirykpopeye revised this gist
Jul 16, 2018 . 1 changed file with 59 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ <?php namespace App\Form; use App\Entity\Image; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\OptionsResolver\OptionsResolver; class ImageType extends AbstractType { private $imagePath; /** * ImageType constructor. * @param $imagePath */ public function __construct($imagePath) { $this->imagePath = $imagePath; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('file', FileType::class, array( 'label' => 'Image (jpg/jpeg/png)', 'image_property' => 'file', 'required' => false )) ->add('alt', TextType::class, array( 'label' => 'Alt text', 'required' => false )); $builder->get('file')->addModelTransformer(new CallbackTransformer( function($value) { if (file_get_contents($this->imagePath . '/' . $value)) { return new File($this->imagePath . '/' . $value); } return null; }, function($value) { return $value; } )); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => Image::class )); } } -
cirykpopeye renamed this gist
Jul 16, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
cirykpopeye created this gist
Jul 16, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ <?php /** * Created by PhpStorm. * User: ciryk * Date: 11/07/18 * Time: 19:23 */ namespace App\Service; use Symfony\Component\HttpFoundation\File\UploadedFile; class FileUploader { private $targetDirectory; /** * FileUploader constructor. * @param $targetDirectory */ public function __construct($targetDirectory) { $this->targetDirectory = $targetDirectory; } public function upload(UploadedFile $file) { $filename = md5(uniqid()) . '.' . $file->guessExtension(); $file->move($this->targetDirectory, $filename); return $filename; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,91 @@ <?php /** * Created by PhpStorm. * User: ciryk * Date: 11/07/18 * Time: 18:56 */ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity * @ORM\Table(name="image") */ class Image { /** * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * @ORM\Column(type="integer") */ private $id; /** * @var string $file * @ORM\Column(type="string") * * @Assert\NotBlank(message="Please, upload an image first.") * @Assert\File(mimeTypes={ "image/png", "image/jpeg", "image/jpg" }) */ private $file; /** * @ORM\Column(type="string", nullable=true) */ private $alt; /** * @return mixed */ public function getId() { return $this->id; } /** * @param mixed $id */ public function setId($id): void { $this->id = $id; } /** * @return string */ public function getFile() { return $this->file; } /** * @param string $file */ public function setFile($file) { if ($file) { $this->file = $file; } } /** * @return mixed */ public function getAlt() { return $this->alt; } /** * @param mixed $alt */ public function setAlt($alt) { $this->alt = $alt; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ <?php /** * Created by PhpStorm. * User: ciryk * Date: 11/07/18 * Time: 23:37 */ namespace App\EventListener; use App\Entity\Image; use App\Service\FileUploader; use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs; use Symfony\Component\HttpFoundation\File\UploadedFile; class ImageUploadListener { private $uploader; /** * ImageUploadListener constructor. * @param $uploader */ public function __construct(FileUploader $uploader) { $this->uploader = $uploader; } public function prePersist(LifecycleEventArgs $args) { $entity = $args->getEntity(); $this->uploadFile($entity); } public function preUpdate(PreUpdateEventArgs $args) { $entity = $args->getEntity(); $this->uploadFile($entity); } private function uploadFile($entity) { if (!$entity instanceof Image) { return; } $file = $entity->getFile(); if ($file instanceof UploadedFile) { $filename = $this->uploader->upload($file); $entity->setFile($filename); } } }