88namespace Magento \MediaStorage \Model \File \Validator ;
99
1010use Laminas \Validator \AbstractValidator ;
11+ use Magento \Framework \App \ObjectManager ;
1112use Magento \Framework \File \Mime ;
1213use Magento \Framework \Filesystem \Driver \File ;
14+ use Magento \Framework \Image \Adapter \ConfigInterface ;
1315use Magento \Framework \Image \Factory ;
16+ use Psr \Log \LoggerInterface ;
1417
1518/**
1619 * Image validator
@@ -45,19 +48,35 @@ class Image extends AbstractValidator
4548 */
4649 private $ file ;
4750
51+ /**
52+ * @var ConfigInterface
53+ */
54+ private $ config ;
55+
56+ /**
57+ * @var LoggerInterface
58+ */
59+ private $ logger ;
60+
4861 /**
4962 * @param Mime $fileMime
5063 * @param Factory $imageFactory
5164 * @param File $file
65+ * @param ConfigInterface|null $config
66+ * @param LoggerInterface|null $logger
5267 */
5368 public function __construct (
5469 Mime $ fileMime ,
5570 Factory $ imageFactory ,
56- File $ file
71+ File $ file ,
72+ ConfigInterface $ config = null ,
73+ LoggerInterface $ logger = null
5774 ) {
5875 $ this ->fileMime = $ fileMime ;
5976 $ this ->imageFactory = $ imageFactory ;
6077 $ this ->file = $ file ;
78+ $ this ->config = $ config ?? ObjectManager::getInstance ()->get (ConfigInterface::class);
79+ $ this ->logger = $ logger ?? ObjectManager::getInstance ()->get (LoggerInterface::class);
6180
6281 parent ::__construct ();
6382 }
@@ -71,12 +90,20 @@ public function isValid($filePath): bool
7190 $ isValid = false ;
7291
7392 if (stripos (serialize ($ this ->imageMimeTypes ), $ fileMimeType ) !== false ) {
93+ $ defaultAdapter = $ this ->config ->getAdapterAlias ();
7494 try {
75- $ image = $ this ->imageFactory ->create ($ filePath );
95+ $ image = $ this ->imageFactory ->create ($ filePath , $ defaultAdapter );
96+ $ image ->open ();
97+ $ isValid = true ;
98+ } catch (\InvalidArgumentException $ e ) {
99+ $ adapters = $ this ->config ->getAdapters ();
100+ unset($ adapters [$ defaultAdapter ]);
101+ $ image = $ this ->imageFactory ->create ($ filePath , array_key_first ($ adapters ) ?? null );
76102 $ image ->open ();
77103 $ isValid = true ;
78104 } catch (\Exception $ e ) {
79105 $ isValid = false ;
106+ $ this ->logger ->critical ($ e , ['exception ' => $ e ]);
80107 }
81108 }
82109
0 commit comments