public function getExtension() { return pathinfo($this->getBasename(), PATHINFO_EXTENSION); }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { parent::buildView($view, $form, $options); $data = $form->getData(); if (is_string($data)) { $data = ['filename' => basename($data), 'url' => $data]; } if (is_array($data)) { if (!isset($view->var['url']) && isset($data['url'])) { $view->vars['url'] = $data['url']; } if (!isset($view->var['filename']) && isset($data['filename'])) { $view->vars['filename'] = $data['filename']; } } // autodetect type of upload $uploadtype = $options['uploadtype']; if (isset($view->vars['filename'])) { $file = new File($view->vars['filename'], false); $view->vars['filetype'] = $file->getExtension(); switch ($file->getExtension()) { case 'png': case 'jpg': case 'jpeg': case 'gif': $uploadtype = 'image'; break; case 'mp4': $uploadtype = 'video'; break; case 'mp3': case 'm4v': $uploadtype = 'audio'; break; default: break; } } $view->vars['uploadtype'] = $uploadtype; $view->vars['placeholdWidth'] = $options['placeholdWidth']; $view->vars['placeholdHeight'] = $options['placeholdHeight']; $view->vars['placeholdText'] = $options['placeholdText']; $view->vars['ratio'] = $options['ratio']; $view->vars['remove'] = $options['remove']; $pattern = $options['pattern']; if ($pattern != null) { if (!$this->container->hasParameter($pattern)) { throw new \Exception('You must define an existing pattern'); } $patternSize = $this->container->getParameter($pattern); $view->vars['label'] .= ' (Max width: ' . $patternSize['width'] . ', Max height: ' . $patternSize['height'] . ')'; } }