src/EventSubscriber/ImgUploaderSubscriber.php line 25
<?phpnamespace App\EventSubscriber;use App\Entity\Creative;use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class ImgUploaderSubscriber implements EventSubscriberInterface{public function onBeforeEntityPersistedEvent(BeforeEntityPersistedEvent $event): void{$creative = $event->getEntityInstance();if (!$creative instanceof Creative) {return;}$imageUrl = '../public/uploads/creatives/' . $event->getEntityInstance()->getPath();$data = getimagesize($imageUrl, $info);$bytes = $this->formatSizeUnits(filesize($imageUrl));$creative->setSize($data[0] . 'x' . $data[1]);$creative->setFilesize($bytes);}public function onBeforeEntityUpdatedEvent(BeforeEntityUpdatedEvent $event): void{$creative = $event->getEntityInstance();if (!$creative instanceof Creative) {return;}$imageUrl = '../public/uploads/creatives/' . $event->getEntityInstance()->getPath();$data = getimagesize($imageUrl, $info);$bytes = $this->formatSizeUnits(filesize($imageUrl));$creative->setSize($data[0] . 'x' . $data[1]);$creative->setFilesize($bytes);}public static function getSubscribedEvents(): array{return [BeforeEntityPersistedEvent::class => 'onBeforeEntityPersistedEvent',BeforeEntityUpdatedEvent::class => 'onBeforeEntityUpdatedEvent',];}private function formatSizeUnits($bytes): string{if ($bytes >= 1073741824) {$bytes = number_format($bytes / 1073741824, 2) . ' GB';} elseif ($bytes >= 1048576) {$bytes = number_format($bytes / 1048576, 2) . ' MB';} elseif ($bytes >= 1024) {$bytes = number_format($bytes / 1024, 2) . ' KB';} elseif ($bytes > 1) {$bytes = $bytes . ' bytes';} elseif ($bytes == 1) {$bytes = $bytes . ' byte';} else {$bytes = '0 bytes';}return $bytes;}}