ImageService.php (2114B)
1 <?php 2 3 namespace PartKeepr\ImageBundle\Services; 4 5 use Doctrine\ORM\EntityManager; 6 use PartKeepr\ImageBundle\Entity\CachedImage; 7 use PartKeepr\UploadedFileBundle\Entity\UploadedFile; 8 use PartKeepr\UploadedFileBundle\Services\UploadedFileService; 9 use Symfony\Component\HttpFoundation\File\File; 10 11 class ImageService extends UploadedFileService 12 { 13 /** 14 * {@inheritdoc} 15 */ 16 public function replaceFromFilesystem(UploadedFile $file, File $filesystemFile) 17 { 18 parent::replaceFromFilesystem($file, $filesystemFile); 19 $this->invalidate($file); 20 } 21 22 /** 23 * Invalidates any cached files. 24 * 25 * @param UploadedFile $file The file to invalidate 26 */ 27 public function invalidate(UploadedFile $file) 28 { 29 /** 30 * @var EntityManager 31 */ 32 $entityManager = $this->container->get('doctrine')->getManager(); 33 $queryBuilder = $entityManager->createQueryBuilder(); 34 $queryBuilder->select(['c']) 35 ->from('PartKeepr\ImageBundle\Entity\CachedImage', 'c') 36 ->where('c.originalId = :id') 37 ->andWhere('c.originalType = :type') 38 ->setParameter('id', $file->getId()) 39 ->setParameter('type', $file->getType()); 40 41 $query = $queryBuilder->getQuery(); 42 43 foreach ($query->getResult() as $file) { 44 /** 45 * @var CachedImage 46 */ 47 if (file_exists($file->getCacheFile())) { 48 unlink($file->getCacheFile()); 49 } 50 $entityManager->remove($file); 51 } 52 } 53 54 /** 55 * Checks if the system can handle the given mime type as image. Currently hardcoded for GD. 56 * 57 * @param $mimeType The mime type to check 58 * 59 * @return bool True if the system can display images of the given mimetype, false otherwise 60 */ 61 public function canHandleMimetype($mimeType) 62 { 63 switch ($mimeType) { 64 case 'image/jpeg': 65 case 'image/png': 66 case 'image/gif': 67 return true; 68 default: 69 return false; 70 } 71 } 72 }