/** * Delete avatar image from disk on user deletion * * @param User $user * @param LifecycleEventArgs $event */ public function preRemove(User $user, LifecycleEventArgs $event) { $oldAvatarImage = $user->getAvatarImage(); if (!empty($oldAvatarImage)) { $this->userService->deleteAvatar($oldAvatarImage); } }
public function postProcess($object) { if (!$object instanceof User) { return; } $imagePath = $object->getAvatarImage(); // No image given, bail out if (empty($imagePath)) { return; } $fullPath = "{$this->vendorBaseDir}/{$imagePath}"; // Image doesn't exist, bail out if (!file_exists($fullPath)) { return; } $mimeType = $this->mimeTypeGuesser->guess($fullPath); $image = new Binary(file_get_contents($fullPath), $mimeType, $this->extensionGuesser->guess($mimeType)); $avatarImage = $this->userService->storeImageAsAvatar($image); $object->setAvatarImage($avatarImage); }