Esempio n. 1
0
 public function testCropImage()
 {
     $image = new Image(__DIR__ . '/../../Fixtures/upload/black.png');
     $image->addFilterCrop(0, 0, 5, 5);
     $base64 = $image->getBase64();
     $this->assertEquals(5, $image->getWidth());
     $this->assertEquals(5, $image->getHeight());
 }
 /**
  * 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;
 }
Esempio n. 3
0
File: Image.php Progetto: hykz/Depot
 /**
  * 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);
 }
Esempio n. 4
0
 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));
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     $configs = $options['configs'];
     $data = $form->getViewData();
     if (!empty($data)) {
         if (!$data instanceof Image) {
             $data = new Image($form->getConfig()->getAttribute('rootDir') . '/' . $data);
         }
         if ($data->hasThumbnail($this->selected)) {
             $thumbnail = $data->getThumbnail($this->selected);
             $view->vars['thumbnail'] = array('file' => $configs['folder'] . '/' . $thumbnail->getFilename(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight());
         }
         $value = $configs['folder'] . '/' . $data->getFilename();
         $view->vars = array_replace($view->vars, array('value' => $value, 'file' => $value, 'width' => $data->getWidth(), 'height' => $data->getHeight()));
     }
     $view->vars['filters'] = $this->filters;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form)
 {
     $configs = $form->getAttribute('configs');
     $data = $form->getClientData();
     if (!empty($data)) {
         if (!$data instanceof Image) {
             $data = new Image($form->getAttribute('rootDir') . '/' . $data);
         }
         if ($data->hasThumbnail($this->selected)) {
             $thumbnail = $data->getTumbnail($this->selected);
             $view->set('thumbnail', array('file' => $configs['folder'] . '/' . $thumbnail->getFilename(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()));
         }
         $value = $configs['folder'] . '/' . $data->getFilename();
         $view->set('value', $value)->set('file', $value)->set('width', $data->getWidth())->set('height', $data->getHeight());
     }
     $view->set('filters', $this->filters);
 }
 public function changeAction()
 {
     $request = $this->container->get('request');
     $rootDir = rtrim($this->container->getParameter('genemu.form.file.root_dir'), '/\\') . DIRECTORY_SEPARATOR;
     $folder = rtrim($this->container->getParameter('genemu.form.file.folder'), '/\\') . DIRECTORY_SEPARATOR;
     $file = $request->get('image');
     $handle = new Image($rootDir . $this->stripQueryString($file));
     switch ($request->get('filter')) {
         case 'rotate':
             $handle->addFilterRotate(90);
             break;
         case 'negative':
             $handle->addFilterNegative();
             break;
         case 'bw':
             $handle->addFilterBw();
             break;
         case 'sepia':
             $handle->addFilterSepia('#C68039');
             break;
         case 'crop':
             $x = $request->get('x');
             $y = $request->get('y');
             $w = $request->get('w');
             $h = $request->get('h');
             $handle->addFilterCrop($x, $y, $w, $h);
             break;
         case 'blur':
             $handle->addFilterBlur();
         default:
             break;
     }
     $handle->save();
     $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]);
         }
         $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('result' => '1', 'file' => $folder . '/' . $handle->getFilename() . '?' . time(), 'thumbnail' => array('file' => $folder . '/' . $thumbnail->getFilename() . '?' . time(), 'width' => $thumbnail->getWidth(), 'height' => $thumbnail->getHeight()), 'image' => array('width' => $handle->getWidth(), 'height' => $handle->getHeight()));
     return new Response(json_encode($json));
 }