public function upload()
 {
     $files = $this->request->data('files');
     $config =& $this->config[$this->request->data('config')];
     $output = [];
     $config['directory'] = realpath(WWW_ROOT . $config['path']);
     foreach ($files as $file) {
         if (!in_array(strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)), $config['allowed'])) {
             continue;
         }
         $name = uniqid('', true) . '.' . strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
         $config['destination'] = $config['directory'] . DS . $name;
         $config['url'] = str_replace('//', '/', "{$config['path']}/{$name}");
         $config['rel_table'] = $config['model'];
         $image = new Image($file['tmp_name']);
         $image->maxWidth(1200);
         $name = uniqid('', true) . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
         $image->save($config['destination']);
         $entity = $this->Galleries->newEntity($config);
         if ($result = $this->Galleries->save($entity)) {
             $output[] = ['id' => $result->id, 'url' => $entity->url];
         }
     }
     echo json_encode($output);
 }
Beispiel #2
0
 /**
  * Scales the image proportionally to a width
  *
  * @param int $width
  *
  * @throws \InvalidArgumentException
  * @return void
  */
 public function scaleToWidth($width)
 {
     if (!self::isValidImageDimension($width)) {
         throw new \InvalidArgumentException('Width and Height must be integer > 0.');
     }
     $height = (int) round($width / $this->image->getWidth() * $this->image->getHeight());
     $this->image->scaleToDimensions($width, $height);
 }