private function deleteImageFile(Images $image, $directoryPath) { $fileName = $image->getImageName(); $filePath = $directoryPath . '/' . $fileName; if (file_exists($filePath)) { unlink($filePath); } }
/** * @param string $url * @param Images $image * @return Images * @throws \Exception */ private function generateFromRemote($url, Images $image) { $client = new Client(); $urlParts = parse_url($url); if (empty($urlParts['path'])) { throw new \Exception('Invalid url'); } $pathParts = pathinfo($urlParts['path']); if (empty($pathParts['extension'])) { throw new \Exception('Remote path is not pointing to a file'); } $imageId = $image->getId(); $ds = DIRECTORY_SEPARATOR; $tempDestination = sys_get_temp_dir() . $ds . $pathParts['basename']; $response = $client->get($url, ['save_to' => $tempDestination]); if ($response->getStatusCode() != 200) { throw new \Exception('Cannot receive the remote file'); } $isImage = getimagesize($tempDestination); if (!$isImage) { throw new \Exception('File is not an image'); } $relativePath = $imageId . $ds . $pathParts['basename']; $fileManager = new Filesystem(); $fileManager->mkdir($this->uploadPath . $ds . $imageId); $fileManager->rename($tempDestination, $this->uploadPath . $relativePath); $image->setPath($relativePath); $this->entityManager->persist($image); $this->entityManager->flush(); return $image; }
/** * @param Images $image * @return \Symfony\Component\Form\Form */ private function createDeleteForm(Images $image) { return $this->createFormBuilder()->setAction($this->generateUrl('_delete', ['id' => $image->getId()]))->setMethod('DELETE')->getForm(); }