public function uploadAction() { $handle = $this->container->get('request')->files->get('Filedata'); $folder = $this->container->getParameter('genemu.form.file.folder'); $uploadDir = $this->container->getParameter('genemu.form.file.upload_dir'); $name = uniqid() . '.' . $handle->guessExtension(); $json = array(); if ($handle = $handle->move($uploadDir, $name)) { $json = array('result' => '1', 'thumbnail' => array(), 'image' => array(), 'file' => ''); if (preg_match('/image/', $handle->getMimeType())) { $handle = new Image($handle->getPathname()); $thumbnail = $handle; if ($this->container->hasParameter('genemu.form.image.thumbnails')) { $thumbnails = $this->container->getParameter('genemu.form.image.thumbnails'); foreach ($thumbnails as $name => $thumbnail) { $handle->createThumbnail($name, $thumbnail[0], $thumbnail[1]); } if (0 < count($thumbnails)) { $selected = key(reset($thumbnails)); if ($this->container->hasParameter('genemu.form.image.selected')) { $selected = $this->container->getParameter('genemu.form.image.selected'); } $thumbnail = $handle->getThumbnail($selected); } } $json = array_replace($json, array('thumbnail' => array('file' => $folder . '/' . $thumbnail->getFilename() . '?' . time(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()), 'image' => array('width' => $handle->getWidth(), 'height' => $handle->getHeight()))); } $json['file'] = $folder . '/' . $handle->getFilename() . '?' . time(); } else { $json['result'] = '0'; } return new Response(json_encode($json)); }
/** * Get Handle to Path * * @param string $path * * @return File */ private function getHandleToPath($path) { $path = $this->rootDir . '/' . $this->stripQueryString($path); if (is_file($path)) { $handle = new File($path); if (preg_match('/image/', $handle->getMimeType())) { $handle = new Image($handle->getPathname()); } return $handle; } return null; }
/** * Search thumbnails */ public function searchThumbnails() { $thumbnails = array(); $fileExt = $this->guessExtension(); $fileName = $this->getBasename('.' . $fileExt); $files = new Finder(); $files->in($this->getPath())->name($fileName . '*.' . $fileExt)->notName($this->getFilename())->files(); foreach ($files as $file) { $file = new Image($file->getPathname()); $thumbnail = preg_replace('/^' . $fileName . '(\\w+)(.*)/', '$1', $file->getFilename()); $thumbnails[$thumbnail] = $file; } $this->gd->setThumbnails($thumbnails); }